2017-07-15
libc 함수 주소를 leak하고 libc-database 를 사용해서 오프셋을 구하는게 귀찮아서 yeonnic모듈을 만들면서 추가했습니돠
앞으로 쓰면서 여러 기능을 넣을 예정
2017-09-10
킹갓 폰툴에서 gdb.attach()라는 함수를 접했는데 와우!!! ㄹㅇ 개신세계였습니다.ㅎㅎ
그래서 gdb.attach() 할 때 도움 되는 함수랑 원하는 가젯을 찾는 기능을 추가했습니닷
2018-01-31
get_libc와 leak_dump함수를 추가 하였습니다.
바이너리의 PIE_base주소를 구하는 방식을 수정 했습니다. (나중에 또 고칠것 투성이...ㅠㅠㅠ)
(아직도 많이 허접한 듯 합니다...)
yeonnic.py code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | #-*- coding: utf8 -*- from pwn import * import subprocess import os, sys #libc database 경로 libc_database_path="/home/yeonnic/libc-database/" # cmd명령을 실행하고 결과값 리턴 def execute_cmd(cmd): p=subprocess.Popen(cmd,shell=True, stdout=PIPE) return p.stdout.read() real_path=execute_cmd("pwd").strip() # leak한 주소를 가지고 libc를 찾음 def find_libc(name,addr): os.chdir(libc_database_path) print "\x1b[93m"+'*'*10+"yeonnic"+'*'*10+"\x1b[0m" print 'name :\x1b[92m %s \x1b[0m\naddr : \x1b[92m0x%x\x1b[0m' %(name,addr) cmd="./find %s 0x%x" %(name,addr) output=execute_cmd(cmd) os.chdir(real_path) output=output.split("\n") result=[] for s in output: index=s.find("id ") if index>=0: result.append(s[index+3:-1]) print "find! -> ",result print "\x1b[93m"+'*'*10+"yeonnic"+'*'*10+"\x1b[0m" return result #찾은 libc_id 리스트를 반환 # find_libc함수 가지고 libc를 ELF객체로 넘겨 받는다. def get_libc(name, addr, index=0): libc_id = find_libc(name,addr) libc=ELF(libc_database_path+"db/%s.so" %(libc_id[index])) libc.address = addr - libc.symbols[name] return libc #libc_id와 오프셋을 구할 함수이름을 인자로 넘겨줌 def get_off(libc_id,func_name): os.chdir(libc_database_path) cmd="./dump %s %s" %(libc_id,func_name) output=execute_cmd(cmd) os.chdir(real_path) result=output.split(" ")[2] result=int(result,16) return result #찾은 함수의 오프셋을 정수로 반환 def wait_proc(): #디버깅 할때 쓰려고 만듬 raw_input("wait!!!!") return 0 # PIE가 걸린 실행 파일의 베이스 주소를 구함 def get_pie_base(p): f=open("/proc/%d/maps" %(p.pid),"r") data="" for i in f: if i.find(p.cwd+"/"+re.findall("[^/][-|_|.|a-zA-Z0-9]+",p.argv[0])[-1]) != -1 : data=i.split("-") break f.close() result=int(data[0],16) return result # gdb로 프로세스에 attach 하고 gdb명령어 입력 def gdb_attach(p,cmd): if len(sys.argv)==2: attach_p=gdb.attach(p,cmd) return attach_p else: return 0 # PIE가 걸린 프로세스의 베이스 주소를 구한다음, # 브레이크 포인트를 걸려는 오프셋을 b_off로 넘겨줌 def gdb_pie_attach(p,b_off,cmd=""): if len(sys.argv)==2: pie_base=get_pie_base(p) gdb_cmd="" for off_set in b_off: gdb_cmd+="b *0x%x + 0x%x\n" %(pie_base,off_set) gdb_cmd+=cmd+"c" attach_p=gdb.attach(p,gdb_cmd) return attach_p else: return 0 # 문자열 hex값출력 def print_hex(data): result="" try : for i in data: result+="\\x%02x" %(ord(i)) print result return 0 except: return -1 # ELF객체에서 넘겨받은 asm_s의 어셈블 값의 오프셋을 찾음 def find_gadget(elf,asm_s): result=next(elf.search(asm(asm_s))) return result # 임의주소 덤프 def leak_dump(p, addr, size): if len(sys.argv)==2: print hexdump(p.leak(addr,size), skip=False, begin=addr) return 0 | cs |
'system > etc' 카테고리의 다른 글
libc database 사용법 (0) | 2017.04.15 |
---|