Si vous utilisez OllyDbg non pas pour déboguer vos propres applications pour lesquelles vous avez des informations de débogage, mais pour la rétro-ingénierie ou la recherche tierce, votre activité typique consiste à fouiller dans le code machine du produit de quelqu'un d'autre, à comprendre l'essence des choses qui se passent et à placer un grand nombre d'étiquettes (étiquettes ) et des commentaires tout au long du code, ainsi que des sections de données. Vous explorez systématiquement l'essence des procédures / fonctions et des variables et leur donnez des noms qui reflÚtent votre compréhension de leur objectif.
Et maintenant, aprĂšs quelques heures (voire quelques jours) de travail, quand vous avez compris et signĂ© des centaines, voire des milliers de procĂ©dures et de variables, OllyDbgse fige ou se bloque soudainement, emportant tout votre travail (qui peut inclure non seulement des Ă©tiquettes et des commentaires en grand nombre, mais Ă©galement des points d'arrĂȘt et des modifications placĂ©s aux bons endroits). C'est une perte de temps et un facteur monstrueusement dĂ©motivant, dont on abandonne et pĂąlit.
Cet article explique comment j'ai utilisé OllyDbg dans une telle urgence pour rétroconcevoir rapidement OllyDbg (partiellement), enregistrer les données suspendues par un fil et développer une recette d'action en cas de telles catastrophes.
Puisque vous avez continué à lire l'article, mettons-nous d'accord sur certaines choses dÚs le début:
- OllyDbg 1.xx, OllyDbg 2.xx, , - , 1.xx ( ).
- OllyDbg , , , .. « » â « », , , , OllyDbg. , , â .
- IDA Pro , OllyDbg . Audi « BMW» . â , .
- , . « » . howto- , SSH, , , , ? , « BSOD» â , , . ?
«» , , , , , . :
- (Labels) â OllyDbg , , .
- (Comments) â .
- (Breakpoints) â .
- (Hints) â OllyDbg, , 4 4 , DWORD.
, , , . , , . , , , â - .
, , , , { â } OllyDbg names. .
- NM_LABEL â User-defined label
- NM_EXPORT â Exported (global) name
- NM_IMPORT â Imported name
- NM_LIBRARY â Name extracted from library, object file or debug data
- NM_CONST â User-defined constant (currently not implemented)
- NM_COMMENT â User-defined comment
- NM_LIBCOMM â Automatically generated comment from library or object file
- NM_BREAK â Condition related with breakpoint
- NM_ARG â Arguments decoded by analyser
- NM_ANALYSE â Comment added by analyser
- NM_BREAKEXPR â Expression related with breakpoint
- NM_BREAKEXPL â Explanation related with breakpoint
- NM_ASSUME â Assume function with known arguments
- NM_STRUCT â Code structure decoded by analyzer
- NM_CASE â Case description decoded by analyzer
- NM_PLUGCMD â Plugin commands to execute at breakpoint
- NM_INSPECT â Several last entered inspect expressions
- NM_WATCH â Watch expressions
( .udd), â . , - , , , ( ) . , , UDD-, supercool.dll, foo.exe, bar.exe, supercool.dll.
, , OllyDbg ( ). OllyDbg , .
, UDD-? -, , UDD- , . UDD . «Save everything to UDD». «Update .udd file now» â (EXE/DLL/OCX) . , Modules , - . , UDD, . 20 - , UDD , . -, .
4 :
- â . BSOD . .
- â . , , , , « » « ». ( ?), . . , OllyDbg, , - , .
- . OllyDbg . , OllyDbg.exe, . : , OllyDbg «» 27 . , . , OllyDbg, deadlock - Windows, - ( - ). , Microsoft- Spy++, « » , Spy++ GUI- DLL, . - , GUI-. , , . .
- OllyDbg UDD-, . UDD- , OllyDbg . , . OllyDbg , , â , , UDD-, . , - . , OllyDbg UDD, UDD-. , UDD- , , , , . â DLL. OllyDbg «» UDD-, , UDD-, , UDD-. UDD-.
, , , , , , , (.. â ).
UDD â . , , . , . .
UDD :
- . , git ( ) ( , ). diff , (, custom diff git). , merge â .
- , , . , UDD .
- , . - , , . - ( â , , , , , , ..) - - .
- VCS Git , . : ( WinMain()
), â ( , ). , , . , ( memcpy
, strncat
, qsort
). , , (merge conflict), , . « », .
, - - , , , « », , « », . , , - , , ( , new
).
, - ( ) , , , , .
, , , DAW . : - 5â6 , , , , , . merge, , , â VST- , â - . , , , , git diff
, VST- VST- -.
UDD- - , , OllyDbg, ( ( , ) ) ( ), .
Markup Dumper, markup â , . «», â markup, , , .
OllyDbg, , , . , - . , , - .
@ MODULE OLLYDBG @ VERSION 1.0.10.0 @ BASEADDR 00400000 @ CHECKSUM 17ca77a5 // // Section: .text // LABEL 0040c214 like_ParseStuff CMT 0040c4c5 colon case CMT 0040c521 eq case CMT 0040c575 checking for 1-byte typespec CMT 0040c578 jump if more than 2 byte spec CMT 00463f08 checking arg1 (addr) against 0 CMT 00463f0c EDI = arg2 (type of name) CMT 00463f0f goto error if addr==NULL CMT 00463f11 checking [addr] against 0 CMT 00463f15 goto error if 0 ... LABEL 004642fb Insertname_EPILOGUE LABEL 004a3530 like_memcpy LABEL 004a36d4 like_strlen LABEL 004a38bc parse_keyword_1 LABEL 004a6c2c like_sprintf // // Section: .data // LABEL 004eae14 pNamesBlock LABEL 004eae18 cNameEntriesCur LABEL 004eae1c cNameEntriesMax LABEL 004eae30 pNamesStringPool LABEL 004eae34 cbStringPoolUsed LABEL 004eae38 cbStringPoolTotal LABEL 004eae3c xxxWtfNameStringPoolrelated
. , .
, CMT
, LABEL
â . . , git diff , , â , . LABEL
, CMT
, , , git diff , ( ), - , :
, , , , (git add --patch
).
, ( ) , , , - .
, , DVCS , â1 (BSOD ) â4 ( UDD).
â2 â3 ( ). .
, « », « », «», â SEH-, , , « » ( SEH- EXCEPTION_NONCONTINUABLE
).
OllyDbg ( ) â EXCEPTION_ACCESS_VIOLATION
(0xc0000005
), . , EIP , ESP/EBP , . , ( ) . , - . « » , (GetMessageâTranslateMessageâDispatchMessage), , . OllyDbg GUI-, , , .
, - , . EBP . , ( ) , retn
(EIP) .
, , , â () . EXCEPTION_ACCESS_VIOLATION
, ( ). , , , , , DEP, . , ( DEP ), ( , ) , . , , , EIP , , ( , ). , â .
, , , , Markup Dumper, message loop ( ) .
( ), OllyDbg , ( , ), .
, , , EIP , . , OllyDbg , , ( ). , . . , : , , - .
, - , . , , , , , .
Findname()
Findnextname()
â API-, OllyDbg .
void DumpMarkupEx(FILE * fiOutput, int iNameType, char* pszNameType, ulong vaStart, ulong vaLimit)
{
ulong vaCur;
char rgchName[TEXTLEN];
Findname(vaStart-1, iNameType, NULL);
while(vaCur = Findnextname(rgchName) && vaCur<vaLimit)
if(vaCur >= vaStart) fprintf(fiOutput, "%5s %.8x %s\n", pszNameType, vaCur, rgchName);
}
void DumpMarkup(FILE * fiOutput, ulong vaStart, ulong vaLimit)
{
DumpMarkupEx(fiOutput, NM_LABEL, "LABEL", vaStart, vaLimit
DumpMarkupEx(fiOutput, NM_COMMENT, "CMT", vaStart, vaLimit)
}
: TEXTLEN , , API- Findnextname()
rgchName
. .
, OllyDbg. , â /, .
, , , OllyDbg. , , OllyDbg - (, OllyDbg) .
, - . , - . ( ), (unwinding) , message loop. , Markup Dumper, , , - â EIP , , â , , , , .
10â20% OllyDbg . 80â90% , OllyDbg - WinAPI-, , , . , , - NtWaitForSingleObject, . wait-, - , . Windows deadlock, . , .
, OllyDbg , . , ( New origin here EIP). New Origin Here, EIP â WinAPI- SetThreadContext()
. , Windows , â . APC. «» APC.
, Windows, OllyDbg, , , deadlock- , , UI , , (WaitForDebugEvent
, ReadProcessMemory
, WriteProcessMemory
) â . , watchdog-, GUI- ( ) . , Spy++ , .
, , OllyDbg , . OllyDbg â , , , . , TLS , , , , , . , , , , /, .
- OllyDbg , CreateRemoteThread()
.
push param1 push param2 push param3 call <____> int3 int3 int3
( ), , int3 â , ( ) -.
, , , OllyDbg . , . , .
, , .
, , â2 â - . , .
«» :
, 0xC000026E
( 0xC000005
â STATUS_ACCESS_VIOLATION
, / ), 0x004436E8
. , , 0x00400000
EXE-, - ( ).
«», , ( , ), . , 0xC000026E
. , STATUS_VOLUME_DISMOUNTED
. OllyDbg - 0x004436E8
, , , OllyDbg?
, , :
- OllyDbg.exe . , . , OllyDbg.exe, , «Go to address» â , :
- - Ctrl+G, (,
at
CommandBar, Ctrl+G). - , .
0x004436E8
â â , «Go to expression». - CS:EIP=
001B:004436E8
004436E8
( ),004436E8
, . , x86- , ( PE CR0 ) ( PG CR0 ). - () , â P (Present) PDE PTE .
- #PF (Page Fault), .
- Windows, (
KiTrap0E()
âMmAccessFault()
â .) â - ( ) ( , ( user-mode)VirtualAlloc
, , - , image-backed swap-backed copy-on-write ). -
00443000
, , , image-backed. - - (
h:\soft\odbg\ollydbg.exe
), , ,H:\
, . ,STATUS_VOLUME_DISMOUNTED
(0xC000026E
).
, . , , , (working set) , , , .
( ), OllyDbg , -
VirtualQueryEx()
, - . â , ( ) , , , , .
, ( DLL, OllyDbg) H:\
. Markup Dumper , . , (), , ( «» , ) .
, , .
, , , , , ( EIP - ) .
- , , OllyDbg (names), , , .
: . , - , . (Oleh Yuschuk), ...
, , , , , , .
Process Explorer , . , , - . Process Explorer ( VAD-), â , ReadProcessMemory()
. , .
â , «», «», . , , â - OllyDbg OllyDbg. H:\
.
API- Insertname()
, name ( , - ) names. names â , (markup), .
, names?
- {, , }. , â . : ( ), , , , , , , , .
- , (). , â , 30 . ( , ). , «» , .
- , (RB, AVL, ..). , , ( ) , / , / .
, , «», . â , .
, OllyDbg OllyDbg _Insertname()
, :
, , EBX, ESI, EDI, â . , â VirtualAlloc()
VirtualFree()
, memcpy
, - OllyDbg .
- : Process Monitor ( FileMon), , - ( TEST.DLL
), - TEST.UDD
, TEST.ARG
( , ). ARG- , , , OllyDbg
- ARG-.
- OllyDbg , ARG-, . : ARG- OllyDbg. ARG- custom-, , , OllyDbg â , , , VirtualAlloc()
VirtualFree()
, , , (push 4
fProtect = 4
, fProtet = PAGE_READWRITE
).
: ARG-, . . , ARG-.
like_memcpy
( memcpy()
libc) ,
Arg3 = ... Arg2 = ... Arg1 = ...
count = ... src = ... dest = ...
, , clr = 8000FF
clr = (R=255 G=0 B=128)
ARG- , OllyDbg «Custom function descriptions», .
, RTFM twice.
, - â ⊠, .
, ? - , . () â , .
:
- ( ),
malloc()
/HeapAlloc()
,VirtualAlloc()
, (, 4 ) . , EDX*24, EDX , - . , 24 â . - Plugin Development Kit (PDK) , Insertname() Quickinsertname() Mergequicknames(), names â API- . , , .
API- Insertname()
, :
, , . Names, , â , , , 24 , 12 , DWORD- ( â , , ). 24 12, 2 â , .
, Insertname()
:
int Insertname(ulong addr,int type,char *name)
{
//
//
//
if(addr == NULL ||
name == NULL ||
type == NM_ANYNAME || type == NM_NONAME)
{
return -1;
}
//
// , name-
//
if(!pNamesBlock) return -1;
if(!(cNameEntriesCur < cNameEntriesMax) && cNameEntriesMax < 1000000)
{
//
// ,
//
void* const pNewBlock = VirtualAlloc(NULL,
2 * cNameEntriesMax * sizeof(NAME_REC),
MEM_COMMIT,
PAGE_READWRITE);
if(pNewBlock)
{
//
// ,
// .
//
memcpy(pNewBlock, pNameBlock, cNameEntriesMax * sizeof(NAME_REC));
VirtualFree(pNamesBlock, 0, MEM_RELEASE);
//
// ,
// ,
// .
//
pNamesBlock = (NAME_REC*)pNewBlock;
cNameEntriesMax *= 2;
}
}
// 00463FB0
, , name
NULL, , -1
. API-:
Insertname
Inserts new or replaces existing name of given type in the name table. If name is NULL or empty, entry is deleted. Returns 0 on success and -1 on error. Note: do not call this function between calls to Quickinsertname and Mergequicknames!
int Insertname(ulong addr,int type,char *name);**
Parameters:
addr â name address;
type â name type ( NM_xxx for predefined types);
name â name to insert. If name is NULL or empty, entry is removed from the name table.
. , - OllyDbg.
. OllyDbg OllyDbg, OllyDbg , OllyDbg , {, , } c «» «» ( NM_LABEL
NM_COMMENT
).
:
DWORD | Address |
DWORD | Value |
DWORD | NameType |
, «Value» names, , - ASCII-. , , â - .
, , - ( ), . - .
, OllyDbg , VirtualAlloc DWORD , : , - .
, ?
, : - , , , , VirtualAlloc
VirtualFree
memcpy
, , , strlen
memcpy
.
, name' , , , , , , â , .
, :
, . , :
if(!(cbStringPoolUsed + TEXTLEN < cbStringPoolTotal))
{
char* pNewStringPool;
int cbDesiredNewPoolSize;
if(GlobVarC > 4096 || cbStringPoolTotal > 320000000)
cbDesiredNewPoolSize = cbStringPoolTotal;
else
cbDesiredNewPoolSize = cbStringPoolTotal * 2;
pNewStringPool = VirtualAlloc(NULL,
cbDesiredNewPoolSize,
MEM_COMMIT,
PAGE_READWRITE);
if(pNewStringPool)
{
int cchUsed = 0;
int i;
for(int i = 0; i < cNameEntriesCur; i++)
{
char* pszCurString = pNamesStringPool + pNamesBlock[i].Offset;
int cchCurString = strlen(pszCurString) + 1;
memcpy(pNewStringPool + cchUsed, pszCurString, cchCurString);
pNamesBloc[i].Offset = cchUsed;
ccUsed += cchCurString;
}
VirtualFree(pNamesStringPool, 0, MEM_RELEASE);
pNamesStringPool = pNewStringPool;
cbStringPoolUsed = cchUsed;
cbStringPoolTotal = cbDesiredNewPoolSize;
GlobalVarC = 0;
}
}
, (, - name', ) , (), , 320 , - ( GlobVarC
) 4096.
, memcpy
? , â ( , , ). , , , - , . , , , , 320 GlobVarC
> 4096. , GlobVarC
â , (delayed) . , / .
, , , cbStringPoolUsed + TEXTLEN < cbStringPoolTotal
,
cchCurString = strlen(Value) + 1;
if(!(cbStringPoolUsed + cchCurString < cbStringPoolTotal))
-, , , , 5 , 100 , , 256 ( TEXTLEN
), .
-, 256 , - . , 255 , .
, strlen(value)
, Insertname()
, 255 !
1000 , Findname()
Findnextname()
, , .
, TEXTLEN
, , . . , , , â , - , . , , , , .
-, , Insertname()
. , watch expressions, , , â name-. , 255 , .
-, OllyDbg , TEXTLEN
, . , Insertname()
Quickinsertname()
, . . , , OllyScript, . , , , .
OllyDbg (, ).
CNFFMappedStream::Unmap
, ?Unmap@CNFFMappedStream@@UAGXEPAPAX@Z
, OllyDbg « ». . , , . :
virtual void __stdcall CNFFMappedStream::Unmap(unsigned char,void * *)
. . , , COMDAT folding.
, . , , , , , , . , , , , , , COMDAT ( Communal Data).
« » : COMDAT, ( , , , ; , COMDAT- , , ). â COMDAT , . "ERROR"
, â , . , , --.
:
unsigned short GetCodepointLength(const char* pCodepoint)
{
#ifdef UNICODE_STRINGS
#if UNICODE_STINGS == UTF8
const char cp = *pCodepoint;
unsigned short cbExtra;
for(cbExtra = 0; (cp & 0x80) && (cbExtra < 3); cbExtra++, cp<<=1)
if(cp & 0x40) {if(!(cp & 0x20)) break; } else return 0;
return 1 + cbExtra;
#else // UCS-2 or UTF-16
return sizeof(wchar_t);
#endif
#else
return sizeof(char);
#endif
}
LONG __stdcall CFlatArrayObject::GetDimensionsCount() // for IArrayObject::GetDimensionsCount
{
return 1;
}
BOOL CAudioCodec::IsSampleFormatSupported(SAMPLE_FORMAT sf)
{
return TRUE; // We support all
}
:
mov eax, 00000001 retn 4
( Microsoft ) COMDAT-folding . .
, COMDAT folding' , , « », , //
«FOLDED:
». COMDAT-folding' ( COM COM-, E_NOTIMP
â 5â6 , , , RETN
). , . , TEXTLEN
. ? OllyScript .
API- , , , , .
, «» Insertname()
, , , , ( ):
PE- (EXE DLL) : ( ) swap-backed , image-backed, copy-on-write image-backed swap-backed, / / , PE-.
, ( , , OllyDbg , , , , .bss
) , OllyDbg ( ), , , swap-backed-.
H:\
( SSD-), OllyDbg.exe, , , ReadProcessMemory()
.
â , , , â , . , ( ) , - , , , .
, â , , , â :
-
NAME_REC
, {, , }. 12 . «» . . - , -, (, ..). «» , , . .
OllyDbg.exe
, â .
, . , , , â , 7 ( 4 ).
, . 1.10. ! 1.10, , OllyDbg, , .
, 0x004EAE14
, , , , , OllyDbg. : â API- Insertname()
. ( , ). API- . , , . , , , , VirtualFree
, , VirtualAlloc
.
, . OllyDbg , , - (ollydbg.exe
), , - . «» , , . -, , , :
struct
{
struct
{
DWORD pNamesBlock;
DWORD cNameEntriesCur;
DWORD cNameEntriesMax;
} NameBlockInfo;
struct
{
DWORD pNamesStringPool;
DWORD cbStringPoolUsed;
DWORD cbStringPoolTotal;
DWORD wtf;
} StringPool;
} Metadata;
void main()
{
HANDLE hOdbgProc;
DWORD cbRead = 0;
DWORD pid = ?????????;
hOdbgProc = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
ReadProcessMemory(hOdbgProc,
(LPCVOID)0x004EAE14,
&Metadata.NameBlockInfo,
sizeof(Metadata.NameBlockInfo),
&cbRead))
ReadProcessMemory(hOdbgProc,
(LPCVOID)0x004EAE30,
&Metadata.StringPool,
sizeof(Metadata.StringPool),
&cbRead))
VOID* pNamesBlock;
VOID* pNamesPool;
pNamesBlock = VirtualAlloc(0, Metadata.NameBlockInfo.cNameEntriesMax * 12, MEM_COMMIT, PAGE_READWRITE);
pNamesPool = VirtualAlloc(0, Metadata.StringPool.cbStringPoolTotal, MEM_COMMIT, PAGE_READWRITE);
ReadProcessMemory(hOdbgProc,
(LPCVOID)Metadata.NameBlockInfo.pNamesBlock,
pNamesBlock,
Metadata.NameBlockInfo.cNameEntriesMax * 12,
&cbRead))
ReadProcessMemory(hOdbgProc,
(LPCVOID)Metadata.StringPool.pNamesStringPool,
pNamesPool,
Metadata.StringPool.cbStringPoolTotal,
&cbRead))
DumpBlockToFile(&Metadata, sizeof(Metadata), "names-meta.dat");
DumpBlockToFile(pNamesBlock, Metadata.NameBlockInfo.cNameEntriesMax * 12, "names-tbl.dat");
DumpBlockToFile(pNamesPool, Metadata.StringPool.cbStringPoolTotal, "names-pool.dat");
VirtualFree(pNamesBlock, 0, MEM_RELEASE);
VirtualFree(pNamesPool, 0, MEM_RELEASE);
}
â , , ReadProcessMemory
. , - , - â , , , -, . , ( ), - â OllyDbg.
, , OllyDbg, , - , .
- , , , , . , , , . , .
- , , , . , , , - ?
-
VirtualAlloc()
, , (4K x86). - 0x00000FFF , . - ,
VirtualAlloc()
Insertname()
- , , , SYSTEM_INFO::dwAllocationGranularity, x86 64K. 16 , . -
VirtualQueryEx()
, ()VirtualAlloc()
, (PAGE_READWRITE
), , . - , «» .
- ( ) , , â . , , 32- , 640 . , , ,
NM_xxxx
, PDK. - ( ) - , . , -
, ( ), â , (.. ). , , . - , , ( ).
, - , « » â , . , API- Insertname
, , .
?
, . . ?
, :
- , Markup Dumper, , , ( , ). , OllyDbg. , 5 , . ( ) .
- «» , ( , ) OllyDbg, API-, . ..
Insertname()
( ). , UDD-. - , , â OllyDbg , / . UDD-. - , . , , , .
, , . , :
typedef struct
{
DWORD Va; // virtual address of NAME in debugger process
DWORD Offset; // offset of NAME's value in string pool
DWORD Type; // one of NM_xxxxx values
} NAME_REC;
, for
-, cNameEntriesCur
Insertname()
, , .
P.S. . , , , , . , howto-, , , - Git VCS, - API- OllyDbg, OllyDbg, Windows . , , , , , - .