Source

APKinspector / CallInOut.py

The default branch has multiple heads

Full commit
import re
import copy
import sys

class CallInOut:
    # the global dict
    # idDir:{id ---> method}
    # idDir2:{method ---> id}
    # invokeDir:{the invoked method id---> the invoking method id}
    # invokeDir2:{the invoking method id---> the invoked method id}
    idDir={}
    idDir2={}
    invokeDir={}
    invokeDir2 = {}

    def __init__(self, methodInvokeList):
        self.preprocess(methodInvokeList)
    

    # This function is to pre-process the invoke relations.
    # I just want to add indexes for querying fastly.
    def preprocess(self, methodInvokeList):
        pattern="(.*) ---> (.*)"
        id=0
        ID0=0
        ID1=0
    
        for line in methodInvokeList:
            li=re.search(pattern,line).groups()
            # if the recursion exists, the detect loop will not be end
            if li[0] == li[1]:
                continue		
        
            if li[0] not in list(self.idDir.values()):
                self.idDir[id]=li[0]
                self.idDir2[li[0]]=id
                ID0=id
                id=id+1
            else:
                ID0=self.idDir2[li[0]]
    
            if li[1] not in list(self.idDir.values()):
                self.idDir[id]=li[1]
                self.idDir2[li[1]]=id
                ID1=id
                id=id+1
            else:
                ID1=self.idDir2[li[1]]
            
            if ID1 not in list(self.invokeDir.keys()):
                self.invokeDir[ID1]=[ID0]
            else:
                if ID0 not in self.invokeDir[ID1]:
                    self.invokeDir[ID1].append(ID0)

            if ID0 not in list(self.invokeDir2.keys()):
                self.invokeDir2[ID0]=[ID1]
            else:
                if ID1 not in self.invokeDir2[ID0]:
                    self.invokeDir2[ID0].append(ID1)
    
    
    # This function is to search the "call out" methods
    def searchCallOut(self, method):
        callOutIdList = []
        if method in list(self.idDir.values()):
            if self.idDir2[method] not in self.invokeDir2.keys():
                return []
            else:
                for i in self.invokeDir2[self.idDir2[method]]:
                    callOutIdList.append(self.idDir[i])
        
        return callOutIdList
                
                
                
    # This function is to search the "call in" methods            
    def searchCallIn(self, method):
        callInIdList = []
        idDirList = list(self.idDir.values())
        idDir2List = []
        for m in idDirList:
            if m.find("^") == -1:
                continue
            m0 = m.split("^")[0]
            m1 = m.split("^")[1]

            if m0 == method:
                if self.idDir2[m] not in self.invokeDir.keys():
                    pass
                else:
                    for i in self.invokeDir[self.idDir2[m]]:
                        callInIdList.append(self.idDir[i] + "^" +m1)
        return callInIdList