1. 程式人生 > >Hacking: The Art of Exploitation 讀書筆記(一)程式碼除錯技巧

Hacking: The Art of Exploitation 讀書筆記(一)程式碼除錯技巧

GDB 相關 -q 不列印版本資訊 功能:
  • set disassemble-flavor intel /att 設定彙編語法
  • list:列印程式碼
  • disassemble:反彙編
  • break:設定斷點
  • run:執行
  • 檢視記憶體 x
    • 進位制:o x u t
    • 列出附近幾條儲存資訊:x/2
    • 顯示方式:b h w g
    • (gdb) x/8xb $eip
      0x8048384 <main+16>: 0xc7 0x45 0xfc 0x00 0x00 0x00 0x00 0x83
  • info register xxx:列印暫存器資訊
    • x/i $eip
    • x/3i $sip
    • x/3i $eip -4
    • (gdb) print $ebp - 4
      $1 = (void *) 0xbffff804
    • x/xw $1
    • print $1
  • info frame: 列印棧幀 (stack frame) 資訊
  • nexti:單步執行
  • bt: 回溯棧
    • bt full: 包括 stack frame 中的變數值 
程式碼除錯: objdump -M intel -D a.out | grep -A20 main.: 列印從 main 函式開始20行內的彙編指令,使用 Intel 語法 bc -ql 執行命令列計算器 檢查 malloc( ) 函式記憶體分配結果程式碼複用:
void *errorchecked_malloc(unsigned int size){
    void *ptr;
    ptr = malloc(size);
    if(ptr == NULL){
        fprint(stderr, “Error: could not allocate heap memory. \n”);
        exit(-1);
    }
    return ptr;
}

// test
char *sentence = (char *)errorchecked_malloc(20);