자동으로 XOR 키를 찾아 디코딩 해주는 스크립트

XOR로 인코딩 하여 내부적으로 혹은 다른 모듈로 복호화를 진행하는 악성코드들이 발견되고 있습니다.
따라서 XOR로 인코딩 된 파일 확보 시 XOR 키를 찾아서 다시 디코딩 작업을 거쳐야 하는데 이런 과정을 자동화 해주는 스크립트를 작성해 보았습니다.

간단하게 https://jjoon.net/page/?p=149 글에 나왔던 파일에 대해 자동으로 디코딩 해주는 스크립트라 보면 될 거 같습니다.

#!/usr/bin/python
import operator, sys

def find_key(filename):
    file = open(filename, 'rb')
    hex = file.read()
    m = ord(hex[0])
    z = ord(hex[1])

    count = 0
    while 1:
        if operator.xor(m, count) == 77:    # M
            if operator.xor(z, count) == 90:    # Z
                break
        count += 1

    print '[+] Find XOR key : ' + '0x%0X' % count
    return count

def xor(filename, key):
    file = open(filename, 'rb')
    data = file.read()
    newfile = open(filename + '_xor', 'wb')

    for x in data:
        if ord(x) == 0x00 or ord(x) == key:
            newfile.write(chr(ord(x)))
        else:
            newfile.write(chr(operator.xor(ord(x), key)))
    print '[+] Create PE file : ' + filename + '_xor'

def main():
    if len(sys.argv) != 2:
        print 'Usage: xor.py <filename>'
        sys.exit(1)
    print 'ByJJoon XOR Decoder!'
    print '[+] Start'
    filename = sys.argv[1] 
    key = find_key(filename)
    xor(filename, key)
    print '[+] End'

if __name__ == "__main__":
    main()

사용법은 아래와 같습니다..

[byjjoon@ByJJoon xor]$ wget http://www.jiandaonet.com/jcf/k01.exe
--2010-03-23 23:52:29--  http://www.jiandaonet.com/jcf/k01.exe
Resolving www.jiandaonet.com... 124.172.109.19
Connecting to www.jiandaonet.com|124.172.109.19|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 30208 (30K) [application/octet-stream]
Saving to: `k01.exe'

100%[========================================================================================>] 30,208      3.79K/s   in 7.8s    

2010-03-23 23:52:38 (3.79 KB/s) - `k01.exe' saved [30208/30208]

[byjjoon@ByJJoon xor]$ file k01.exe 
k01.exe: data
[byjjoon@ByJJoon xor]$ ./xor.py k01.exe 
ByJJoon XOR Decoder!
[+] Start
[+] Find XOR key : 0xA2
[+] Create PE file : k01.exe_xor
[+] End
[byjjoon@ByJJoon xor]$ file k01.exe_xor 
k01.exe_xor: PE32 executable for MS Windows (GUI) Intel 80386 32-bit
[byjjoon@ByJJoon xor]$

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다