Self-Improvement

IDA API idautils 기본 예제들 (7.4 이하, 7.4 이상) 본문

IDA API(python.. etc)

IDA API idautils 기본 예제들 (7.4 이하, 7.4 이상)

JoGeun 2021. 5. 4. 13:10

system 함수를 호출 전 -x10 에서부터 sprintf 함수를 수행하는 로직을 찾는 코드

import idautils

count=0
system = LocByName("system")	# system 함수

for ref in CodeRefsTo(system, 0):	# system 주소를 하나씩
    current = ref - 0x10	# system 주소 -0x10
    while current < ref:
        current += 4	# 4byte 씩 증가
        op = GetOpnd(current, 0)	# current 주소에서 첫 번째 operand 값 
        if op == "sprintf":	# 첫 번째 operand 가 sprintf 이면
            print("Possible vulnerable code path at : " +hex(ref).replace("L", "")+"->"+idc.GetDisasm(ref))	# 해당 system 주소와 명령어
            count+=1
            SetColor(ref, CIC_ITEM, 0x0000ff)

print ("[+] Found %d locations" %count)

 

인터넷에서 얻어온 것 원리는 같다.

#coding:utf-8
from idaapi import *

def judgeAduit(addr):
    '''
    not safe function handler
    '''
    MakeComm(addr,"### AUDIT HERE ###")
    SetColor(addr,CIC_ITEM,0x0000ff)  #set backgroud to red
    pass

def flagCalls(danger_funcs):
    count = 0
    for func in danger_funcs:      
        faddr = LocByName( func )     
        if faddr != BADADDR: 
            # Grab the cross-references to this address         
            cross_refs = CodeRefsTo( faddr, 0 )                       
            for addr in cross_refs:
                count += 1 
                Message("%s[%d] calls 0x%08x\n"%(func,count,addr))  
                judgeAduit(addr)
                    
if __name__ == '__main__':
    '''
    handle all not safe functions
    '''
    print "-------------------------------"
    danger_funcs = ["free","strcpy","sprintf"] 
    flagCalls(danger_funcs)
    print "-------------------------------"

해당 주소 함수의 첫 주소부터 끝 주소까지 첫번째 오퍼랜드를 출력하는 코드

import idautils
from idaapi import *

func = idaapi.get_func(0x1EB6B4)
addr = func.startEA
while addr < func.endEA:
    print(idc.GetOpnd(addr, 0))
    addr = idc.NextHead(addr)

 

 

7.4 이상?부터는 각 함수명이 변경되었다 아래의 링크를 참조하자

ex) LocbyName -> get_name_ea_simple

www.hex-rays.com/products/ida/support/ida74_idapython_no_bc695_porting_guide.shtml

 

Porting from IDAPython 6.x-7.3, to 7.4

 

www.hex-rays.com

 

 

자동으로 bp 걸기

import idautils

count=0
recv = LocByName("recv")	# system 함수

for ref in CodeRefsTo(recv, 0):	# system 주소를 하나씩
    AddBpt(ref)