Source

APKinspector / startQT.py

  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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# -*- coding: utf-8 -*-

"""
Module implementing actNew.
"""

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
sys.path.append("./src/images")

if sys.path[0] =="":
    SYSPATH = sys.path[1]
else:
    SYSPATH = sys.path[0]
    

#from PyQt4.QtGui import QMainWindow
#from PyQt4.QtCore import pyqtSignature

from Ui_MainWindow import Ui_mainWindow



from APKInfo import *
import zipfile
import GetMethods

from Graph import *
from xdotParser import *
from jad import *
from newMessageBox import *




class startQT(QMainWindow, Ui_mainWindow):
    """
    Class documentation goes here.
    """
    path2method = {}
    CL = None
    Graph = None

    def __init__(self, parent = None):
        """
        Constructor
        """
        mainWindow=QMainWindow.__init__(self, parent)        
        self.setupUi(self)
        self.mdiArea.addSubWindow(self.win1)
        self.mdiArea.addSubWindow(self.win2)
        
        # init the CFG Tab's Graph scene
        self.Graph = GraphicsView()
        self.Graph.initShow(self.tab_cfg, self.gridLayout_11)
    
    @pyqtSignature("")
    def on_actNew_triggered(self):
        """
        Slot documentation goes here.
        """
        dlg = QFileDialog(self)
        filename = dlg.getOpenFileName(self, self.tr("Open APK File"), QString(),  self.tr("APK Files(*.apk)"))
        if not zipfile.is_zipfile(filename):
            self.msgbox = QMessageBox()
            self.msgbox.setText("Please select the APK file correctly!")
            self.msgbox.setWindowTitle("Warning!")
            self.msgbox.show()
        else:
            apk = APK(filename)
            
            # judge this APK is valid or invalid
            if not apk.isVaildAPK():
                self.msgbox = QMessageBox()
                self.msgbox.setText("This APK is invalid!")
                self.msgbox.setWindowTitle("Error!")
                self.msgbox.show()
                return
                
            
            # clear all the last info
            self.listWidget_strings.clear()
            self.listWidget_classes.clear()
                
            self.Graph.scene.clear()
            self.plainTextEdit_smali.setPlainText("")
            self.plainTextEdit_java.setPlainText("")
            self.plainTextEdit_bytecode.setPlainText("")
            
            self.Tab_APKInfo(apk)
            self.Tab_Methods(apk)
            self.Tab_Strings()
            self.Tab_Classes()
            self.Tab_Files(str(filename))
            
            self.tabWidget_2.setCurrentIndex(4)
            self.tabWidget.setCurrentIndex(0)



    @pyqtSignature("")
    def on_About_triggered(self):
        self.msgbox = newMessageBox()
        self.msgbox.setText("Author:  CongZheng(CN)\nMentor: Ryan W Smith(US), Anthony Desnos(FR)\nSupported by the Honeynet Project and Gsoc2011")
        self.msgbox.setWindowTitle("About")
        self.msgbox.show()

##################################################
#Tab_APKInfo:
#    build the APKInfo Tab  
##################################################
    def Tab_APKInfo(self, apk):        
        self.textEdit_receivers = QTextEdit()
        self.textEdit_receivers.setText(apk.getReceivers()[0])
        self.textEdit_services = QTextEdit()
        self.textEdit_services.setText(apk.getServices()[0])
        self.textEdit_permissions = QTextEdit()
        self.textEdit_permissions.setText(apk.getPermissions()[0])

        # get the number of lines
        num_receivers = apk.getReceivers()[1]
        num_services = apk.getServices()[1]
        num_permissions = apk.getPermissions()[1]
        
        # set the default height of textedit widget
        self.textEdit_receivers.setFixedHeight(25*num_receivers)
        self.textEdit_services.setFixedHeight(25*num_services)
        self.textEdit_permissions.setFixedHeight(25*num_permissions)
        
        # add these items to the tableWidget. the last three items is the textEdits as cell widgets 
        self.tableWidget_apkinfo.setItem(0, 0, QTableWidgetItem(apk.getFilename()))
        self.tableWidget_apkinfo.setItem(1, 0, QTableWidgetItem(apk.getVersionCode()))
        self.tableWidget_apkinfo.setItem(2, 0, QTableWidgetItem(apk.getVersionName()))
        self.tableWidget_apkinfo.setItem(3, 0, QTableWidgetItem(apk.getPackage()))
        self.tableWidget_apkinfo.setCellWidget(4, 0, self.textEdit_receivers)
        self.tableWidget_apkinfo.setCellWidget(5, 0, self.textEdit_services)
        self.tableWidget_apkinfo.setCellWidget(6, 0, self.textEdit_permissions)
        
        # resize the tableWidget for adjusting to the content
        self.tableWidget_apkinfo.resizeColumnsToContents()
        self.tableWidget_apkinfo.resizeRowsToContents()
 
 
##################################################
#Tab_Methods:
#    build the Methods Tab  
################################################## 
    def Tab_Methods(self, a):
        clsslice = []
        
        self.CL = GetMethods.CLASS(a)
        
        classes = self.CL.get_class()
        maxdepth = self.CL.get_maxdepth()
        
        self.treeWidget_methods.clear()
        self.treeWidget_methods.setColumnCount(2)
        
        classlist = self.CL.get_classlist()
        classlist.sort()
        
        pathdir = {}
        pathlist = {}
        
        for i in range(0, maxdepth):
            pathdir[i] = ""
            pathlist[i] = "" 
            
        for classname in classlist:
            methodlist = self.CL.get_methods_class(classname)
            if methodlist == []:
                continue
            
            flag = 0
            slice = classname.split("/")
            
            
            for i in range(0, len(slice)-1):
                if slice[i] == pathlist[i]:
                    if flag == 0:
                        continue
                else:
                    flag = 1
                    
                if i == 0:
                    child = QTreeWidgetItem(self.treeWidget_methods)
                    child.setText(0, slice[i])
                    pathdir[i] = child
                    pathlist[i] = slice[i]
                
                else:
                    child = QTreeWidgetItem(pathdir[i-1])
                    child.setText(0, slice[i])
                    pathdir[i] = child
                    pathlist[i] = slice[i]
            
            if flag == 0:
                child = QTreeWidgetItem(pathdir[i])
                child.setText(0, slice[-1])
            else:
                child = QTreeWidgetItem(child)
                child.setText(0, slice[-1])
            
            # add the node child items, which are methods.
            for m in methodlist:
                # build a gloal dir from classname+methodname path to method obj
                self.path2method[classname + m._name] = m
                methodname = self.CL.get_methodname(m)
                childMethod = QTreeWidgetItem(child)
                childMethod.setText(0, methodname)
        
        
        # create a connect between signal and slot.
        self.connect(self.treeWidget_methods, SIGNAL("itemDoubleClicked(QTreeWidgetItem *, int)"), self.locateMethod)

        pheader = self.treeWidget_methods.header()
        pheader.setResizeMode(3)
        pheader.setStretchLastSection(0)
        
        self.treeWidget_methods.setStyleSheet("QTreeView::item:hover{background-color:rgb(0,255,0,50)}"
                                             "QTreeView::item:selected{background-color:rgb(255,0,0,100)}")



##################################################
#locateMethod:
#    locate the method, which has been clicked  
##################################################        
    def locateMethod( self, item, index):  
      if item.childCount() != 0:
          return
      else:
          methodname = item.text(index)
          parentlist = []
          classname = ""
          parent = item.parent()
 
          while parent :
            parentlist.append(parent.text(index))
            parent = parent.parent()

          for i in reversed(parentlist):
              if i == parentlist[0]:
                  classname += i
              else:
                  classname += i + "/"
          
          path = classname + methodname
          method = self.path2method[str(path)]
          
          # after locating the method, get the content for Tab_Smali, Tab_CFG, Tab_Bytecode
          self.Tab_Smali(method)
          self.Tab_CFG(method)
          self.Tab_Bytecode(method)
          self.tabWidget.setCurrentIndex(0)



##################################################
#Tab_Smali:
#    build the Smali Tab  
################################################## 
    def Tab_Smali(self, method):
        smaliContent = ""
        code =  method.get_code()
        
        if code == None:
            self.plainTextEdit_smali.setPlainText("")
        else:
            bc = code.get_bc()
            idx = 0
            linenum = 0
            for i in bc.get():
                smaliContent += str(linenum) + " " + "0x%x" % idx + " "
                smaliContent += i.show_buff(idx) + "\n"
                idx += i.get_length()
                linenum += 1
                
            self.plainTextEdit_smali.setPlainText(smaliContent)



##################################################
#Tab_Classes:
#    build the Classes Tab 
##################################################         
    def Tab_Classes(self):
        Classes = self.CL.vm.get_classes_names()
        for c in Classes:
            listItem = QListWidgetItem(c, self.listWidget_classes)
            listItem.setFlags(Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled)


##################################################
#Tab_Strings:
#    build the Strings Tab 
################################################## 
    def Tab_Strings(self):
        Strings = self.CL.vm.get_strings()
        for s in Strings:
            listItem = QListWidgetItem(s, self.listWidget_strings)
            listItem.setFlags(Qt.ItemIsEditable | Qt.ItemIsSelectable | Qt.ItemIsEnabled)
        


##################################################
#Tab_Bytecode:
#    build the Bytecode Tab 
################################################## 
    def Tab_Bytecode(self, method):
        bytecode = ""
        code = method.get_code()
        
        if code == None:
            self.plainTextEdit_bytecode.setPlainText("")
        else:
            bc = code.get_bc()
            raw = bc.get_raw()
        
            import binascii
            c = binascii.hexlify(raw)
            while c:
                f = c[0:2]
                bytecode += "\\x" + f
                c = c[2:]

            self.plainTextEdit_bytecode.setPlainText(bytecode)



##################################################
#Tab_CFG:
#    build the CFG Tab 
################################################## 
    def Tab_CFG(self, method):
        if method.get_code() == None:
            return
        xdot = XDot(method, self.CL.vm)
        xdot.method2xdot()
        [pagesize, nodeList, linkList] = xdot.parse()
        self.Graph.setPageSize(pagesize)
        self.Graph.show(nodeList, linkList)
        


##################################################
#Tab_Files:
#    build the Files Tab 
################################################## 
    def Tab_Files(self, filename):
        self.treeWidget_files.clear()
        self.treeWidget_files.setColumnCount(2)
        
        # create a connect between signal and slot.
        self.connect(self.treeWidget_files, SIGNAL("itemDoubleClicked(QTreeWidgetItem *, int)"), self.locateFile)

        pheader = self.treeWidget_files.header()
        pheader.setResizeMode(3)
        pheader.setStretchLastSection(0)
        
        self.treeWidget_files.setStyleSheet("QTreeView::item:hover{background-color:rgb(0,255,0,50)}"
                                             "QTreeView::item:selected{background-color:rgb(255,0,0,100)}")
        if decompile(filename) != 1:
            return

        rootpath = SYSPATH +  "/temp/java"
        parent = self.treeWidget_files
        path2parent = {rootpath:parent}
        for root, dirs, files in os.walk(rootpath):
            for f in files:
                parent = path2parent[root]
                child = QTreeWidgetItem(parent)
                child.setText(0, f)
            for d in dirs:
                parent = path2parent[root]
                child = QTreeWidgetItem(parent)
                child.setText(0, d)
                path2parent[root + "/" +d] = child
                


##################################################
#locateFile:
#    locate the Java file, which has been clicked  
################################################## 
    def locateFile( self, item, index):  
      if item.childCount() != 0:
          return
      else:
          filename = item.text(index)
          parentlist = []
          path = ""
          parent = item.parent()
 
          while parent :
            parentlist.append(parent.text(index))
            parent = parent.parent()

          for i in reversed(parentlist):
              if i == parentlist[0]:
                  path += i
              else:
                  path += i + "/"
          
          inputpath = SYSPATH + "/temp/java/" + path + "/" + filename
          
          data = open(inputpath, "r").read()
          
          self.Tab_Java(data)
          self.tabWidget.setCurrentIndex(2)




##################################################
#Tab_Java:
#    build the Java Tab 
################################################## 
    def Tab_Java(self, javacode):
        if javacode == None:
            self.plainTextEdit_java.setPlainText("")
        else:
            self.plainTextEdit_java.setPlainText(javacode)
        
 
 
if __name__ == "__main__":
    app= QApplication(sys.argv)
    myapp = startQT()
    myapp.show()
    sys.exit(app.exec_())
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.