2017-02-14 1 views
1

나는 파이썬에서 PROCESS_VM_READV를 호출하는 법을 배우려하고있다. the manual에서 읽기, 나는 그들의 예와 비슷한 것을 창조하기로 결정했습니다.python에서 linux syscall PROCESS_VM_READV를 호출하는 방법은 무엇입니까?

루트 액세스 권한이있는 터미널에 python3을 열었습니다. 그런 다음 & 초기화에 필요한 모듈과 변수

예에서
import ctypes 
libc = ctypes.CDLL('libc.so.6') 
vm=libc.process_vm_readv 

을 가져 진행, iovec라는 구조체가있다. 그래서,

class iovec(ctypes.Structure): 
    _fields_=[("iov_base",ctypes.c_void_p),("iov_len",ctypes.c_int)] 

그런 다음 KMines

의 PID와
vm(2242,local,2,remote,1,0) 

을 PROCESS_VM_READV를 호출, 로컬 및 원격

p1=ctypes.c_char_p(b"") 
p1=ctypes.cast(p1,ctypes.c_void_p) 
local=iovec(p1,10) 
remote=iovec(0x00400000,20) # Address of ELF header 

마지막으로 변수를 만들 파이썬에서 다시 작성해야하지만 반환 -1이고 로컬 또는 원격의 iov_base에는 변경 사항이 없습니다. 나는 아주 단순한 실수를하고있는 것처럼 느껴지지만 내 손가락을 대충 댈 수는 없다.

도움을 주시면 감사하겠습니다.

답변

2

너무 늦게 여기에있을 수도 있지만 나는 process_vm_readv 우리가 테스트 목적을 위해 나는 간단한 인사 세계를 컴파일에 GDB를 사용, 올바른 읽을 수있는 원격 주소를 전달해야 here

의 사람에서 예를 복제 할 수 있었다 동일한 결과를 검색하기 위해 파이썬 코드를 올바른 주소 아래

(gdb) break main 
Breakpoint 1 at 0x5a9: file hello.c, line 4. 
(gdb) run 
Starting program: /user/Desktop/hello 
=> 0x800005a9 <main+25>: sub esp,0xc 
    0x800005ac <main+28>: lea edx,[eax-0x19b0] 
    0x800005b2 <main+34>: push edx 
    0x800005b3 <main+35>: mov ebx,eax 
    0x800005b5 <main+37>: call 0x800003f0 <[email protected]> 
    0x800005ba <main+42>: add esp,0x10 
    0x800005bd <main+45>: nop 
    0x800005be <main+46>: lea esp,[ebp-0x8] 
    0x800005c1 <main+49>: pop ecx 
    0x800005c2 <main+50>: pop ebx 
(gdb) x/20b 0x800005a9 
0x800005a9 <main+25>: 0x83 0xec 0x0c 0x8d 0x90 0x50 0xe6 0xff 
0x800005b1 <main+33>: 0xff 0x52 0x89 0xc3 0xe8 0x36 0xfe 0xff 
0x800005b9 <main+41>: 0xff 0x83 0xc4 0x10 

을 읽어

from ctypes import * 

class iovec(Structure): 
    _fields_ = [("iov_base",c_void_p),("iov_len",c_size_t)] 

local = (iovec*2)()    #create local iovec array 
remote = (iovec*1)()[0]  #create remote iovec 
buf1 = (c_char*10)() 
buf2 = (c_char*10)() 
pid = 25117 

local[0].iov_base = cast(byref(buf1),c_void_p) 
local[0].iov_len = 10 
local[1].iov_base = cast(byref(buf2),c_void_p) 
local[1].iov_len = 10 
remote.iov_base = c_void_p(0x800005a9)  #pass valid readable address 
remote.iov_len = 20 


libc = CDLL("libc.so.6") 
vm = libc.process_vm_readv 

vm.argtypes = [c_int, POINTER(iovec), c_ulong, POINTER(iovec), c_ulong, c_ulong] 

nread = vm(pid,local,2,remote,1,0) 

if nread != -1: 
    bytes = "[+] " 
    print "[+] received %s bytes" % (nread) 
    for i in buf1: bytes += hex(ord(i)) + " " 
    for i in buf2: bytes += hex(ord(i)) + " " 
    print bytes 

출력

[email protected]:~/Desktop# python process_vm_readv.py 
[+] received 20 bytes 
[+] 0x83 0xec 0xc 0x8d 0x90 0x50 0xe6 0xff 0xff 0x52 0x89 0xc3 0xe8 0x36 0xfe 0xff 0xff 0x83 0xc4 0x10 
관련 문제