Import Structimport os# defines a function that reads the length of a string def DecUnsignedLEB128 (file): result = Struct.unpack ("I", File.read (4)) [0]# Read the first byte in 4 bytes result = result&0x000000ff File.seek ( -3, 1) #倒退回前面的第三个字节 # cannot be converted directly from 1 bytes to 4 bytes, so take 4 bytes first, then empty 3 bytes if ( Result > 0x7f): Next = Struct.unpack ("I", File.read (4)) [0] next = next&0x000000ff #第一位是个位 fil E.seek ( -3, 1) result = (result&0x7f) | (next&0x7f) <<7 if (Next > 0x7f): Next = Struct.unpack ("I", File.read (4)) [0] Next = next&0x000000ff #加入十位 File.seek ( -3, 1) result = Result | (next&0x7f) <<14 if (Next > 0x7f): Next = Struct.unpack ("I", File.read (4)) [0] Next = next&0x000000ff File.seek ( -3, 1) result = Result | (next&0x7f) <<21 if (Next > 0x7f): Next = Struct.unpack ("I", File.read (4)) [0] Next = next&0x000000FF File.seek ( -3, 1) result = Result | next<<28 #print "Result:", result return Resultdex = open ("Imisstest.dex", ' RB ') #rb的意思是 Read and write in binary filedex.seek (0x38, 0) offset of #string table TMP = Dex.read (8) string_count, String_table_off = struct.un Pack ("II", TMP) # "II" is read separately meaning print ("Size:", String_count, "off:", String_table_off) Dex.seek (string_table_off, 0) Dexstrofflist = []count = 0while (count<string_count): Dexstrofflist.append (Struct.unpack ("I", Dex.read (4)) [0]) # Unpack returns a tuple taking the No. 0 element count+=1dexstrlist = []nonullcount = 0for Stroff in DexStrOffList:dex.seek (stroff, 0) str Len = DecUnsignedLEB128 (dex) if (strlen = = 0): Continue input = Dex.read (strlen) dexstrlist.append (struct.u Npack (str (strlen) + "s", input)) #解析不定长的字符串 Nonullcount+=1outputfile = open ("String.txt", "w") Count = 0print ("string:", S Tring_count) for I in DexStrList:outputfile.write ('%s\n '%i) #将元组中的元素写入文件oUtputfile.close () Dex.close ()
Read all strings in a. dex file