def deoat(self): """ De-oat the OTA package. """ if self.mBootOAT == None: Log.i( TAG, "deoat(): boot.oat not found in %s, nothing need deoat" % self.mRoot) return self if os.path.exists(self.mBootClassFolder): Log.d(TAG, "Delete the already exists %s" % self.mBootClassFolder) shutil.rmtree(self.mBootClassFolder) # Phase 1: de-oat boot.oat OatZip.deoatBootOAT(self.mBootOAT) # Phase 2: de-oat all the other oat files, of which suffix is odex. # [Android 5.0]: All the oat jars are located in the same folder with boot.oat OatZip.deoatFrw(self.mBootOATDir) # Phase 3: de-oat app OatZip.deoatApp(self.mFrwDir, self.mBootClassFolder) OatZip.deoatApp(self.mAppDir, self.mBootClassFolder) OatZip.deoatApp(self.mPrivAppDir, self.mBootClassFolder) return self
def deoat(self): """ De-oat the OTA package. """ if self.mBootOAT == None: Log.i(TAG, "deoat(): boot.oat not found in %s, nothing need deoat" % self.mRoot) return self if os.path.exists(self.mBootClassFolder): Log.d(TAG, "Delete the already exists %s" %self.mBootClassFolder) shutil.rmtree(self.mBootClassFolder) # Phase 1: de-oat boot.oat OatZip.deoatBootOAT(self.mBootOAT) # Phase 2: de-oat all the other oat files, of which suffix is odex. # [Android 5.0]: All the oat jars are located in the same folder with boot.oat OatZip.deoatFrw(self.mBootOATDir) # Phase 3: de-oat app OatZip.deoatApp(self.mFrwDir, self.mBootClassFolder) OatZip.deoatApp(self.mAppDir, self.mBootClassFolder) OatZip.deoatApp(self.mPrivAppDir, self.mBootClassFolder) return self
def repackageFrw(frwDir, bootClassFolder): """ Repackage the classes.dex into jar of frwDir. """ if OPTIONS.formatFrw == False : return # Keep the old directory, we will change back after some operations. oldDir = os.path.abspath(os.curdir) # Some dexFiles are parted, such as framework-classes2.dex regex = re.compile("(.*)-(classes\d?).dex") Log.i(TAG, "Repackage JARs of %s" %(frwDir)) os.chdir(frwDir) for dexFile in os.listdir(bootClassFolder): if dexFile.endswith(".dex"): jarFile = dexFile[0:-4] + ".jar" dexName = "classes.dex" if not os.path.exists(jarFile): # Match out the jar file with regex matcher = regex.match(dexFile) if matcher != None: jarFile = matcher.group(1) + ".jar" dexName = matcher.group(2) + ".dex" Log.d(TAG, "Repackage %s" %(jarFile)) # Put the dex and framework's jar in the same folder, and jar into the jarFile shutil.move(os.path.join(bootClassFolder, dexFile), os.path.join(frwDir, dexName)) Utils.runWithOutput(["jar", "uf", jarFile, dexName]) if os.path.exists(dexName): os.remove(dexName) os.chdir(oldDir)
def deoatApp(oatApkDir, bootClassFolder): """ De-oat app """ if OPTIONS.formatApp == False: return Log.i(TAG, "De-oat files of oat-format in %s, with BOOTCLASSFOLDER=%s" %(oatApkDir, bootClassFolder)) for (dirpath, dirnames, filenames) in os.walk(oatApkDir): dirnames = dirnames # no use, to avoid warning for filename in filenames: if filename.endswith(".odex"): # no need to de-oat if original apk does not exist apkFile = filename[0:-5] + ".apk" apkPath = os.path.dirname(dirpath) if not os.path.exists(os.path.join(apkPath, apkFile)): continue oatApk = os.path.join(dirpath, filename) deoatApk = oatApk[0:-5] + ".dex" if os.path.exists(deoatApk): Log.d(TAG, "Delete the already exists %s" % deoatApk) os.remove(deoatApk) Utils.runWithOutput([OatZip.OAT2DEX, oatApk, bootClassFolder])
def deoatAppWithArch(appsDir, frwDir, arch, arch2): """ De-oat app """ if OPTIONS.formatApp == False: return Log.i(TAG, "De-oat files of oat-format in %s" % (appsDir)) bootClassFolderArch = os.path.join(frwDir, arch, "odex") bootClassFolderArch2 = os.path.join(frwDir, arch2, "odex") #for app in $( ls $appdir ); do for app in os.listdir(appsDir): appPath = os.path.join(appsDir, app) apkFile = os.path.join(appPath, app + ".apk") archPath = os.path.join(appPath, "oat", arch) #if [[ -d "$appdir/$app/$arch" ]]; if os.path.exists(archPath): odexFile = os.path.join(archPath, app + ".odex") #java -Xmx512m -jar $oat2dex $appdir/$app/$arch/$app.odex $framedir/$arch/odex Utils.runWithOutput( [OatZip.OAT2DEX, odexFile, bootClassFolderArch]) else: # if exists arch2 if arch2.strip(): arch2Path = os.path.join(appPath, "oat", arch2) if os.path.exists(arch2Path): odexFile2 = os.path.join(arch2Path, app + ".odex") Utils.runWithOutput( [OatZip.OAT2DEX, odexFile2, bootClassFolderArch2])
def deoatApp(oatApkDir, bootClassFolder): """ De-oat app """ if OPTIONS.formatApp == False: return Log.i( TAG, "De-oat files of oat-format in %s, with BOOTCLASSFOLDER=%s" % (oatApkDir, bootClassFolder)) for (dirpath, dirnames, filenames) in os.walk(oatApkDir): dirnames = dirnames # no use, to avoid warning for filename in filenames: if filename.endswith(".odex"): # no need to de-oat if original apk does not exist apkFile = filename[0:-5] + ".apk" apkPath = os.path.dirname(dirpath) if not os.path.exists(os.path.join(apkPath, apkFile)): continue oatApk = os.path.join(dirpath, filename) deoatApk = oatApk[0:-5] + ".dex" if os.path.exists(deoatApk): Log.d(TAG, "Delete the already exists %s" % deoatApk) os.remove(deoatApk) Utils.runWithOutput( [OatZip.OAT2DEX, oatApk, bootClassFolder])
def runUserScriptEnv(self, uscript): from SocketWrap import SocketWrap device = self value = str(self.lastValue) Device.uscriptsOutputPrint[uscript] = "" path = common.config['HubServer']['USCRIPTS_PATH'] + uscript Log.i('Device/RunUScript', "Reading Users Script at: " + path) try: f = open(path, "r") except EnvironmentError: DB.addAlert('', '', 1005, 'Path: ' + path) return scriptCont = f.read() scriptCont = scriptCont.replace( "print(", "Device.logUserPrint('" + uscript + "',") scriptCont = scriptCont.replace( "print (", "Device.logUserPrint('" + uscript + "',") scriptCont = scriptCont.replace( "print\t(", "Device.logUserPrint('" + uscript + "',") f.close() #print(scriptCont) try: exec(scriptCont, locals(), globals()) res = Device.uscriptsOutputPrint[uscript] except Exception as e: exc = str(e) Log.e('Device/RunUScript', "Executing scripts failed with error " + exc) DB.addAlert('', '', 1007, uscript + ":\n" + exc + "\n" + traceback.format_exc()) res = exc Log.i('Device/RunUScript', "Executed: " + res) #del Task.uscriptsOutputPrint[uscript] return res
def execute(self): Log.i('Tasker',"Executing task %i NOW" % self.id) threading.Thread(target = Task.runUserScriptEnv, args=(self.script,)).start() #self.runUserScriptEnv(self.script) DB.registerEvent('','','Tasker','Executing task #%i' % (self.id ) ) now = datetime.now() self.lastTimeRun = datetime.strftime(now, "%Y.%m.%d %H:%M:%S") self.updateNextTime()
def checkVPN(): res = str(check_output("ifconfig".split(" ") )) Log.i("PortalConnect","Checking for VPN res:",res) if "ppp" not in res: res = os.system("sudo pon main updetach persist") print(res) if res != 0: Log.e("VPN Setup","Error connecting to VPN server(",res)
def heartbeatRes(self, msg): Log.i('Device', "Received heartbeat.res for device", self.unid, self.setid, "sendAttempt:", msg.sendAttempt) if (msg.sendAttempt < 0 and self.isOnline == True): self.setIsOnline(False) elif (self.isOnline == False): #else: self.setIsOnline(True)
def testArch(frwDir, arch): """ Test whether arch exists """ bootOATPath = os.path.join(frwDir, arch, "boot.oat") Log.i(TAG, "testArch : " + bootOATPath) if os.path.exists(bootOATPath): return True return False
def deoatBootOAT(frwDir, arch): """ De-oat boot.oat """ bootClassFolder = os.path.join(frwDir, arch) bootOatPath = os.path.join(bootClassFolder, "boot.oat") bootClassFolderDex = os.path.join(frwDir, arch + "-dex") bootClassFolderOdex = os.path.join(frwDir, arch + "-odex") if os.path.exists(bootClassFolderDex): Log.d(TAG, "Delete the already exists %s" % bootClassFolderDex) shutil.rmtree(bootClassFolderDex) if os.path.exists(bootClassFolderOdex): Log.d(TAG, "Delete the already exists %s" % bootClassFolderOdex) shutil.rmtree(bootClassFolderOdex) Log.i(TAG, "De-oat %s" % bootClassFolder) if OPTIONS.use_baksmali: for item in os.listdir(bootClassFolder): if item.endswith(".oat"): oatPath = os.path.join(bootClassFolder, item) dexlist = Utils.runWithResult( [OatZip.BAKSMALI, "list", "dex", oatPath]) jarName = os.path.basename(dexlist[0].strip("\n")) jarOutDir = os.path.join(frwDir, arch + "-odex", jarName + ".out") dexDir = os.path.join(frwDir, arch + "-dex") if not os.path.exists(dexDir): os.makedirs(dexDir) for dex in dexlist: if dex.strip("\n").endswith(".jar"): dexName = "" smaliDir = "smali" dexSaveName = jarName[0:-4] + ".dex" else: dexName = dex.strip("\n").split(":")[1] smaliDir = "smali_" + dexName[0:-4] dexSaveName = jarName[0:-4] + "-" + dexName Log.d( TAG, "baksmali deodex -b %s %s -o %s" % (bootOatPath, os.path.join(oatPath, dexName), os.path.join(jarOutDir, smaliDir))) Utils.runWithOutput([ OatZip.BAKSMALI, "deodex", "-b", bootOatPath, os.path.join(oatPath, dexName), "-o", os.path.join(jarOutDir, smaliDir) ]) Log.d( TAG, "smali assemble %s -o %s" % (os.path.join(jarOutDir, smaliDir), os.path.join(dexDir, dexSaveName))) Utils.runWithOutput([ OatZip.SMALI, "assemble", os.path.join(jarOutDir, smaliDir), "-o", os.path.join(dexDir, dexSaveName) ]) else: Utils.runWithOutput([OatZip.OAT2DEX, "boot", bootClassFolder])
def onValueSetACK(msg, device): Log.i('API/OnValueACK',msg.toJSON()) if (API.socketAwaitACK != None): if(msg.cmd == "device.val.set.scheduled"): API.socketAwaitACK.send(json.dumps({"errcode": "0", "data": {"msg":"scheduled"}}).encode()) elif(msg.sendAttempt < 0): API.socketAwaitACK.send(json.dumps({"errcode": "2004","msg":"Too many attempts where made", "data": {}}).encode()) elif(msg.cmd == "ACK" or msg.cmd == "0"): API.socketAwaitACK.send(json.dumps({"errcode": "0", "data": {}}).encode()) API.socketAwaitACK = None
def deodexOneFile(odexFile, deodexFile): """ De-odex one file. """ if not odexFile.endswith(".odex"): return temp = tempfile.mktemp() # Phase 1: Baksmali the odex file cmd = [ "baksmali", "-x", odexFile, "-d", "framework", "-I", "-o", os.path.join(temp, "out") ] if OPTIONS.apiLevel != None: cmd.extend(["-a", OPTIONS.apiLevel]) if OPTIONS.classpath != None: cmd.extend(["-c", OPTIONS.classpath]) cmd = " ".join(cmd) Log.d(TAG, cmd) Log.d(TAG, commands.getoutput(cmd)) # Phase 2: Smali the files into dex oldDir = os.path.abspath(os.curdir) os.chdir(temp) cmd = "smali out/ -o classes.dex" Log.d(TAG, commands.getoutput(cmd)) #Utils.runWithOutput(["smali", "out", "-o", "classes.dex"]) # Phase 3: Package if os.path.exists(deodexFile): #cmd = ["jar", "uf", deodexFile, "classes.dex"] cmd = "jar uf %s classes.dex" % deodexFile else: #cmd = ["jar", "cf", deodexFile, "classes.dex"] cmd = "jar cf %s classes.dex" % deodexFile Log.d(TAG, commands.getoutput(cmd)) #Utils.runWithOutput(cmd) os.chdir(oldDir) if os.path.exists(odexFile): os.remove(odexFile) Log.i(TAG, "Delete %s" % temp) shutil.rmtree(temp) # Phase 4: zipalign #Utils.runWithOutput(["zipalign", "4", deodexFile, deodexFile+".aligned"]) #Utils.runWithOutput(["mv", deodexFile+".aligned", deodexFile]) cmd = "zipalign 4 %s %s" % (deodexFile, deodexFile + ".aligned") Log.d(TAG, commands.getoutput(cmd)) cmd = "mv %s %s" % (deodexFile + ".aligned", deodexFile) Log.d(TAG, cmd)
def rebuild(self): """ Rebuild the deoated zip """ if self.mBootOAT == None: Log.i(TAG, "rebuild(): boot.oat not found, nothing need rebuild") return # repackage app OatZip.repackageAppWithArch(self.mAppDir, self.arch) if self.arch2.strip(): OatZip.repackageAppWithArch(self.mAppDir, self.arch2) OatZip.repackageAppWithArch(self.mPrivAppDir, self.arch) if self.arch2.strip(): OatZip.repackageAppWithArch(self.mPrivAppDir, self.arch2) # repackage framework #$framedir/$arch #OatZip.repackageFrwWithArch(self.mFrwDir, os.path.join(self.mFrwDir, self.arch)) #$framedir/$arch/dex if os.path.exists(os.path.join(self.mFrwDir, self.arch + "-dex")): OatZip.repackageFrwWithArch( self.mFrwDir, os.path.join(self.mFrwDir, self.arch + "-dex")) #$framedir/oat/$arch if os.path.exists(os.path.join(self.mFrwDir, "oat", self.arch)): OatZip.repackageFrwWithArch( self.mFrwDir, os.path.join(self.mFrwDir, "oat", self.arch)) # deal with additional apks not in system/framework system/app system/priv-app OatZip.dealWithAdditionalApks(self.mSystemDir, self.mFrwDir, self.arch, self.arch2, self.mAllAppDirList) # Remove arch and arch2 dir os.chdir(self.mRoot) if os.path.exists(os.path.join(self.mFrwDir, self.arch)): shutil.rmtree(os.path.join(self.mFrwDir, self.arch)) if os.path.exists(os.path.join(self.mFrwDir, self.arch + "-dex")): shutil.rmtree(os.path.join(self.mFrwDir, self.arch + "-dex")) if os.path.exists(os.path.join(self.mFrwDir, self.arch + "-odex")): shutil.rmtree(os.path.join(self.mFrwDir, self.arch + "-odex")) if self.arch2.strip(): if os.path.exists(os.path.join(self.mFrwDir, self.arch2)): shutil.rmtree(os.path.join(self.mFrwDir, self.arch2)) if os.path.exists(os.path.join(self.mFrwDir, self.arch2 + "-dex")): shutil.rmtree(os.path.join(self.mFrwDir, self.arch2 + "-dex")) if os.path.exists(os.path.join(self.mFrwDir, self.arch2 + "-odex")): shutil.rmtree(os.path.join(self.mFrwDir, self.arch2 + "-odex")) if os.path.exists(os.path.join(self.mFrwDir, "oat")): shutil.rmtree(os.path.join(self.mFrwDir, "oat"))
def deoatFrw(oatJarDir): """ De-oat framework """ if not OPTIONS.formatFrw: return Log.i(TAG, "De-oat files of oat-format in %s" % oatJarDir) for item in os.listdir(oatJarDir): if item.endswith(".odex"): # COMMANDS: oat2dex boot <jar-of-oat-format> oatJar = os.path.join(oatJarDir, item) Utils.runWithOutput([OatZip.OAT2DEX, "boot", oatJar])
def repackageFrwWithArch(frwDir, dexFolder): """ Repackage the classes.dex into jar of frwDir. """ if OPTIONS.formatFrw == False: return # Keep the old directory, we will change back after some operations. oldDir = os.path.abspath(os.curdir) Log.i(TAG, "Repackage JARs of %s - %s" % (frwDir, dexFolder)) os.chdir(dexFolder) for dexFile in os.listdir(dexFolder): if dexFile.endswith(".dex") and dexFile.find("classes") == -1: appName = dexFile[0:-4] jarFile = os.path.join(frwDir, appName + ".apk") if not os.path.exists(jarFile): jarFile = jarFile[0:-4] + ".jar" if not os.path.exists(jarFile): dexName = "classes.dex" shutil.move(os.path.join(dexFolder, dexFile), os.path.join(dexFolder, dexName)) Utils.runWithOutput(["jar", "cf", jarFile, dexName]) os.remove(os.path.join(dexFolder, dexName)) continue Log.d(TAG, "Repackage %s" % (jarFile)) if not OatZip.isDeodexed(jarFile): # Put the dex and framework's jar in the same folder, and jar into the jarFile dexName = "classes.dex" shutil.move(os.path.join(dexFolder, dexFile), os.path.join(dexFolder, dexName)) Utils.runWithOutput(["jar", "uf", jarFile, dexName]) os.remove(os.path.join(dexFolder, dexName)) dexName = "classes2.dex" dexFile = appName + "-" + dexName if os.path.exists(os.path.join(dexFolder, dexFile)): shutil.move(os.path.join(dexFolder, dexFile), os.path.join(dexFolder, dexName)) Utils.runWithOutput(["jar", "uf", jarFile, dexName]) os.remove(os.path.join(dexFolder, dexName)) dexName = "classes3.dex" dexFile = appName + "-" + dexName if os.path.exists(os.path.join(dexFolder, dexFile)): shutil.move(os.path.join(dexFolder, dexFile), os.path.join(dexFolder, dexName)) Utils.runWithOutput(["jar", "uf", jarFile, dexName]) os.remove(os.path.join(dexFolder, dexName)) os.chdir(oldDir)
def deodexOneFile(odexFile, deodexFile): """ De-odex one file. """ if not odexFile.endswith(".odex"): return temp = tempfile.mktemp() # Phase 1: Baksmali the odex file cmd = ["baksmali", "-x", odexFile, "-d", "framework", "-I", "-o", os.path.join(temp, "out")] if OPTIONS.apiLevel != None: cmd.extend(["-a", OPTIONS.apiLevel]) if OPTIONS.classpath != None: cmd.extend(["-c", OPTIONS.classpath]) cmd = " ".join(cmd) Log.d(TAG, cmd) Log.d(TAG, commands.getoutput(cmd)) # Phase 2: Smali the files into dex oldDir = os.path.abspath(os.curdir) os.chdir(temp) cmd = "smali out/ -o classes.dex" Log.d(TAG, commands.getoutput(cmd)) #Utils.runWithOutput(["smali", "out", "-o", "classes.dex"]) # Phase 3: Package if os.path.exists(deodexFile): #cmd = ["jar", "uf", deodexFile, "classes.dex"] cmd = "jar uf %s classes.dex" % deodexFile else: #cmd = ["jar", "cf", deodexFile, "classes.dex"] cmd = "jar cf %s classes.dex" % deodexFile Log.d(TAG, commands.getoutput(cmd)) #Utils.runWithOutput(cmd) os.chdir(oldDir) if os.path.exists(odexFile): os.remove(odexFile) Log.i(TAG, "Delete %s" %temp) shutil.rmtree(temp) # Phase 4: zipalign #Utils.runWithOutput(["zipalign", "4", deodexFile, deodexFile+".aligned"]) #Utils.runWithOutput(["mv", deodexFile+".aligned", deodexFile]) cmd = "zipalign 4 %s %s" %(deodexFile, deodexFile+".aligned") Log.d(TAG, commands.getoutput(cmd)) cmd = "mv %s %s" %(deodexFile+".aligned", deodexFile) Log.d(TAG, cmd)
def connect(): connected = False while True: try: if(connected == False): Log.i("PortalConnect","Connecting to PORTAL",PortalConnect.IP, 4046) PortalConnect.checkVPN() server_address = (PortalConnect.IP, 4046 ) PortalConnect.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) PortalConnect.sock.connect(server_address) PortalConnect.sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) PortalConnect.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1) PortalConnect.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, 1) PortalConnect.sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPCNT, 5) Log.i("PortalConnect","Connected to PORTAL") PortalConnect.sock.settimeout(3) data = { 'cmd' : 'register.renew', 'locIP' : PortalConnect.get_lan_ip(), 'webVer' : PortalConnect.config ['PortalConnect']['webVer'], 'hubVer' : PortalConnect.getMaxVer(), 'id' : PortalConnect.config ['PortalConnect']['id'], } #PortalConnect.sock.setblocking(0) connected = True PortalConnect.sock.sendall(json.dumps({"data":data}).encode()) PortalConnect.thread() #data = PortalConnect.sock.recv(1024) #Log.i('PortalConnect', "ReReged: ", data) #PortalConnect.sock.sendall(b"") time.sleep(1) #if not data: #connected = False #break except ConnectionError as e: Log.e('PortalConnect/Connect',"While E: %s"%e) connected = False time.sleep(5) #break except ValueError as e: Log.e('PortalConnect/Connect',"While E: %s"%e) connected = False time.sleep(5) except socket.error as e: #if(e.errno != 11): Log.w('PortalConnect/Connect',"Retrying...\n%s"%e) connected = False time.sleep(5)
def deoatFrwWithArch(frwDir, arch): """ De-oat framework """ if not OPTIONS.formatFrw: return Log.i(TAG, "De-oat files of oat-format in %s" % frwDir) archDir = os.path.join(frwDir, arch) odexDir = os.path.join(archDir, "odex") for item in os.listdir(archDir): if item.endswith(".odex"): jarFile = os.path.join(frwDir, item[0:-5] + ".jar") if not OatZip.isDeodexed(jarFile): odexFile = os.path.join(archDir, item) Utils.runWithOutput([OatZip.OAT2DEX, odexFile, odexDir])
def onValueRecieved(self, value): self.lastValue = value.replace('\n', '').replace(';', '\;') self.lastTime = strftime("%Y.%m.%d %H:%M:%S", localtime()) self.lastValue = self.parseValueToFormat(value) self.updateValueDb(self.lastValue) if (self.isOnline == False): self.setIsOnline(True) #if 'O' in self.direction and hasattr(self, 'tcpConn'): #self.tcpConn.send(self.unid+''+self.setid+''+self.parseFormatToValue(self.lastValue)+'\r\n') Log.i( 'Device', "New value for device " + self.unid + " " + self.setid + " :" + self.lastValue + " at " + self.lastTime) #print("Processing triggers") self.executeTriggerSeq(2)
def rebuild(self): """ Rebuild the deoated zip """ if self.mBootOAT == None: Log.i(TAG, "rebuild(): boot.oat not found, nothing need rebuild") return OatZip.repackageFrw(self.mFrwDir, self.mBootClassFolder) OatZip.repackageApp(self.mFrwDir) OatZip.repackageApp(self.mAppDir) OatZip.repackageApp(self.mPrivAppDir) # Remove the whole OAT directory if os.path.exists(self.mBootOATDir): shutil.rmtree(self.mBootOATDir)
def deoatBootOAT(bootOAT): """ De-oat boot.oat """ bootClassFolder = os.path.dirname(bootOAT) bootClassFolderDex = os.path.join(bootClassFolder, "dex") bootClassFolderOdex = os.path.join(bootClassFolder, "odex") if os.path.exists(bootClassFolderDex): Log.d(TAG, "Delete the already exists %s" % bootClassFolderDex) shutil.rmtree(bootClassFolderDex) if os.path.exists(bootClassFolderOdex): Log.d(TAG, "Delete the already exists %s" % bootClassFolderOdex) shutil.rmtree(bootClassFolderOdex) Log.i(TAG, "De-oat %s" % bootOAT) Utils.runWithOutput([OatZip.OAT2DEX, "boot", bootOAT])
def __init__(self, unzipRoot): self.mRoot = unzipRoot self.mFrwDir = os.path.join(self.mRoot, "system/framework") self.mAppDir = os.path.join(self.mRoot, "system/app") self.mPrivAppDir = os.path.join(self.mRoot, "system/priv-app") self.mAllAppDirList = [self.mFrwDir, self.mAppDir, self.mPrivAppDir] self.mSystemDir = os.path.join(self.mRoot, "system") self.findArch() self.mBootOAT = os.path.join(self.mFrwDir, self.arch, "boot.oat") if os.path.exists(self.mBootOAT): Log.i(TAG, "mBootOAT : " + self.mBootOAT) else: self.mBootOAT = None Log.i(TAG, "boot.oat not found!")
def unzip(self): # Already unziped if self.mRoot is not None: return self.mRoot = tempfile.mkdtemp() Log.i(TAG, "unzip %s to %s" % (self.mInZip, self.mRoot)) cmd = "unzip -q -o %s -d %s" %(self.mInZip, self.mRoot) Log.d(TAG, commands.getoutput(cmd)) self.dedatIfNeeded() # Format path if os.path.exists(os.path.join(self.mRoot, "SYSTEM")): shutil.move(os.path.join(self.mRoot, "SYSTEM"), os.path.join(self.mRoot, "system")) return self
def onValueRecieved(self, unid, setid, value): allId = unid + '' + setid Log.i( 'SocketWrap', "New value at socket for device " + unid + " " + setid + " :" + value) if unid == '' and setid == '': return if allId in SocketWrap.allUnits: device = SocketWrap.allUnits[allId] if len(value) > 0: device.onValueRecieved(value) else: if unid == '0000' and setid == '0000': self.registerDevice(value) else: self.registerDevice(unid, setid)
def create(options): """ Create a zip formatter for the incoming zip file """ zipModel = ZipModel(options.inZip, options.outZip) zipType = zipModel.getZipType() Log.i(TAG, "process(): Creating %s ZipFormatter..." %zipType) if zipType == ZipModel.ART: deoat.OPTIONS = options return Deoat(zipModel) elif zipType == ZipModel.DVM: deodex.OPTIONS = options return Deodex(zipModel) else: raise Exception("Unknown OTA package zip. Is it an ART or DALVIKVM package?")
def deodexFrw(odexJarDir): """ De-odex framework """ if OPTIONS.formatFrw == False: return coreOdex = os.path.join(odexJarDir, "core.odex") if os.path.exists(coreOdex): Log.i(TAG, "De-odex core.odex") deodexFile = os.path.join(odexJarDir, "core.jar") OdexZip.deodexOneFile(coreOdex, deodexFile) Log.i(TAG, "De-odex files of odex-format in %s" % odexJarDir) for item in os.listdir(odexJarDir): if item.endswith(".odex"): odexJar = os.path.join(odexJarDir, item) deodexJar = odexJar[0:-4] + ".jar" OdexZip.deodexOneFile(odexJar, deodexJar) break
def deoat(self): """ De-oat the OTA package. """ if self.mBootOAT == None: Log.i( TAG, "deoat(): boot.oat not found in %s, nothing need deoat" % self.mRoot) return self # Phase 1: de-oat boot.oat OatZip.deoatBootOAT(os.path.join(self.mFrwDir, self.arch, "boot.oat")) if self.arch2.strip(): OatZip.deoatBootOAT( os.path.join(self.mFrwDir, self.arch2, "boot.oat")) # Phase 2: de-oat all the other oat files, of which suffix is odex. # [Android 5.0]: All the oat jars are located in the same folder with boot.oat # Phase 3: de-oat app # de-oat app threadApp = threading.Thread(target=OatZip.deoatAppWithArch, args=(self.mAppDir, self.mFrwDir, self.arch, self.arch2)) threadApp.start() threadPrivApp = threading.Thread(target=OatZip.deoatAppWithArch, args=(self.mPrivAppDir, self.mFrwDir, self.arch, self.arch2)) threadPrivApp.start() threadApp.join() threadPrivApp.join() # Phase 4: de-oat framework # de-oat framework OatZip.deoatFrwWithArch(self.mFrwDir, self.arch) # de-oat framework/oat/$arch OatZip.deoatFrwOatWithArch(self.mFrwDir, self.arch) return self
def getInfoFRDB(self): if self.unid != '' and self.setid != '': Log.i( 'Device', "Obtaining data device from DB with id %s %s" % (self.unid, self.setid)) sql = """ SELECT units_run.lastValue, units_run.id, units_run.lastTime, units_run.needSetValue, units_run.name, units_def.description, units_def.units, units_def.direction, units_def.valueType, units_def.timeout FROM `units_run` LEFT OUTER JOIN units_def ON units_def.unid = units_run.unid WHERE units_run.setid = '%s' AND units_run.unid = '%s' """ % (self.setid, self.unid) data = DB.sqlSelect(sql) #print(sql) if len(data) == 0: Log.e( 'Device', "device with ID %s|%s not FOUND!!" % (self.unid, self.setid)) ##TODO: register as new one DB.addAlert(self.unid, self.setid, 1003) else: data = data[0] self.id = data['id'] self.lastTime = data['lastTime'] self.lastValue = data['lastValue'] self.needSetValue = int(data['needSetValue']) == 1 self.name = data['name'] self.desc = data['description'] self.units = data['units'] self.valueType = data['valueType'] self.direction = data['direction'] self.timeout = data['timeout']
def setValue(self, value, callback=None): from Msg import Msg Log.i( 'Device', "Set value for device" + self.unid + "|" + self.setid + " :" + value + ", isOnline:%s" % self.isOnline) if self.mySocketWrap != None and self.isOnline == True: Log.i('Device', "Sending expl now") self.setValueCallback = callback Log.d('DeviceSetVal', "[1.1]") msg = self.mySocketWrap.sendValue(self, value, self.callbackSetValue) Log.d('DeviceSetVal', "[1.2]") self.needSetValue = False elif (self.mySocketWrap == None and self.isOnline == True): DB.addAlert(self.unid, self.setid, 1008) if (callback != None): msg = Msg(cmd="device.val.set.error") msg.sendAttempt = -1 callback(msg, self) else: Log.i('Device', "Scheduled for when online") msg = Msg(cmd="device.val.set.scheduled") self.executeTriggerSeq(1) if (callback != None): callback(msg, self) self.needSetValue = True self.updateValueDb(value) if (self.isOnline == False): self.sendHeartBeat() #else: # self.setIsOnline(False) self.lastValue = value
def repackageApp(appDir): """ Repackage the classes.dex into apk of appDir """ if OPTIONS.formatApp == False: return # Keep the old directory, we will change back after some operations. oldDir = os.path.abspath(os.curdir) Log.i(TAG, "Repackage APKs of %s" %(appDir)) for (dirpath, dirnames, filenames) in os.walk(appDir): dirnames = dirnames # no use, to avoid warning for dexFile in filenames: if dexFile.endswith(".dex"): apkFile = dexFile[0:-4] + ".apk" apkPath = os.path.dirname(dirpath) if not os.path.exists(os.path.join(apkPath, apkFile)): Log.d(TAG, "No apk matched with %s, Ignore" %dexFile) continue dexName = "classes.dex" Log.d(TAG, "Repackage %s" %(apkPath)) # Put the dex and apk in the same folder, and jar into the apk shutil.move(os.path.join(dirpath, dexFile), os.path.join(apkPath, dexName)) os.chdir(apkPath) Utils.runWithOutput(["jar", "uf", apkFile, dexName]) if os.path.exists(dexName): os.remove(dexName) shutil.rmtree(dirpath) os.chdir(oldDir)
def deoatFrwOatWithArch(frwDir, arch): """ De-oat framework oat """ if not OPTIONS.formatFrw: return Log.i(TAG, "De-oat files of oat-format in %s/oat" % frwDir) archDir = os.path.join(frwDir, arch) #odexDir = os.path.join(archDir, "odex") oatDir = os.path.join(frwDir, "oat", arch) if not os.path.exists(oatDir): return for item in os.listdir(oatDir): if item.endswith(".odex"): jarFile = os.path.join(frwDir, item[0:-5] + ".jar") if not OatZip.isDeodexed(jarFile): odexFile = os.path.join(oatDir, item) if OPTIONS.use_baksmali: bootOatPath = os.path.join(archDir, "boot.oat") jarOutDir = os.path.join(oatDir, item[0:-5] + ".jar.out") dexFile = os.path.join(oatDir, item[0:-5] + ".dex") Log.d( TAG, "baksmali deodex -b %s %s -o %s" % (bootOatPath, odexFile, jarOutDir)) Utils.runWithOutput([ OatZip.BAKSMALI, "deodex", "-b", bootOatPath, odexFile, "-o", jarOutDir ]) Log.d(TAG, "smali assemble %s -o %s" % (jarOutDir, dexFile)) Utils.runWithOutput([ OatZip.SMALI, "assemble", jarOutDir, "-o", dexFile ]) else: Utils.runWithOutput( [OatZip.OAT2DEX, odexFile, archDir])
def repackageApp(appDir): """ Repackage the classes.dex into apk of appDir """ if OPTIONS.formatApp == False: return # Keep the old directory, we will change back after some operations. oldDir = os.path.abspath(os.curdir) Log.i(TAG, "Repackage APKs of %s" % (appDir)) for (dirpath, dirnames, filenames) in os.walk(appDir): dirnames = dirnames # no use, to avoid warning for dexFile in filenames: if dexFile.endswith(".dex"): apkFile = dexFile[0:-4] + ".apk" apkPath = os.path.dirname(dirpath) if not os.path.exists(os.path.join(apkPath, apkFile)): Log.d(TAG, "No apk matched with %s, Ignore" % dexFile) continue dexName = "classes.dex" Log.d(TAG, "Repackage %s" % (apkPath)) # Put the dex and apk in the same folder, and jar into the apk shutil.move(os.path.join(dirpath, dexFile), os.path.join(apkPath, dexName)) os.chdir(apkPath) Utils.runWithOutput(["jar", "uf", apkFile, dexName]) if os.path.exists(dexName): os.remove(dexName) shutil.rmtree(dirpath) os.chdir(oldDir)
def repackageAppWithArch(appDir, arch): """ Repackage the classes.dex into apk of appDir """ if OPTIONS.formatApp == False: return # Keep the old directory, we will change back after some operations. oldDir = os.path.abspath(os.curdir) Log.i(TAG, "Repackage APKs of %s" % (appDir)) for app in os.listdir(appDir): apkPath = os.path.join(appDir, app) apkFile = os.path.join(apkPath, app + ".apk") archPath = os.path.join(apkPath, "oat", arch) dexFile = os.path.join(archPath, app + ".dex") if os.path.exists(archPath): if not OatZip.isDeodexed(apkFile): OatZip.packageDexToAppWithArch(apkFile, arch) #rm -rf $appdir/$app/$arch shutil.rmtree(archPath) os.chdir(oldDir)
def zip(self): if self.mRoot is None: return origDir = os.path.abspath(os.curdir) Log.i(TAG, "zip from %s to %s" % (self.mRoot, self.mOutZip)) os.chdir(self.mRoot) cmd = "zip -r -y -q tmp *; mv tmp.zip %s" % self.mOutZip Log.d(TAG, commands.getoutput(cmd)) os.chdir(origDir) Log.i(TAG, "Deleting %s" % self.mRoot) shutil.rmtree(self.mRoot) Log.i(TAG, "===> %s" % self.mOutZip)
def deoatBootOAT(bootOAT): """ De-oat boot.oat """ Log.i(TAG, "De-oat %s" % bootOAT) Utils.runWithOutput([OatZip.OAT2DEX, "boot", bootOAT])