def getProtectedBroadcastDict(fIn,fInEmu): outdict = {} emuDict={} f = open(fIn, 'r') fEmu = open(fInEmu, 'r') while True: line=fEmu.readline() if not line: break if line.find('.xml:') > -1: #idx1 = line.find('/') + 1 #idx2 = line.find(':') #key = line[idx1:idx2] key = P.getAttrValueByAttrTitle('android:name', line) if not emuDict.has_key(key): emuDict[key] = 'YES' while True: line = f.readline() if not line: break if line.find('.xml:') > -1: #idx1 = line.find('/') + 1 #idx2 = line.find(':') #key = line[idx1:idx2] key = P.getAttrValueByAttrTitle('android:name', line) if not outdict.has_key(key): outdict[key]='NO' if emuDict.has_key(key): outdict[key] = emuDict[key] return outdict
def generateProtectionLevelToProtectionLevelDict(): protectionLevelDict = {} f = open(protectionLevelTxt, 'r') while True: line = f.readline() if not line: break if line.find('android:protectionLevel') > -1: value = P.getAttrValueByAttrTitle('android:protectionLevel', line) key = P.getAttrValueByAttrTitle('android:name', line) protectionLevelDict[key] = value #print protectionLevelDict return protectionLevelDict
def main(): P.prepareFilesFromPhone() if not os.path.exists(P.EmuListPath): print "Please copy emu android manifest running this script! Directory path is:\n" + P.EmuListPath return else: P.prepareDirsAndDicts() _wb = Workbook() PB.Output(_wb) P.Output(_wb) SS.Output(_wb) BP.Output(_wb) _wb.save(outXls) print "Generate xls table successed!! --> %s" % outXls
def genPkgPermissionProtectionLevelDict(fIn): outdict = {} f = open(fIn, 'r') while True: line = f.readline() if not line: break if line.find('.xml:') > -1: idx1 = line.find('/') + 1 idx2 = line.find(':') key = line[idx1:idx2] permissionName = P.getAttrValueByAttrTitle('android:name', line) protectionLevel = P.getAttrValueByAttrTitle('android:protectionLevel', line) if not protectionLevel: protectionLevel = 'Not Found' if not outdict.has_key(key): outdict[key] = {} outdict[key][permissionName] = protectionLevel return outdict
def main(): if len(sys.argv) < 2: print "The software version can not be empty." return outXls = P.outdir + "/AndroidSecurity_"+sys.argv[1]+time.strftime('_%Y%m%d%H%M%S')+".xls" P.prepareFilesFromPhone() if not os.path.exists(P.EmuListPath): print "Please copy emu android manifest running this script! Directory path is:\n" + P.EmuListPath return else: P.prepareDirsAndDicts() _wb = Workbook() PB.Output(_wb) P.Output(_wb) SS.Output(_wb) BP.Output(_wb) _wb.save(outXls) print "Generate xls table successed!! --> %s" % outXls
def main(): P.prepareFilesFromPhone() os.system("adb shell service list > %s" % (P.SystemServiceTxt)) f = open(P.SystemServiceTxt, 'r') f.readline() while True: line=f.readline() if not line: break if line.find(':') > -1: #print line idx1 = line.find(':') key=line[:idx1] idx1=key.index(' ') key=key[idx1:] key=key.strip() SystemSerivceList.append(key) #print key if not os.path.exists(P.EmuListPath): print "Please copy emu android manifest running this script! Directory path is:\n" + EmuListPath return else: #protectedBroadcastDict = getProtectedBroadcastDict(P.ProtectedBroadcastTxt,P.emuProtectedBroadcastTxt) DictExcel = xlrd.open_workbook(P.DictXls) #print DictExcel.sheet_names() SystemServiceSheet = DictExcel.sheet_by_name(u'systemservice') for rownum in range(SystemServiceSheet.nrows): #print SystemServiceSheet.row_values(rownum) key=SystemServiceSheet.row(rownum)[0].value #print key if not SystemSerivceDict.has_key(key): SystemSerivceDict[key]=SystemServiceSheet.row_values(rownum) style = P.setStyles(False) style_title = P.setStyles(True) initWorkbook(style, style_title, SystemSerivceList,SystemSerivceDict)
def main(): P.prepareFilesFromPhone() #get ProtectedBroadcast From EmuManifestListPath --> emuProtectedBroadcastTxt P.grepTagToOutputByPath(P.EmuListPath,'protected-broadcast', P.emuProtectedBroadcastTxt) #get ProtectedBroadcast From ManifestListPath --> protectedBroadcastTxt P.grepTagToOutputByPath(P.ManifestListPath,'protected-broadcast', P.ProtectedBroadcastTxt) if not os.path.exists(P.EmuListPath): print "Please copy emu android manifest running this script! Directory path is:\n" + EmuListPath return else: protectedBroadcastDict = getProtectedBroadcastDict(P.ProtectedBroadcastTxt,P.emuProtectedBroadcastTxt) style = P.setStyles(False) style_title = P.setStyles(True) initWorkbook(style, style_title, protectedBroadcastDict)
def genPkgAndPermssionDict(fIn): outdict = {} f = open(fIn, 'r') while True: line = f.readline() if not line: break if line.find('.xml:') > -1: idx1 = line.find('/') + 1 idx2 = line.find(':') key = line[idx1:idx2] value = P.getAttrValueByAttrTitle('android:name', line) if not outdict.has_key(key): outdict[key] = [] outdict[key].append(value) return outdict
def Output(_wb): #P.prepareFilesFromPhone() P.getProtectLevelFromManifest('permission ', P.protectionLevelTxt) P.getProtectLevelFromManifest('uses-permission', usesProtectionLevelTxt) if not os.path.exists(P.EmuListPath): print "Please copy emu android manifest running this script! Directory path is:\n" + EmuListPath return else: #add by jinshi.song DictExcel = xlrd.open_workbook(P.DictXls) #print DictExcel.sheet_names() BundlePackageSheet = DictExcel.sheet_by_name(u'bundlepackage') for rownum in range(BundlePackageSheet.nrows): #print BundlePackageSheet.row_values(rownum) key=BundlePackageSheet.row(rownum)[1].value #print key if not BundlePackageDict.has_key(key): BundlePackageDict[key]=BundlePackageSheet.row_values(rownum) #P.prepareDirsAndDicts() #P.getProtectLevelFromManifest('permission ', protectionLevelTxt) #P.generatePackageInstallationToPathDict() P.generateProtectionLevelToProtectionLevelDict() pkgProtectionLevelDict = genPkgPermissionProtectionLevelDict(P.protectionLevelTxt) pkgPermissionDict = genPkgAndPermssionDict(P.protectionLevelTxt) pkgUsesPermissionDict = genPkgAndPermssionDict(usesProtectionLevelTxt) #P.filterCustomOEM() #pkgSourceDict = P.genPkgSourceDict(P.outList) #print pkgSourceDict outList = genBundledPkgInfo(pkgPermissionDict, pkgUsesPermissionDict, P.sourceDict, pkgProtectionLevelDict) style = P.setStyles(False) style_title = P.setStyles(True) style_pkg = setPkgStyle() initWorkbook(style, style_title, style_pkg, outList,BundlePackageDict,_wb)
def genBundledPkgInfo(pkgPermissionDict, pkgUsesPermissionDict, pkgSourceDict, pkgProtectionLevelDict): outList = [] #print pkgSourceDict #print P.protectionLevelDict #print P.pathDict #print pkgPermissionDict #print pkgUsesPermissionDict #print pkgProtectionLevelDict #pkgPermissionDict = sorted(pkgPermissionDict.items(), key=lambda e:e[0], reverse=False) for root,dirs,files in os.walk(P.ManifestListPath): for filespath in files: jrdfilepath = os.path.join(root,filespath) pkg = Package() manifestStr = P.getNodeByTag('manifest', jrdfilepath) name = P.getAttrValueByAttrTitle('package', manifestStr) shareUserId = P.getAttrValueByAttrTitle('android:sharedUserId', manifestStr).strip(' ') pkg.name = name if shareUserId == '': pkg.packageUID = 'system assigned' else: pkg.packageUID = shareUserId if P.pathDict.has_key(name): tmpStr = P.pathDict[name] idx = tmpStr.rfind('/') pkg.location = tmpStr[:idx] pkg.apkname = tmpStr[idx+1:] if pkgSourceDict.has_key(name): pkg.source = pkgSourceDict[name] if pkgPermissionDict.has_key(filespath): for per in pkgPermissionDict[filespath]: permission = Permission() permission.name = per if pkgProtectionLevelDict[filespath].has_key(per): permission.protectionLevel = checkProtectionLevelValue(pkgProtectionLevelDict[filespath][per]) else: permission.protectionLevel = 'Not Found' pkg.permission.append(permission) if pkgUsesPermissionDict.has_key(filespath): for per in pkgUsesPermissionDict[filespath]: permission = Permission() permission.name = per tmpProtectionLevel = '' if pkgProtectionLevelDict.has_key(filespath): if pkgProtectionLevelDict[filespath].has_key(per): #permission.protectionLevel = checkProtectionLevelValue() tmpProtectionLevel = pkgProtectionLevelDict[filespath][per] elif P.protectionLevelDict.has_key(per): #permission.protectionLevel = checkProtectionLevelValue(P.protectionLevelDict[per]) tmpProtectionLevel = P.protectionLevelDict[per] #print '@@@ ' + tmpProtectionLevel else: tmpProtectionLevel = 'Not Found' elif P.protectionLevelDict.has_key(per): #permission.protectionLevel = checkProtectionLevelValue(P.protectionLevelDict[per]) tmpProtectionLevel = P.protectionLevelDict[per] #print '@@@ ' + tmpProtectionLevel else: tmpProtectionLevel = 'Not Found' permission.protectionLevel = checkProtectionLevelValue(tmpProtectionLevel) pkg.usesPermission.append(permission) outList.append(pkg) return outList
def genBundledPkgInfo(pkgPermissionDict, pkgUsesPermissionDict, pkgSourceDict, pkgProtectionLevelDict): outList = [] #print pkgSourceDict #print P.protectionLevelDict #print P.pathDict #print pkgPermissionDict #print pkgUsesPermissionDict #print pkgProtectionLevelDict #pkgPermissionDict = sorted(pkgPermissionDict.items(), key=lambda e:e[0], reverse=False) for root,dirs,files in os.walk(P.ManifestListPath): for filespath in files: jrdfilepath = os.path.join(root,filespath) apkNameFromFileName=filespath[:filespath.find('.',filespath.find('.')+1)] #print apkNameFromFileName pkg = Package() manifestStr = P.getNodeByTag('manifest', jrdfilepath) if P.renamePkgDict.has_key(apkNameFromFileName): name=P.renamePkgDict[apkNameFromFileName] else: name=P.getAttrValueByAttrTitle('package', manifestStr) #name = P.renamePkgDict.has_key(apkNameFromFileName)?P.renamePkgDict[apkNameFromFileName]:P.getAttrValueByAttrTitle('package', manifestStr) shareUserId = P.getAttrValueByAttrTitle('android:sharedUserId', manifestStr).strip(' ') pkg.name = name splitedName='' if shareUserId == '': pkg.packageUID = 'system assigned' else: pkg.packageUID = shareUserId if P.pathDict.has_key(name): tmpStr = P.pathDict[name] idx = tmpStr.rfind('/') pkg.location = tmpStr[:idx] pkg.apkname = tmpStr[idx+1:] else: #modify for googleDrive.apk and soft link. print "BundledPackages error:"+name+" not found,retry once after split the package name." splitedName=name[:name.rfind('.')] if P.pathDict.has_key(splitedName): tmpStr = P.pathDict[splitedName] idx = tmpStr.rfind('/') pkg.location = tmpStr[:idx] pkg.apkname = tmpStr[idx+1:] pkg.name=splitedName print "BundledPackages info:"+name+" has been repaired->"+splitedName else: print "BundledPackages fatal error:"+name+" still not found,skip." continue if pkgSourceDict.has_key(name): pkg.source = pkgSourceDict[name] else: if pkgSourceDict.has_key(splitedName): pkg.source=pkgSourceDict[splitedName] if pkgPermissionDict.has_key(filespath): for per in pkgPermissionDict[filespath]: permission = Permission() permission.name = per if pkgProtectionLevelDict[filespath].has_key(per): permission.protectionLevel = checkProtectionLevelValue(pkgProtectionLevelDict[filespath][per]) else: permission.protectionLevel = 'Not Found' pkg.permission.append(permission) if pkgUsesPermissionDict.has_key(filespath): for per in pkgUsesPermissionDict[filespath]: permission = Permission() permission.name = per tmpProtectionLevel = '' if pkgProtectionLevelDict.has_key(filespath): if pkgProtectionLevelDict[filespath].has_key(per): #permission.protectionLevel = checkProtectionLevelValue() tmpProtectionLevel = pkgProtectionLevelDict[filespath][per] elif P.protectionLevelDict.has_key(per): #permission.protectionLevel = checkProtectionLevelValue(P.protectionLevelDict[per]) tmpProtectionLevel = P.protectionLevelDict[per] #print '@@@ ' + tmpProtectionLevel else: tmpProtectionLevel = 'Not Found' elif P.protectionLevelDict.has_key(per): #permission.protectionLevel = checkProtectionLevelValue(P.protectionLevelDict[per]) tmpProtectionLevel = P.protectionLevelDict[per] #print '@@@ ' + tmpProtectionLevel else: tmpProtectionLevel = 'Not Found' permission.protectionLevel = checkProtectionLevelValue(tmpProtectionLevel) pkg.usesPermission.append(permission) outList.append(pkg) return outList