def writeObjectToFile(object,filename): file=makeopen(filename) xstream=XStream(DomDriver()) file.write(xstream.toXML(object)) file.close()
def readObjectFromFile(filename): file = open(filename,'r') xstream=XStream(DomDriver()) object=xstream.fromXML(file.read()) file.close() return object
def execute(self, data): if not self.is_jython: print '[!] This module can only be used in jython!' return data # turn data into a Java object bis = io.ByteArrayInputStream(data) ois = io.ObjectInputStream(bis) obj = ois.readObject() # converting Java object to XML structure xs = XStream() xml = xs.toXML(obj) return xml
def execute(self, data): if not self.is_jython: print '[!] This module can only be used in jython!' return data # Creating XStream object and creating Java object from XML structure xs = XStream() serial = xs.fromXML(data) # writing created Java object to and serializing it with ObjectOutputStream bos = io.ByteArrayOutputStream() oos = io.ObjectOutputStream(bos) oos.writeObject(serial) # I had a problem with signed vs. unsigned bytes, hence the & 0xff return "".join([chr(x & 0xff) for x in bos.toByteArray().tolist()])
def deserial(self, data): if not self.is_jython: print '[!] This module can only be used in jython!' return data try: # turn data into a Java object bis = io.ByteArrayInputStream(data) ois = io.ObjectInputStream(bis) obj = ois.readObject() # converting Java object to XML structure xs = XStream() xml = xs.toXML(obj) return xml except Exception, e: print '[!] Caught Exception. Could not convert.\n' return data
def deserializeStream(self): """ Deserialize stream, put the result inside self.output """ bis = io.ByteArrayInputStream(self.input) ois = io.ObjectInputStream(bis) i = 0 while True: # Block raw data ? block_data = [] try: while True: byte = BinaryUtils.tohex(ois.readByte()) #raw_byte = chr(int(byte, 16)) #block_data.append(raw_byte) block_data.append(byte) except: if len(block_data) > 0: #print 'Block data detected - length = {0}:'.format(len(block_data)) self.output.append(('block', block_data)) i += 1 print # Object ? try: obj = ois.readObject() # converting Java object to XML structure xs = XStream() xml = xs.toXML(obj) #self.output += xml + '\n' self.output.append(('object', xml)) i += 1 continue except: pass # if we arrive here, it means there is nothing more break
def serializeData(self): """ Serialize data, put the result inside self.output """ self.output = '\xAC\xED\x00\x05' for type_, data in self.input: print type_ print data print len(data) if type_ == 'object': # Create XStream object and create Java object from the XML structure xs = XStream() serialized = xs.fromXML(data) # Serialize created object with ObjectOutputStream bos = io.ByteArrayOutputStream() oos = io.ObjectOutputStream(bos) oos.writeObject(serialized) self.output += bos.toByteArray()[4:] elif type_ == 'block': # TC_BLOCKDATA = (byte)0x77 if len(data) <= 0xff: self.output += '\x77' self.output += struct.pack('<B', len(data)) # length on 1 byte # TC_BLOCKDATALONG = (byte)0x7A else: self.output += '\x7A' self.output += struct.pack( '>I', len(data)) # length on 4 bytes (big endian) self.output += ''.join( list(map(lambda x: chr(int(x, 16)), data)))