def show(self): return print("StringBlock", hex(self.start), hex(self.header), hex(self.header_size), hex(self.chunkSize), hex(self.stringsOffset), self.m_stringOffsets) for i in _range(0, len(self.m_stringOffsets)): print(i, repr(self.getString(i)))
def __init__(self, raw_buff): self.axml = AXMLParser(raw_buff) self.xmlns = False self.buff = '' while True and self.axml.is_valid(): _type = next(self.axml) # print "tagtype = ", _type if _type == const.START_DOCUMENT: self.buff += '<?xml version="1.0" encoding="utf-8"?>\n' elif _type == const.START_TAG: self.buff += '<' + \ self.getPrefix(self.axml.getPrefix()) + \ self.axml.getName() + '\n' self.buff += self.axml.getXMLNS() for i in _range(0, self.axml.getAttributeCount()): self.buff += "%s%s=\"%s\"\n" % ( self.getPrefix(self.axml.getAttributePrefix(i)), self.axml.getAttributeName(i), self._escape(self.getAttributeValue(i))) self.buff += '>\n' elif _type == const.END_TAG: self.buff += "</%s%s>\n" % (self.getPrefix( self.axml.getPrefix()), self.axml.getName()) elif _type == const.TEXT: self.buff += "%s\n" % self.axml.getText() elif _type == const.END_DOCUMENT: break
def __init__(self, buff): self.start = buff.get_idx() self._cache = {} self.header_size, self.header = self.skipNullPadding(buff) self.chunkSize = unpack('<i', buff.read(4))[0] self.stringCount = unpack('<i', buff.read(4))[0] self.styleOffsetCount = unpack('<i', buff.read(4))[0] self.flags = unpack('<i', buff.read(4))[0] self.m_isUTF8 = ((self.flags & UTF8_FLAG) != 0) self.stringsOffset = unpack('<i', buff.read(4))[0] self.stylesOffset = unpack('<i', buff.read(4))[0] self.m_stringOffsets = [] self.m_styleOffsets = [] self.m_strings = [] self.m_styles = [] for i in _range(0, self.stringCount): self.m_stringOffsets.append(unpack('<i', buff.read(4))[0]) for i in _range(0, self.styleOffsetCount): self.m_styleOffsets.append(unpack('<i', buff.read(4))[0]) size = self.chunkSize - self.stringsOffset if self.stylesOffset != 0: size = self.stylesOffset - self.stringsOffset # FIXME if (size % 4) != 0: warn("ooo") for i in _range(0, size): self.m_strings.append(unpack('=b', buff.read(1))[0]) if self.stylesOffset != 0: size = self.chunkSize - self.stylesOffset # FIXME if (size % 4) != 0: warn("ooo") for i in _range(0, int(size / 4)): self.m_styles.append(unpack('<i', buff.read(4))[0])
def decode2(self, array, offset, length): data = "" for i in _range(0, length): t_data = pack("=b", self.m_strings[offset + i]) data += str(t_data, errors='ignore') return data
def __init__(self, buff, parent=None): self.start = buff.get_idx() self.parent = parent self.id_parent = unpack('<i', buff.read(4))[0] self.count = unpack('<i', buff.read(4))[0] self.items = [] for i in _range(0, self.count): self.items.append((unpack('<i', buff.read(4))[0], ARSCResStringPoolRef(buff, self.parent)))
def __init__(self, buff, parent=None): self.start = buff.get_idx() self.parent = parent self.id = unpack('<b', buff.read(1))[0] self.res0 = unpack('<b', buff.read(1))[0] self.res1 = unpack('<h', buff.read(2))[0] self.entryCount = unpack('<i', buff.read(4))[0] # print "ARSCResTypeSpec", hex(self.start), hex(self.id), # hex(self.res0), hex(self.res1), hex(self.entryCount), "table:" + # self.parent.mTableStrings.getString(self.id - 1) self.typespec_entries = [] for i in _range(0, self.entryCount): self.typespec_entries.append(unpack('<i', buff.read(4))[0])
def decode(self, array, offset, length): length = length * 2 length = length + length % 2 data = "" for i in _range(0, length): t_data = pack("=b", self.m_strings[offset + i]) data += str(t_data, errors='ignore') if data[-2:] == "\x00\x00": break end_zero = data.find("\x00\x00") if end_zero != -1: data = data[:end_zero] return data
def doNext(self): if self.m_event == const.END_DOCUMENT: return event = self.m_event self.reset() while True: chunkType = -1 # Fake END_DOCUMENT event. if event == const.END_TAG: pass # START_DOCUMENT if event == const.START_DOCUMENT: chunkType = const.CHUNK_XML_START_TAG else: if self.buff.end(): self.m_event = const.END_DOCUMENT break chunkType = unpack('<L', self.buff.read(4))[0] if chunkType == const.CHUNK_RESOURCEIDS: chunkSize = unpack('<L', self.buff.read(4))[0] # FIXME if chunkSize < 8 or chunkSize % 4 != 0: warn("Invalid chunk size") for i in _range(0, int(chunkSize / 4 - 2)): self.m_resourceIDs.append( unpack('<L', self.buff.read(4))[0]) continue # FIXME if chunkType < const.CHUNK_XML_FIRST or \ chunkType > const.CHUNK_XML_LAST: warn("invalid chunk type") # Fake START_DOCUMENT event. if chunkType == const.CHUNK_XML_START_TAG and event == -1: self.m_event = const.START_DOCUMENT break self.buff.read(4) # /*chunkSize*/ lineNumber = unpack('<L', self.buff.read(4))[0] self.buff.read(4) # 0xFFFFFFFF if chunkType == const.CHUNK_XML_START_NAMESPACE or \ chunkType == const.CHUNK_XML_END_NAMESPACE: if chunkType == const.CHUNK_XML_START_NAMESPACE: prefix = unpack('<L', self.buff.read(4))[0] uri = unpack('<L', self.buff.read(4))[0] self.m_prefixuri[prefix] = uri self.m_uriprefix[uri] = prefix self.m_prefixuriL.append((prefix, uri)) self.ns = uri else: self.ns = -1 self.buff.read(4) self.buff.read(4) (prefix, uri) = self.m_prefixuriL.pop() # del self.m_prefixuri[ prefix ] # del self.m_uriprefix[ uri ] continue self.m_lineNumber = lineNumber if chunkType == const.CHUNK_XML_START_TAG: self.m_namespaceUri = unpack('<L', self.buff.read(4))[0] self.m_name = unpack('<L', self.buff.read(4))[0] # FIXME self.buff.read(4) # flags attributeCount = unpack('<L', self.buff.read(4))[0] self.m_idAttribute = (attributeCount >> 16) - 1 attributeCount = attributeCount & 0xFFFF self.m_classAttribute = unpack('<L', self.buff.read(4))[0] self.m_styleAttribute = (self.m_classAttribute >> 16) - 1 self.m_classAttribute = (self.m_classAttribute & 0xFFFF) - 1 for i in _range(0, attributeCount * const.ATTRIBUTE_LENGHT): self.m_attributes.append( unpack('<L', self.buff.read(4))[0]) for i in _range( const.ATTRIBUTE_IX_VALUE_TYPE, len(self.m_attributes), const.ATTRIBUTE_LENGHT): self.m_attributes[i] = self.m_attributes[i] >> 24 self.m_event = const.START_TAG break if chunkType == const.CHUNK_XML_END_TAG: self.m_namespaceUri = unpack('<L', self.buff.read(4))[0] self.m_name = unpack('<L', self.buff.read(4))[0] self.m_event = const.END_TAG break if chunkType == const.CHUNK_XML_TEXT: self.m_name = unpack('<L', self.buff.read(4))[0] # FIXME self.buff.read(4) self.buff.read(4) self.m_event = const.TEXT break
def __init__(self, raw_buff): self.analyzed = False self.buff = bytecode.BuffHandle(raw_buff) # print "SIZE", hex(self.buff.size()) self.header = ARSCHeader(self.buff) self.packageCount = unpack('<i', self.buff.read(4))[0] # print hex(self.packageCount) self.stringpool_main = StringBlock(self.buff) self.next_header = ARSCHeader(self.buff) self.packages = {} self.values = {} for i in _range(0, self.packageCount): current_package = ARSCResTablePackage(self.buff) package_name = current_package.get_name() self.packages[package_name] = [] mTableStrings = StringBlock(self.buff) mKeyStrings = StringBlock(self.buff) # self.stringpool_main.show() # self.mTableStrings.show() # self.mKeyStrings.show() self.packages[package_name].append(current_package) self.packages[package_name].append(mTableStrings) self.packages[package_name].append(mKeyStrings) pc = PackageContext(current_package, self.stringpool_main, mTableStrings, mKeyStrings) current = self.buff.get_idx() while not self.buff.end(): header = ARSCHeader(self.buff) self.packages[package_name].append(header) if header.type == const.RES_TABLE_TYPE_SPEC_TYPE: self.packages[package_name].append( ARSCResTypeSpec(self.buff, pc)) elif header.type == const.RES_TABLE_TYPE_TYPE: a_res_type = ARSCResType(self.buff, pc) self.packages[package_name].append(a_res_type) entries = [] for i in _range(0, a_res_type.entryCount): current_package.mResId = \ current_package.mResId & 0xffff0000 | i entries.append((unpack('<i', self.buff.read(4))[0], current_package.mResId)) self.packages[package_name].append(entries) for entry, res_id in entries: if self.buff.end(): break if entry != -1: ate = ARSCResTableEntry(self.buff, res_id, pc) self.packages[package_name].append(ate) elif header.type == const.RES_TABLE_PACKAGE_TYPE: break else: warn("unknown type") break current += header.size self.buff.set_idx(current)