지난번 iOS Hooking관련 포스팅을 진행하면서 Frida를 소개해드린 바 있습니다.

[링크] http://bachs.tistory.com/entry/iOS-Hooking2Frida?category=892887


전에 언급했던 것처럼 Frida는 iOS뿐만아니라 Android와 Windows에서도 활용할 수 있는데요, 오늘은 Windows에서 활용하는 방법에 대해서 

포스팅을 해보려고 합니다. 예제는 코드엔진의 Basic level03 문제를 이용하였습니다.

[링크] http://codeengn.com/challenges/basic/03


1. 분석

예제 프로그램을 실행하면, 영어인듯 하지만 영어는 아닌것으로 보이는 알럿이 뜹니다.


 

여기서 취소를 누르면 그대로 프로그램을 종료되고, 확인을 누르면 진행이 됩니다.





위의 알럿창에서 뭐라고 이야기했는지 정확히는 모르지만 실행결과를 보니 Regcode를 입력해서 인증을 하는 프로그램인듯 합니다.

더불어 이 예제의 목적은 Regcode를 모르는 상태에서 이 프로그램을 크래킹해내는 것이겠죠.


여기까지를 보고 예상해볼 수 있는 점은 입력 기대값이 있고, 입력값과 비교해서 성공/실패여부를 반환할 것이라는걸 알 수 있습니다.

그리고 이 예제 프로그램이 통신을 하지 않는 것으로 보이니 파일안에 입력 기대값이 존재하거나 로컬에 존재하는 어떤 파일안에 존재할 가능성 등을 

생각해 볼 수 있습니다.  


자세한 건 IDA로 열어서 imports subview를 한번 봅시다.



보다시피 MSVBVM50이라는 라이브러리에서만 참조하는 것으로 보이네요. 검색을 해보면 MSVBVM50.dll은 Visual Basic으로 개발된 프로그램이

참조하는 dll이라는 것을 알 수 있습니다. 또한 목록에서 볼때는 일단 fopen같은 함수가 보이지 않으니, 입력 기대값이 실행 바이너리 안에 존재할 가능성이

더 높아졌네요.


입력값과 입력기대값을 비교하기위해 문자열 비교함수를 사용했을 가능성이 크니 해당 함수를 기점으로 분석 포인트를 잡는 것이 좋을 것같습니다.

import 함수 목록 중 __vbaStrCmp를 포인트로 잡아 보겠습니다.



__vbaStrCmp 함수의 레퍼런스를 찾아보니 두 군데가 나오고 있습니다. 위의 화면은 두 곳 중 더 상위주소에 있는 곳으로 이동한 화면입니다.

__vbaStrCmp 함수를 호출하기전에 ebp-58과 "2G83G35Hs2"라는 문자열을 함수의 인자로 사용하기 위해 push해주고 있는 모습입니다.

이 것을 보고  ebp-58에 우리가 입력한 입력값이 존재하고 "2G83G35Hs2"가 입력 기대값임을 알 수 있습니다.



오, 기대 입력 값은 잘 찾은거 같네요! 하지만 이번에는 Windows후킹에대해 포스팅을 하는 거니까 후킹으로 풀어봅시다.

일단, 우리가 후킹을 걸 메서드는 MSVBVM50.dll 의 __vbaStrCmp()이고, 리턴 값을 조작해주면 될거 같아요.


2. 후킹

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from __future__ import print_function
import frida
import sys
 
def on_message(message, data):
    print("[%s] => %s" % (message, data))
 
def main(target_process):
    session = frida.attach(target_process)
    #print([x.name for x in session.enumerate_modules()])
  
    script = session.create_script("""
    var baseAddr = Module.findBaseAddress('MSVBVM50.dll');
    console.log('MSVBVM50.dll baseAddr: ' + baseAddr);
    //"use strict";
    const __vbaStrCmp = Module.findExportByName("MSVBVM50.dll", "__vbaStrCmp");
    Interceptor.attach(__vbaStrCmp, {
        onEnter: function (args) {
            console.log('===============================================================================');
            console.log('[+] Called __vbaStrCmp!! [' + __vbaStrCmp + ']');
            console.log('[+] args[0] = [' + args[0] + ']');
            dumpAddr('args[0]', args[0], 0x16); 
            console.log('[+] args[1] = [' + args[1] + ']');
            dumpAddr('args[1]', args[1], 0x16);
        },
        // When function is finished
        onLeave: function (retval) {
            console.log('===============================================================================');
            
            /*
                It doesn't work !
            console.log('[+] (Origin) Returned from __vbaStrCmp: ' + typeof(retval));
            console.log('[+] (Origin) Returned from __vbaStrCmp: ' + retval);
            retval = 0;
            console.log('[+] (forgery) Returned from __vbaStrCmp: ' + typeof(retval));
            console.log('[+] (forgery) Returned from __vbaStrCmp: ' + retval);
            */

            this.context.eax = 0x0;
            console.log('Context information:');
            console.log('Context  : ' + JSON.stringify(this.context));
            //console.log('Return   : ' + this.returnAddress);
            //console.log('ThreadId : ' + this.threadId);
            //console.log('Depth    : ' + this.depth);
            //console.log('Errornr  : ' + this.err);
            console.log('===============================================================================');
        }
    });
    // Print out data array, which will contain de/encrypted data as output
    function dumpAddr(info, addr, size) {
        if (addr.isNull())
            return;
        console.log('Data dump ' + info + ' :');
        var buf = Memory.readByteArray(addr, size);
        // If you want color magic, set ansi to true
        console.log(hexdump(buf, { offset: 0, length: size, header: true, ansi: false }));
    }
    function resolveAddress(addr) {
        // Enter the base address of dll as seen in your favorite disassembler (here IDA)
        var idaBase = ptr('0x77EC0000');
        // Calculate offset in memory from base address in IDA database 
        var offset = ptr(addr).sub(idaBase);
        // Add current memory base address to offset of function to monitor 
        var result = baseAddr.add(offset); 
        // Write location of function in memory to console
        console.log('[+] New addr=' + result); 
        return result;
    }
""")
 
    script.on('message', on_message)
    script.load()
    print("[!] Ctrl+D on UNIX, Ctrl+Z on Windows/cmd.exe to detach from instrumented program.\n\n")
    sys.stdin.read()
    session.detach()
    
if __name__ == '__main__':
    if len(sys.argv) != 2:
        print("this script needs pid or proc name :(")
        sys.exit(1)
 
    try:
        target_process = int(sys.argv[1])
    except ValueError:
        target_process = sys.argv[1]
    main(target_process)
cs

Frida공식 홈페이지를 방문하면 기본적인 틀에 대한 설명과 예제 코드를 볼 수 있습니다. 저 역시 아래 링크의 폼에서 수정을 하며 완성을 시켰습니다.

[링크] https://www.frida.re/docs/examples/windows/


2-1 후킹 메서드 주소 찾기

먼저, 해당 __vbaStrCmp 메서드 주소를 찾는 방법입니다.

var baseAddr = Module.findBaseAddress('MSVBVM50.dll');

const __vbaStrCmp = Module.findExportByName("MSVBVM50.dll", "__vbaStrCmp");


위 코드를 실행할 때는 python script_name.py [PID] 로 실행하게 됩니다. 프로세스를 타겟팅해서 attach 하는 것이지요

타겟 프로세스에서 로드되어있는 dll의 주소를 찾기 위해서는 findBaseAddress()를 사용하여 찾을 수 있습니다.


그리고 우리가 후킹을 걸기 위해 필요한 함수의 주소는 findExportByName("dll명", "함수명") 으로 찾을 수 있죠

따라서 const __vbaStrCmp = Module.findExportByName("MSVBVM50.dll", "__vbaStrCmp"); 이 코드가 실행되고 나면 __vbaStrCmp의 주소가

변수에 담기게 됩니다.


프로세스 내부에 존재하는 사용자 정의함수 같은 경우에는 var print_log = resolveAddress('0x0043FC34'); 와 같은 형식으로 얻어와야합니다.

resolveAddress()에 IDA에서 확인한 주소 값(sub_xxxxxxxx)을 인자로 전달해주고, resolveAddress함수 안에 idaBase변수에 IDA에서 사용한 베이스 주소를 입력해준 후 사용해야 합니다.


2-2 onEnter

타겟 함수에 제대로 attach가 되었다면 이제 값을 자유롭게 보고, (권한이 허용되는 한)조작할 수 있습니다.

1
2
3
4
5
6
7
8
9
onEnter: function (args) {
            console.log('===============================================================================');
            console.log('[+] Called __vbaStrCmp!! [' + __vbaStrCmp + ']');
            console.log('[+] args[0] = [' + args[0+ ']');
            dumpAddr('args[0]', args[0], 0x16); 
 
            console.log('[+] args[1] = [' + args[1+ ']');
            dumpAddr('args[1]', args[1], 0x16);
}
cs

onEnter에서는 args변수로 함수 인자 값들을 확인해 볼 수 있습니다. 지금은 두 인자 값이 포인터로 전달이 되기 때문에 hexdump를 떠서 

확인해보고있습니다.


2-3 onLeave

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
onLeave: function (retval) {
    console.log('===============================================================================');
            
    /*
    It doesn't work !
    console.log('[+] (Origin) Returned from __vbaStrCmp: ' + typeof(retval));
    console.log('[+] (Origin) Returned from __vbaStrCmp: ' + retval);
    retval = 0;
    console.log('[+] (forgery) Returned from __vbaStrCmp: ' + typeof(retval));
    console.log('[+] (forgery) Returned from __vbaStrCmp: ' + retval);
    */
    this.context.eax = 0x0;
    console.log('Context information:');
    console.log('Context  : ' + JSON.stringify(this.context));
    //console.log('Return   : ' + this.returnAddress);
    //console.log('ThreadId : ' + this.threadId);
    //console.log('Depth    : ' + this.depth);
    //console.log('Errornr  : ' + this.err);
    console.log('===============================================================================');
}
cs

__vbaStrCmp는 문자열 비교 결과 일치하는 경우 0을 리턴하고 다른경우 -1을 리턴하는 것으로 보입니다.

따라서 1차 적으로는 retval 을 0 으로 만들어 주었었는데요, 확인을 해보니 retval이 바뀌었지만 제대로 동작하지 않는 모습을 보였습니다.

왜인지는 모르겠어요ㅠㅠ


이후에 함수의 리턴값을 저장하는 eax의 값을 확인해보니 0xffffffff로 -1이 저장되고 있는 것을 확인하였고, 

this.context.eax = 0x0; 로 eax값을 0으로 만들어 준 후 정상적으로 크래킹된 것을 볼 수 있었습니다.



끗!

'Study > Windows' 카테고리의 다른 글

CVE-2017-8464 One Day분석 - 1  (0) 2019.03.24

+ Recent posts