Skip to content

Commit 24dc71f

Browse files
author
linjun
committed
hook win32 messageBoxA ok.
0 parents  commit 24dc71f

File tree

10 files changed

+568
-0
lines changed

10 files changed

+568
-0
lines changed

.idea/MultiWechat.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 298 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MW.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import frida
2+
import sys
3+
from utils.utils import *
4+
class ProcessHooker:
5+
def __init__(self,cmd):
6+
self.pid=frida.spawn(cmd)
7+
self.session=frida.attach(self.pid)
8+
9+
10+
def _process_message(self, message, data):
11+
"""
12+
Frida COMMS
13+
"""
14+
if message['type'] == 'send':
15+
stanza = message['payload']
16+
if stanza['name'] == '+log':
17+
msg=str(stanza["payload"])
18+
logl("["+str(self.pid)+"]\t"+msg)
19+
try:
20+
self.extract.post({ 'type': '+log-ack' })
21+
except Exception as e:
22+
pass
23+
24+
elif stanza['name'] == '+pkill':
25+
logl( "Kill Sub-Process: " + str(stanza['payload']))
26+
27+
else:
28+
logl( "==========ERROR==========")
29+
logl(message)
30+
logl("=========================")
31+
32+
def inject_script(self,jsfile):
33+
# TODO: upgade to use frida-compile
34+
with open(jsfile) as fp:
35+
script_js = fp.read()
36+
self.extract = self.session.create_script(script_js, name="mw.js")
37+
self.extract.on('message', self._process_message)
38+
self.extract.load()
39+
logl("js loaded.")
40+
41+
def go(self):
42+
logl("resume pid:"+str(self.pid))
43+
frida.resume(self.pid)
44+
45+
46+
def main():
47+
cmd=[r"F:\projects\C++\win32\Release\win32.exe"]
48+
jsfile="mw.js"
49+
Hooker=ProcessHooker(cmd)
50+
logl("process spwnded.");
51+
Hooker.inject_script(jsfile)
52+
logl("js injected.");
53+
Hooker.go()
54+
logl("go!");
55+
56+
if __name__ == '__main__':
57+
main()

mw.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
var DEBUG_FLAG = true;
2+
function log(msg)
3+
{
4+
if(DEBUG_FLAG == true){
5+
send({
6+
name: '+log',
7+
payload: msg
8+
});
9+
recv('+log-ack', function () {}).wait();
10+
}
11+
};
12+
13+
14+
var ptrMessageBoxA = Module.findExportByName("user32.dll","MessageBoxA");
15+
var MessageBoxA=new NativeFunction(ptrMessageBoxA,'int',['int','pointer','pointer','int'],'stdcall');
16+
log("ptrMessageBoxA :"+ptrMessageBoxA);
17+
Interceptor.replace(ptrMessageBoxA,new NativeCallback(function (hwnd,pText,pTitle,type) {
18+
strText=Memory.readUtf8String(pText);
19+
strTitle=Memory.readUtf8String(pTitle);
20+
log("MessageBoxA "+strText+" with title "+strTitle);
21+
strHook=Memory.allocUtf8String("hooked!");
22+
return MessageBoxA(hwnd,strHook,pTitle,type);
23+
24+
},'int',['int','pointer','pointer','int'],'stdcall'));
25+

utils/__init__.py

Whitespace-only changes.

utils/__init__.pyc

135 Bytes
Binary file not shown.

utils/utils.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import os
2+
import subprocess
3+
import threading
4+
import time
5+
6+
import chardet
7+
import sys
8+
9+
10+
def appendStrToFile(sstr, filepath):
11+
f = open(filepath, "a+")
12+
try:
13+
sstr = sstr.encode('utf-8')
14+
except Exception, e:
15+
pass
16+
f.write(sstr)
17+
f.close()
18+
19+
20+
def logToFile(msg, filepath):
21+
prefix = "[" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "]"
22+
logtext = prefix + "\t" + msg
23+
appendStrToFile(msg, filepath)
24+
25+
26+
def loglToFile(msg, filepath):
27+
logToFile(msg + "\n", filepath)
28+
29+
30+
def logBase(msg):
31+
sys.stdout.write(msg)
32+
appendStrToFile(msg, r"f:\bciBuild.log")
33+
34+
35+
def log(msg):
36+
prefix = "[" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + "]"
37+
logtext = prefix + "\t" + msg
38+
logBase(logtext)
39+
40+
41+
def loglplus(msg):
42+
log("[+] " + msg + "\n")
43+
44+
45+
def loglminer(msg):
46+
log("[-] " + msg + "\n")
47+
48+
49+
def logldot(msg):
50+
log("[.] " + msg + "\n")
51+
52+
53+
def logldebug(msg):
54+
log("[D] " + msg + "\n")
55+
56+
57+
def logl(msg):
58+
msg=str(msg)
59+
log("[.] " + msg + "\n")
60+
61+
62+
63+
64+
65+
def GetEncoding(data):
66+
chardit1 = chardet.detect(data)
67+
encoding = chardit1['encoding']
68+
# logd("encode:"+encoding)
69+
return encoding
70+
71+
72+
def DoCmd_pexpect(cmd):
73+
logldebug("DoCmd_pexpect:[" + cmd + "]")
74+
# print("spawn..%d")%(threading._get_ident())
75+
# deadlock here sometimes,inside the ReadFile.
76+
spawn = 0 # winpexpect.winspawn("cmd.exe /c "+cmd)
77+
# print("spawn..done")
78+
text = ""
79+
bEmpty = True
80+
while True:
81+
out = spawn.read(16);
82+
if out != "":
83+
bEmpty = False
84+
text += out
85+
86+
# print("empty:%d isalive:%d")%(bEmpty,spawn.isalive())
87+
if bEmpty and not spawn.isalive():
88+
break
89+
bEmpty = True
90+
91+
# print(text)
92+
if text == "":
93+
ret = ""
94+
else:
95+
encoding = GetEncoding(text)
96+
ret = text.decode(encoding)
97+
ret = ret.strip()
98+
logldebug("result_pexpect:[" + ret + "]")
99+
return ret
100+
101+
102+
def DoCmd_OsPopen(cmd):
103+
cmdLogFile = r"d:\bcicmd.log"
104+
msg = "DoCmd_system:[" + cmd + "]"
105+
loglToFile(msg, cmdLogFile)
106+
ret = os.popen("%s 2>&1" % cmd).read()
107+
msg = "result:[" + ret + "]"
108+
loglToFile(msg, cmdLogFile)
109+
return ret
110+
111+
112+
def DoCmd_popen(cmd):
113+
logldebug("DoCmd_popen:[" + cmd + "]")
114+
process = subprocess.Popen("cmd.exe /c " + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
115+
text = ""
116+
bEmpty = True;
117+
while True:
118+
# print("reading... stdout")
119+
out = process.stdout.read(16)
120+
# print("reading... stderr")
121+
122+
# deadlock here sometimes
123+
err = process.stderr.read(16)
124+
# print("reading... done")
125+
126+
if out != '':
127+
text += out
128+
bEmpty = False
129+
# print(out)
130+
if err != '':
131+
text += err
132+
bEmpty = False
133+
# print(err)
134+
if bEmpty and process.poll() != None:
135+
break
136+
bEmpty = True
137+
# print("poll..\n");
138+
# print(text)
139+
if text == "":
140+
ret = ""
141+
else:
142+
encoding = GetEncoding(text)
143+
ret = text.decode(encoding)
144+
ret = ret.strip()
145+
logldebug("result_popen:[" + ret + "]")
146+
return ret
147+
148+
149+
def DoCmd(cmd, use_expect=False):
150+
return DoCmd_OsPopen(cmd)
151+
if not use_expect:
152+
return DoCmd_popen(cmd)
153+
return DoCmd_pexpect(cmd)
154+
155+
156+
# write "data" to file-filename
157+
def writeFile(filename, data):
158+
f = open(filename, "w")
159+
f.write(data)
160+
f.close()
161+
162+
def getFileMd5(filepath):
163+
return DoCmd("md5sum " + filepath + "|grep -oE '[a-zA-Z0-9]{32}'").strip()
164+
pass

utils/utils.pyc

5.01 KB
Binary file not shown.

0 commit comments

Comments
 (0)