LU-LSP-b13:L09

From DiLab
Revision as of 21:01, 17 March 2020 by Leo (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Praktiskais darbs #9.1 - steka satura analīze un piekļuve stekam.

  • Funkciju izsaukumu analīze. printf() lietošana steka satura izdrukai.
  • Koda disasamblēšana ar objdump vai gdb programmām.
objdump -dS <binary-file> | less
  • Piekļuve reģistru saturam caur inline asamblera kodu.
#define GET_EBP_VALUE(result) \
    asm volatile("movl %%ebp, %0\n" : "=m" (result))

Iesūtīšana

Šis PD nav obligāts, nav jāiesūta.

Uzdevumi:

1) Panākt, ka f() izsauc sevi izmantojot steka pārpildi (bez ASM koda).

2) Panākt, ka f() veiksmīgi izsauc funkciju system() ar argumentu "touch file.txt", izmantojot steka pārpildi

Programmas kods

Kompilēt ar opcijām "-g -fno-stack-protector"

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void f(void) {
    char s[16];
    gets(s);
    printf("%s\n", s);
}

int main(void) {
    f();
    return 0;
}


Modificēts kods:

#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define GET_EBP_VALUE(result)				\
     asm volatile("movl %%ebp, %0\n" : "=m" (result))

#define GET_ESP_VALUE(result)				\
     asm volatile("movl %%esp, %0\n" : "=m" (result))

uint32_t esp, ebp;

void f(void) {
    GET_EBP_VALUE(ebp);
    printf("%p\n", (void *) ebp);

    char s[16];
    gets(s);
    printf("%s\n", s);

    asm("leave");
    GET_ESP_VALUE(esp);
    *(uint32_t *) esp = f;
    asm("pop %eax");
    asm("push %eax");
    asm("jmp %eax");
}

int main(void) {
    f();
    return 0;
}