Read all strings in a. dex file

Source: Internet
Author: User
Tags unpack

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.