Jump to content
co4ie

Nytro @ DefCamp Prezentare

Recommended Posts

compile :

Da, am avut si eu aceasta problema.

Thread safe nu cred ca se poate face, cel putin nu in proportie de 100%, dar se poate incerca ceva:

1. Iti faci o lista cu toate threadurile

2. Faci GetThreadConext pe toate

3. Suspenzi toate thread-urile in afara de thread-ul curent si thread-ul care apeleaza functia hooked

4. Verifici daca vreun alt thread, bazat pe context, executa cod in acei bytes ai tai

5. Permiti doar executia acelor thread-uri: cel curent si cel care apeleaza functia

6. Dupa ce ai dat restore la bytes dai resume tuturor thread-urilor

Ce poate sa nu mearga ok:

1. Sa apeleze un alt proces CreateRemoteThread si sa faca vreo prostie

2. Tot oprind si pornind thread-uri in mod "aleator" sa crape programul (la modul logic de functionare)

O solutie 1337 ar fi:

1. Iei toate functiile pe care vrei sa pui hook

2. Aloci o zona de memorie cu read, write, execute

3. Copiezi functiile acolo (trebuie sa stii "dimensiunea" lor, nu te poti baza pe ret/retn)

4. Pui 5 x NOP + mov edi, edi inainte de fiecare functie

5. Modifici toti pointerii la functii catre noua locatie

6. Hook ca pe WinAPI cu jmp-5 + jmp (thread safe)

Problema:

- Daca procesul a facut GetProcAddress ramane cu vechiul pointer la functie

+ Dar acolo pui un simplu jmp catre noua locatie

Link to comment
Share on other sites

Sunt constient de problema threadurilor de asta am si gasit solutia de mai sus care permite hookingul fara sa fie nevoie sa dai move/restore la bytes la fiecare apelare fiind mult mai safe pentru ca riscul ca un thread sa crape este foarte mic(doar la instalarea hookului daca se apleaza API-ul dupa ce ai mutat cei 5 octeti dar inainte de a pune jumpul).

Link to comment
Share on other sites

Da, dar solutia cum "copy & replace" nu este 100% thread safe.

Ai 5 bytes: push ebp, mov ebp, esp, adica 2 instructiuni

Daca un thread executa DOAR "push ebp" apoi tu pui "jmp"... Nu mai e ok.

Ca si in link-ul de mai sus:

"The problem with Detouring a function during live execution is that you can never be sure that at the moment you are patching in the Detour, another thread isn't in the middle of executing an instruction that overlaps the first five bytes of the function. (And you have to alter the code generation so that no instruction starting at offsets 1 through 4 of the function is ever the target of a jump.)"

Bine, asta e paranoia si cred ca e destul de greu sa se intample, dar totusi e posibil.

Nota: Am uitat (nu am vrut sa risc sa stric proiectul) sa dau FlushInstructionCache, ar cam trebui facut acest lucru dupa modificarea de cod.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...