All pastes #286314 Raw Edit

Miscellany

public text v1 · immutable
#286314 ·published 2006-12-20 15:52 UTC
rendered paste body
#!/usr/bin/python
import time
import sys
import struct
import binascii

# -----------------------------------------------------------------------------------------
# main decrypt class based on hrose sources (brett19 and bdaan
# author: evill33t
# -----------------------------------------------------------------------------------------
class decrypt():
    # -----------------------------------------------------------------------------------------
    # Convert byte to hex
    # -----------------------------------------------------------------------------------------
    def convert_to_hex(self,byte):
        return hex(ord(byte))[2:].upper().zfill(2)

    # -----------------------------------------------------------------------------------------
    # Build a checksum table from a copy of TRose.exe
    # -----------------------------------------------------------------------------------------
    def _buildchecksum(self):
        # open file and read last 512 bytes
        try:
            trose = open(self._trose,'rb')
            trose.seek(len(trose.read())-0x200) # seek to file end - 512
            trose_content = trose.read()
            trose.close()
        except:
            self.log.write('error','openening '+self._trose)
            raise
        self.log.write('info','got file content')
        key = []
        key_ct = [0x94, 0x20, 0x5F, 0x53, 0x14, 0xF2, 0x61, 0x11, 0x54, 0xFF, 0x72, 0x99, 0x44, 0xF4, 0xA1, 0xAA, 0xBA, 0xC2, 0x54, 0x52, 0x6F, 0x73, 0x65, 0x2E, 0x65, 0x78, 0x65]
        key_len = 0x0f
        for i in xrange(0,key_len+1):
            key.append(key_ct[i] ^ int(self.convert_to_hex(trose_content[0x18A + i]),16))
        self.log.write('debug','key',key)
        multiplier = 1
        checksum_length = 0
        i=int(self.convert_to_hex(trose_content[0x0f]),16)
        while i > 0:
            tmp = (key[i-1]-0x30)*multiplier;
            multiplier *= 0x0a
            checksum_length += tmp
            i -= 1
        csumtable = [0x34, 0x5C, 0x3D, 0x52, 0xCE, 0xF3, 0x12, 0xD4, 0x05, 0x91, 0xEE, 0xFF, 0x49, 0xC2, 0xD2]
        return_list = []
        for i in xrange(0,checksum_length):
            return_list.append(None)
        i = 0
        tmp = 0
        for i in xrange(0,checksum_length):
            return_list[i] = hex(csumtable[tmp] ^ int(self.convert_to_hex(trose_content[0x87 + i]),16))
            if tmp==0x0e: 
                tmp=0
            else:
                tmp +=1
        self.log.write('debug','checksumlen',return_list)
        return return_list

    # -----------------------------------------------------------------------------------------
    # Build a crypttable and cryptstatus from our checksum
    # -----------------------------------------------------------------------------------------
    def _buildcrypttable(self):
        for i in xrange(0,0x008):
            crypttable[i]=0
        for i in xrange(0,0x100):
            crypttable[i+8]=i
        csumpos=0
        modifier=0
        for i in xrange(0,0x40*4):
            curpos = crypttable[i+8]
            modifier = (checksum[csumpos] + curpos + modifier) & 0xff
            crypttable[8+i] = crypttable[modifier+8]
            crypttable[modifier+8] = curpos;
            csumpos=++csumpos%checksumlen

    # -----------------------------------------------------------------------------------------
    # Decrypt a packet
    # -----------------------------------------------------------------------------------------
    def cryptpacket(self):
        pass
    
    def __init__(self,filename,log,debug=0):
        self.log = log
        self.debug = debug
        self._trose = filename
        self.checksum = self._buildchecksum()

    def __del__(self):
        self.log.write('debug','decrypt exit')