Лекция 5
.pdfПодмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
|
|
Изменение дампа памяти процесса (результат)
Ñì. ôàéë avi/pyrasite_shell_server.avi
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
10/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
|
|
Сценарий инъекции кода
Исходные данные. Имеется процесс запущенной ELF-программы.
Ограничения. Изменять исходный и бинарный коды программы, а также перезапускать программу нельзя.
Цель. Требуется вызвать shell-интерпретатор внутри процесса запущенной программы.
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
11/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
man mmap |
|
|
MMAP(2) |
Linux Programmer's Manual |
|
|
MMAP(2) |
|
NAME
mmap, munmap - map or unmap files or devices into memory
SYNOPSIS
#include <sys/mman.h>
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
int munmap(void *addr, size_t length);
See NOTES for information on feature test macro requirements.
DESCRIPTION
mmap() creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is
specified in addr. The length argument specifies the length of the mapping.
If addr is NULL, then the kernel chooses the address at which to create the mapping; this is the most portable method of creating a new mapping. If addr is not NULL, then the kernel takes it as a hint about where to place
the mapping; on Linux, the mapping will be created at a nearby page boundary. The address of the new mapping is returned as the result of the call.
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
12/50 |
Подмена dll |
|
|
Изменение дампа памяти Python-процесса |
Инъекция кода |
|||
|
loop_sleep.c |
|
|
|
|||
1 |
# include |
< stdio .h > |
|
|
|
||
2 |
# include |
< unistd .h > |
|
|
|
||
3 |
# include |
<sys / mman .h > |
|
|
|||
4 |
# include |
<sys / types .h > |
|
|
|||
5 |
# include |
< signal .h > |
|
|
|
||
6 void sig_process ( int |
sig ) |
{ |
|
||||
7 |
static |
void * addr = NULL ; |
|
|
|||
8 |
if ( sig |
== |
SIGUSR1 ) |
{ |
|
|
|
9 |
const |
int |
prot |
= PROT_READ | PROT_WRITE | PROT_EXEC ; |
// 0 x7 |
||
10 |
const |
int |
flags |
= |
MAP_PRIVATE | MAP_ANONYMOUS ; // |
0 x22 |
|
11 |
addr = |
mmap (0 , |
10 , |
prot , |
flags , 0, 0); |
|
12printf (" signal SIGUSR1 was captured \ naddr : %lx\n", addr );
13}
14 |
if (sig |
== SIGUSR2 && addr |
!= NULL ) |
15 |
printf |
(" signal SIGUSR2 was |
captured \n* addr %s\n", ( char *) addr ); |
16}
17int main (int argc , char * argv []) {
18signal ( SIGUSR1 , sig_process );
19signal ( SIGUSR2 , sig_process );
20const int nsec = 5;
21while (1) {
22printf ("%d: I 'm alive and now I want to sleep for %d sec\n",
23getpid (), nsec );
24sleep ( nsec );
25}
26return 0;
27}
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
13/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
./loop_sleep (компиляция и запуск) |
|||
Компиляция |
|
|
|
$ gcc -static loop_sleep.c -o loop_sleep |
|
||
Запуск |
|
|
|
$ ./loop_sleep |
|
$ cat \ |
|
25646: I'm alive and now ... |
|
||
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
|
|
|
|
|
|
|
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
14/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
./loop_sleep (компиляция и запуск) |
|||
Компиляция |
|
|
|
$ gcc -static loop_sleep.c -o loop_sleep |
|
||
Запуск |
|
|
|
$ ./loop_sleep |
|
$ cat \ |
|
25646: I'm alive and now ... |
|
||
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
Нет страниц для чтения/записи/исполнения |
||
|
|
|
|
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
14/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
./loop_sleep (компиляция и запуск) |
|||
Компиляция |
|
|
|
$ gcc -static loop_sleep.c -o loop_sleep |
|
||
Запуск |
|
|
|
$ ./loop_sleep |
|
$ cat \ |
|
25646: I'm alive and now ... |
|
||
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
|
|
|
signal SIGUSR1 was captured |
$ kill -s SIGUSR1 \ |
|
|
addr: 7f50d0456000 |
|
$(pgrep loop_sleep) |
|
|
|
|
|
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
14/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
./loop_sleep (компиляция и запуск) |
|||
Компиляция |
|
|
|
$ gcc -static loop_sleep.c -o loop_sleep |
|
||
Запуск |
|
|
|
$ ./loop_sleep |
|
$ cat \ |
|
25646: I'm alive and now ... |
|
||
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
|
|
|
signal SIGUSR1 was captured |
$ kill -s SIGUSR1 \ |
|
|
addr: 7f50d0456000 |
|
$(pgrep loop_sleep) |
|
25646: I'm alive and now ... |
$ cat \ |
|
|
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
7f50d0456000-7f50d0457000 rwxp ... |
||
|
|
|
|
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
14/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
./loop_sleep (компиляция и запуск) |
|||
Компиляция |
|
|
|
$ gcc -static loop_sleep.c -o loop_sleep |
|
||
Запуск |
|
|
|
$ ./loop_sleep |
|
$ cat \ |
|
25646: I'm alive and now ... |
|
||
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
|
|
|
signal SIGUSR1 was captured |
$ kill -s SIGUSR1 \ |
||
addr: 7f50d0456000 |
|
$(pgrep loop_sleep) |
|
25646: I'm alive and now ... |
$ cat \ |
|
|
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
7f50d0456000 |
-7f50d0457000 rwxp ... |
|
|
|
Появились страницы для |
|
|
|
чтения/записи/исполнения |
|
|
|
|
|
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
14/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
./loop_sleep (компиляция и запуск) |
|||
Компиляция |
|
|
|
$ gcc -static loop_sleep.c -o loop_sleep |
|
||
Запуск |
|
|
|
$ ./loop_sleep |
|
$ cat \ |
|
25646: I'm alive and now ... |
|
||
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
|
|
|
signal SIGUSR1 was captured |
$ kill -s SIGUSR1 \ |
|
|
addr: 7f50d0456000 |
|
$(pgrep loop_sleep) |
|
25646: I'm alive and now ... |
$ cat \ |
|
|
25646: I'm alive and now ... |
/proc/$(pgrep loop_sleep)/maps | \ |
||
25646: I'm alive and now ... |
grep rwx |
|
|
25646: I'm alive and now ... |
7f50d0456000-7f50d0457000 rwxp ... |
||
|
|
$ gdb -q |
|
|
|
(gdb) attach 25646 |
|
|
|
(gdb) p strcpy((char *) |
|
|
|
0x7f50d0456000, "Hello world!") |
|
|
|
$1 = -800759808 |
|
|
|
(gdb) quit |
|
|
|
|
|
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
14/50 |