0:000> uf IsTheSameDir
*** WARNING: Unable to verify checksum for BWChess.exe
BWChess!IsTheSameDir [d:\testproject\bwchess\bwchess.cpp @ 77]:
77 00401020 55 push ebp
77 00401021 8bec mov ebp,esp
77 00401023 6aff push 0FFFFFFFFh
77 00401025 68d62c4000 push offset BWChess!controlfp+0x20 (00402cd6)
77 0040102a 64a100000000 mov eax,dword ptr fs:[00000000h]
77 00401030 50 push eax
77 00401031 64892500000000 mov dword ptr fs:[0],esp
77 00401038 81ec28020000 sub esp,228h
77 0040103e [B]a138434000[/B] mov eax,dword ptr [BWChess!__security_cookie (00404338)]
77 00401043 33c5 xor eax,ebp
77 00401045 8945ec mov dword ptr [ebp-14h],eax
77 00401048 57 push edi
78 00401049 8b450c mov eax,dword ptr [ebp+0Ch]
78 0040104c 3b0560434000 cmp eax,dword ptr [BWChess!g_hModule (00404360)]
78 00401052 7507 jne BWChess!IsTheSameDir+0x3b (0040105b)
BWChess!IsTheSameDir+0x34 [d:\testproject\bwchess\bwchess.cpp @ 79]:
79 00401054 33c0 xor eax,eax
79 00401056 e90b010000 jmp BWChess!IsTheSameDir+0x146 (00401166)
BWChess!IsTheSameDir+0x3b [d:\testproject\bwchess\bwchess.cpp @ 81]:
81 0040105b 66c785e4fdffff0000 mov word ptr [ebp-21Ch],0
81 00401064 b981000000 mov ecx,81h
81 00401069 33c0 xor eax,eax
81 0040106b 8dbde6fdffff lea edi,[ebp-21Ah]
81 00401071 f3ab rep stos dword ptr es:[edi]
81 00401073 66ab stos word ptr es:[edi]
83 00401075 8d8de4fdffff lea ecx,[ebp-21Ch]
83 0040107b 51 push ecx
83 0040107c 6804010000 push 104h
83 00401081 ff5510 call dword ptr [ebp+10h]
83 00401084 8945f0 mov dword ptr [ebp-10h],eax
85 00401087 8b5508 mov edx,dword ptr [ebp+8]
85 0040108a 52 push edx
85 0040108b 8d8de0fdffff lea ecx,[ebp-220h]
85 00401091 e85a050000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > > (004015f0)
85 00401096 c745fc00000000 mov dword ptr [ebp-4],0
86 0040109d 8d85e4fdffff lea eax,[ebp-21Ch]
86 004010a3 50 push eax
86 004010a4 8d8de0fdffff lea ecx,[ebp-220h]
86 004010aa e8f1050000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::CompareNoCase (004016a0)
86 004010af 85c0 test eax,eax
86 004010b1 7527 jne BWChess!IsTheSameDir+0xba (004010da)
BWChess!IsTheSameDir+0x93 [d:\testproject\bwchess\bwchess.cpp @ 87]:
87 004010b3 c785dcfdffff00000000 mov dword ptr [ebp-224h],0
87 004010bd c745fcffffffff mov dword ptr [ebp-4],0FFFFFFFFh
87 004010c4 8d8de0fdffff lea ecx,[ebp-220h]
87 004010ca e891050000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::~CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > > (00401660)
87 004010cf 8b85dcfdffff mov eax,dword ptr [ebp-224h]
87 004010d5 e98c000000 jmp BWChess!IsTheSameDir+0x146 (00401166)
BWChess!IsTheSameDir+0xba [d:\testproject\bwchess\bwchess.cpp @ 89]:
89 004010da 6a5c push 5Ch
89 004010dc 8d8de0fdffff lea ecx,[ebp-220h]
89 004010e2 e809060000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::ReverseFind (004016f0)
89 004010e7 50 push eax
89 004010e8 8d8dd8fdffff lea ecx,[ebp-228h]
89 004010ee 51 push ecx
89 004010ef 8d8de0fdffff lea ecx,[ebp-220h]
89 004010f5 e866060000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::Left (00401760)
89 004010fa 8985d0fdffff mov dword ptr [ebp-230h],eax
89 00401100 8b95d0fdffff mov edx,dword ptr [ebp-230h]
89 00401106 8995ccfdffff mov dword ptr [ebp-234h],edx
89 0040110c c645fc01 mov byte ptr [ebp-4],1
89 00401110 8b85ccfdffff mov eax,dword ptr [ebp-234h]
89 00401116 50 push eax
89 00401117 8d8de0fdffff lea ecx,[ebp-220h]
89 0040111d e85e050000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::operator= (00401680)
89 00401122 c645fc00 mov byte ptr [ebp-4],0
89 00401126 8d8dd8fdffff lea ecx,[ebp-228h]
89 0040112c e82f050000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::~CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > > (00401660)
91 00401131 8d8de4fdffff lea ecx,[ebp-21Ch]
91 00401137 51 push ecx
91 00401138 8d8de0fdffff lea ecx,[ebp-220h]
91 0040113e e85d050000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::CompareNoCase (004016a0)
91 00401143 f7d8 neg eax
91 00401145 1bc0 sbb eax,eax
91 00401147 40 inc eax
91 00401148 8985d4fdffff mov dword ptr [ebp-22Ch],eax
91 0040114e c745fcffffffff mov dword ptr [ebp-4],0FFFFFFFFh
91 00401155 8d8de0fdffff lea ecx,[ebp-220h]
91 0040115b e800050000 call BWChess!ATL::CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > >::~CStringT<unsigned short,ATL::StrTraitATL<unsigned short,ATL::ChTraitsCRT<unsigned short> > > (00401660)
91 00401160 8b85d4fdffff mov eax,dword ptr [ebp-22Ch]
BWChess!IsTheSameDir+0x146 [d:\testproject\bwchess\bwchess.cpp @ 94]:
94 00401166 8b4df4 mov ecx,dword ptr [ebp-0Ch]
94 00401169 64890d00000000 mov dword ptr fs:[0],ecx
94 00401170 5f pop edi
94 00401171 8b4dec mov ecx,dword ptr [ebp-14h]
94 00401174 33cd xor ecx,ebp
94 [I]00401176[/I] e8[B]63160000[/B] call BWChess!__security_check_cookie (004027de)
94 [B]0040117b[/B] 8be5 mov esp,ebp
94 0040117d 5d pop ebp
94 0040117e c3 ret
0:000> uf searching
BWChess!Searching [d:\testproject\bwchess\bwchess.cpp @ 157]:
157 004011f0 55 push ebp
157 004011f1 8bec mov ebp,esp
157 004011f3 b848120000 mov eax,1248h
157[B] 004011f8[/B] e8[B]03160000[/B] call BWChess!_chkstk (00402800)
157[B] 004011fd[/B] a138434000 mov eax,dword ptr [BWChess!__security_cookie (00404338)]
157 00401202 33c5 xor eax,ebp
157 00401204 8945e4 mov dword ptr [ebp-1Ch],eax
158 00401207 837d0800 cmp dword ptr [ebp+8],0
158 0040120b 7406 je BWChess!Searching+0x23 (00401213)
BWChess!Searching+0x1d [d:\testproject\bwchess\bwchess.cpp @ 158]:
158 0040120d 837d0c00 cmp dword ptr [ebp+0Ch],0
158 00401211 7507 jne BWChess!Searching+0x2a (0040121a)
BWChess!Searching+0x23 [d:\testproject\bwchess\bwchess.cpp @ 159]:
159 00401213 33c0 xor eax,eax
159 00401215 e9c1010000 jmp BWChess!Searching+0x1eb (004013db)
BWChess!Searching+0x2a [d:\testproject\bwchess\bwchess.cpp @ 161]:
161 0040121a 837d1000 cmp dword ptr [ebp+10h],0
161 0040121e 7507 jne BWChess!Searching+0x37 (00401227)
BWChess!Searching+0x30 [d:\testproject\bwchess\bwchess.cpp @ 162]:
162 00401220 33c0 xor eax,eax
162 00401222 e9b4010000 jmp BWChess!Searching+0x1eb (004013db)
BWChess!Searching+0x37 [d:\testproject\bwchess\bwchess.cpp @ 165]:
165 00401227 c745ec00000000 mov dword ptr [ebp-14h],0
167 0040122e 683c010000 push 13Ch
167 00401233 8b4510 mov eax,dword ptr [ebp+10h]
167 00401236 50 push eax
167 00401237 ff5508 call dword ptr [ebp+8]
167 0040123a 83c408 add esp,8
167 0040123d 8945f0 mov dword ptr [ebp-10h],eax
168 00401240 687a010000 push 17Ah
168 00401245 8b4d10 mov ecx,dword ptr [ebp+10h]
168 00401248 51 push ecx
168 00401249 ff5508 call dword ptr [ebp+8]
168 0040124c 83c408 add esp,8
168 0040124f 8945e8 mov dword ptr [ebp-18h],eax
169 00401252 6848020000 push 248h
169 00401257 8b5510 mov edx,dword ptr [ebp+10h]
169 0040125a 52 push edx
169 0040125b ff5508 call dword ptr [ebp+8]
169 0040125e 83c408 add esp,8
169 00401261 8985ccefffff mov dword ptr [ebp-1034h],eax
170 00401267 683b010000 push 13Bh
170 0040126c 8b4510 mov eax,dword ptr [ebp+10h]
170 0040126f 50 push eax
170 00401270 ff5508 call dword ptr [ebp+8]
170 00401273 83c408 add esp,8
170 00401276 8985c4efffff mov dword ptr [ebp-103Ch],eax
171 0040127c 66c745d05000 mov word ptr [ebp-30h],50h
171 00401282 66c745d25300 mov word ptr [ebp-2Eh],53h
171 00401288 66c745d44100 mov word ptr [ebp-2Ch],41h
171 0040128e 66c745d65000 mov word ptr [ebp-2Ah],50h
171 00401294 66c745d84900 mov word ptr [ebp-28h],49h
171 0040129a 66c745da2e00 mov word ptr [ebp-26h],2Eh
171 004012a0 66c745dc4400 mov word ptr [ebp-24h],44h
171 004012a6 66c745de4c00 mov word ptr [ebp-22h],4Ch
171 004012ac 66c745e04c00 mov word ptr [ebp-20h],4Ch
171 004012b2 66c745e20000 mov word ptr [ebp-1Eh],0
172 004012b8 8d4dd0 lea ecx,[ebp-30h]
172 004012bb 51 push ecx
172 004012bc ff95ccefffff call dword ptr [ebp-1034h]
172 004012c2 8945f4 mov dword ptr [ebp-0Ch],eax
174 004012c5 837df400 cmp dword ptr [ebp-0Ch],0
174 004012c9 7507 jne BWChess!Searching+0xe2 (004012d2)
BWChess!Searching+0xdb [d:\testproject\bwchess\bwchess.cpp @ 175]:
175 004012cb 33c0 xor eax,eax
175 004012cd e909010000 jmp BWChess!Searching+0x1eb (004013db)
BWChess!Searching+0xe2 [d:\testproject\bwchess\bwchess.cpp @ 177]:
177 004012d2 6a05 push 5
177 004012d4 8b55f4 mov edx,dword ptr [ebp-0Ch]
177 004012d7 52 push edx
177 004012d8 ff5508 call dword ptr [ebp+8]
177 004012db 83c408 add esp,8
177 004012de 8945f8 mov dword ptr [ebp-8],eax
179 004012e1 6a10 push 10h
179 004012e3 8b45f4 mov eax,dword ptr [ebp-0Ch]
179 004012e6 50 push eax
179 004012e7 ff5508 call dword ptr [ebp+8]
179 004012ea 83c408 add esp,8
179 004012ed 8985c8efffff mov dword ptr [ebp-1038h],eax
180 004012f3 c745fc00000000 mov dword ptr [ebp-4],0
181 004012fa 8d4dec lea ecx,[ebp-14h]
181 004012fd 51 push ecx
181 004012fe 6800100000 push 1000h
181 00401303 8d95d0efffff lea edx,[ebp-1030h]
181 00401309 52 push edx
181 0040130a ff55f0 call dword ptr [ebp-10h]
181 0040130d 50 push eax
181 0040130e ff55f8 call dword ptr [ebp-8]
181 00401311 85c0 test eax,eax
181 00401313 0f84bd000000 je BWChess!Searching+0x1e6 (004013d6)
BWChess!Searching+0x129 [d:\testproject\bwchess\bwchess.cpp @ 183]:
183 00401319 c785c0efffff00000000 mov dword ptr [ebp-1040h],0
183 00401323 eb0f jmp BWChess!Searching+0x144 (00401334)
BWChess!Searching+0x135 [d:\testproject\bwchess\bwchess.cpp @ 183]:
183 00401325 8b85c0efffff mov eax,dword ptr [ebp-1040h]
183 0040132b 83c001 add eax,1
183 0040132e 8985c0efffff mov dword ptr [ebp-1040h],eax
BWChess!Searching+0x144 [d:\testproject\bwchess\bwchess.cpp @ 183]:
183 00401334 8b4dec mov ecx,dword ptr [ebp-14h]
183 00401337 c1e902 shr ecx,2
183 0040133a 398dc0efffff cmp dword ptr [ebp-1040h],ecx
183 00401340 0f8390000000 jae BWChess!Searching+0x1e6 (004013d6)
BWChess!Searching+0x156 [d:\testproject\bwchess\bwchess.cpp @ 188]:
188 00401346 6808020000 push 208h
188 0040134b 8d95b8edffff lea edx,[ebp-1248h]
188 00401351 52 push edx
188 00401352 8b85c0efffff mov eax,dword ptr [ebp-1040h]
188 00401358 8b8c85d0efffff mov ecx,dword ptr [ebp+eax*4-1030h]
188 0040135f 51 push ecx
188 00401360 ff55f0 call dword ptr [ebp-10h]
188 00401363 50 push eax
188 00401364 ff95c8efffff call dword ptr [ebp-1038h]
188 0040136a 85c0 test eax,eax
188 0040136c 7463 je BWChess!Searching+0x1e1 (004013d1)
BWChess!Searching+0x17e [d:\testproject\bwchess\bwchess.cpp @ 191]:
191 0040136e 8b95c4efffff mov edx,dword ptr [ebp-103Ch]
191 00401374 52 push edx
191 00401375 8b85c0efffff mov eax,dword ptr [ebp-1040h]
191 0040137b 8b8c85d0efffff mov ecx,dword ptr [ebp+eax*4-1030h]
191 00401382 51 push ecx
191 00401383 8d95b8edffff lea edx,[ebp-1248h]
191 00401389 52 push edx
191 0040138a ff5520 call dword ptr [ebp+20h]
191 0040138d 83c40c add esp,0Ch
191 00401390 85c0 test eax,eax
191 00401392 743d je BWChess!Searching+0x1e1 (004013d1)
BWChess!Searching+0x1a4 [d:\testproject\bwchess\bwchess.cpp @ 192]:
192 00401394 8b4514 mov eax,dword ptr [ebp+14h]
192 00401397 8b08 mov ecx,dword ptr [eax]
192 00401399 8b551c mov edx,dword ptr [ebp+1Ch]
192 0040139c 8d048a lea eax,[edx+ecx*4]
192 0040139f 50 push eax
192 004013a0 8b4d14 mov ecx,dword ptr [ebp+14h]
192 004013a3 8b11 mov edx,dword ptr [ecx]
192 004013a5 8b4518 mov eax,dword ptr [ebp+18h]
192 004013a8 8d0c90 lea ecx,[eax+edx*4]
192 004013ab 51 push ecx
192 004013ac 8b95c0efffff mov edx,dword ptr [ebp-1040h]
192 004013b2 8b8495d0efffff mov eax,dword ptr [ebp+edx*4-1030h]
192 004013b9 50 push eax
192 004013ba ff550c call dword ptr [ebp+0Ch]
192 004013bd 83c40c add esp,0Ch
192 004013c0 85c0 test eax,eax
192 004013c2 740d je BWChess!Searching+0x1e1 (004013d1)
BWChess!Searching+0x1d4 [d:\testproject\bwchess\bwchess.cpp @ 194]:
194 004013c4 8b4d14 mov ecx,dword ptr [ebp+14h]
194 004013c7 8b11 mov edx,dword ptr [ecx]
194 004013c9 83c201 add edx,1
194 004013cc 8b4514 mov eax,dword ptr [ebp+14h]
194 004013cf 8910 mov dword ptr [eax],edx
BWChess!Searching+0x1e1 [d:\testproject\bwchess\bwchess.cpp @ 197]:
197 004013d1 e94fffffff jmp BWChess!Searching+0x135 (00401325)
BWChess!Searching+0x1e6 [d:\testproject\bwchess\bwchess.cpp @ 201]:
201 004013d6 b801000000 mov eax,1
BWChess!Searching+0x1eb [d:\testproject\bwchess\bwchess.cpp @ 202]:
202 004013db 8b4de4 mov ecx,dword ptr [ebp-1Ch]
202 004013de 33cd xor ecx,ebp
202 [B]004013e0[/B] e8[B]f9130000 [/B] call BWChess!__security_check_cookie (004027de)
202 [B]004013e5[/B] 8be5 mov esp,ebp
202 004013e7 5d pop ebp
202 004013e8 c3 ret
上面是自己写的一段函数的反汇编程序, 主要函数名字叫 Searching(...)
它的参数是kernel32.dll的handle, 然后通过查询pe导出函数表,来获得例如loadlibrary() / getprocaddr() 等函数地址,
函数中会主动家在psapi.dll, 然后同样是查询pe导出表 获得 psapi.dll 里面枚举但前进程加载模块的句柄的函数 EnumProcessModules, 获得所有加载的dll句柄后,就会到我想要做的:
1, Searching是在我的dll(下面就叫mydll.dll) 中,我的dll会有两个导出函数
2, 同样,在和mydll.dll同目录下,也会有很多dll
3, 目的就是想获得mydll.dll以外dlls的两个导出函数地址
结果:
1, c++直接写完,没有问题 能够获得地址
2, 我将上述Searching的机器码放入 char数组 chsch[]中( Searching 中我自己写的函数调用全部是由函数指针实现), 然后调用chsch, 结果crash
2.1 仔细一看,发现vc给我自动加入了几个函数调用:
_chkstk,
__security_cookie 和
__security_check_cookie
2.2 解决尝试1, 首先将上面对应的机器码从数组中删除, 运行 发现正常结束,但是中间的api调用和预期结果不一样导致获取导出函数失败
2.2 解决尝试2, 反汇编
_chkstk, 发现它自身函数很短,且不存在再次调用, 直接将其对应机器码放入char数组chkstk1[], 然后发现另一个函数
IsTheSameDir 也会调用
__security_cookie 和
__security_check_cookie, 这样问题就使要对chsch[]中
157 004011f3 b848120000 mov eax,1248h
157[B] 004011f8[/B] e8[B]03160000[/B] call BWChess!_chkstk (00402800)
157[B] 004011fd[/B] a138434000 mov eax,dword ptr [BWChess!__security_cookie (00404338)]
和
202 [B]004013e0[/B] e8[B]f9130000 [/B] call BWChess!__security_check_cookie (004027de)
对应部分进行重定位, 由于IsTheSameDir 在编译选项固定的情况下生成的机器码固定所以可以根据其函数起始地址加固定的偏移获得
__security_cookie
和 __security_check_cookie 的地址, 然后运算成chsch对应位置的偏移
2.2的结果: 代码正常退出但是我要的结果没出现, chsch在中间执行类似EnumProcessModules 时失败退出。
现在我的问题是:
1,
__security_cookie
和
__security_check_cookie
的作用是什么,他们会导致中间api的结果么?
2, 我计算e8 call 的方法:
157[B] 004011f8[/B] e8[B][U]03160000[/U][/B] call BWChess!_chkstk ([U]00402800[/U])
157[B] [U]004011fd[/U][/B] a138434000 mov eax,dword ptr
00402800 =
004011fd +
00001603
则反过来需要填充
00001603则需要用减法, 如:
void Relocate(void* pfnissame, void* pfnsch)
{
DWORD* phead = (DWORD*)(((BYTE*)pfnissame) + 0x1f);
DWORD* phead2 = (DWORD*)(((BYTE*)pfnsch) + 0x0e);
*phead2 = *phead;
DWORD dwPos = (DWORD)(((BYTE*)pfnissame) + 0x15b);
DWORD* dwVal = (DWORD*)(((BYTE*)pfnissame) + 0x157);
DWORD dwPos2 = (DWORD)(((BYTE*)pfnissame) + 0x1eb + 0xa);
DWORD* dwVal2 = (DWORD*)(((BYTE*)pfnissame) + 0x1eb + 6);
*dwVal2 = dwPos + *dwVal - dwPos2;
}
不知这样算对不对?
3, 最后一个疑惑就是反汇编Searching的时候遇到跳转指令如:
192 004013c2 740d je BWChess!Searching+[B]0x1e1[/B] (004013d1)
等到反汇编chsch时就可能变成了:
740d je BWChess!chsch+[B]0x1e3[/B]
chsch其实就是Searching的完整拷贝,怎么算出来的偏移不一致呢?
其实本来是作的一次活动需要写点儿算法,本来想偷机取巧搞点儿花样,没想到遇到这些问题,搞了许久问题也没解决,希望各位有经验的大侠指点一二
谢谢
[招生]科锐逆向工程师培训(2024年11月15日实地,远程教学同时开班, 第51期)