walied
December 15th, 2011, 18:12
I need to present a new anti-attach trick that i have recently come up with.
Given the two following facts, 1) For a debugger to attach itself to a process, the debugger has to create a remote thread in the process, 2) The OS loader calls TLS callbacks when a new thread is created in a process - we can design a TLS callback which increments a global variable. This global variable holds number of threads in the current process. If value in this variable exceeds a specific number, this means that a foreign thread has just been created and the process has to exit as such.
This is a simple demonstrating example.
http://ollytlscatch.googlecode.com/files/example1.rar ("http://ollytlscatch.googlecode.com/files/example1.rar")
2526
To make things harder, we would use dynamic TLS callbacks instead.
To implement a dynamic TLS callback, follow these 2 steps:
1) Create a TLS structure and then store its rva and size in the TLS data directory at runtime.
2) Set the "_LdrpImageHasTls" global variable in ntdll.dll to true.
Source code can be found here.
http://ollytlscatch.googlecode.com/files/example2.rar ("http://ollytlscatch.googlecode.com/files/example2.rar")
It works on Win XPSP3 only. You can edit the source code to include other OSes.
N.B. This trick is still in progress and i am waiting for any feedback.
Given the two following facts, 1) For a debugger to attach itself to a process, the debugger has to create a remote thread in the process, 2) The OS loader calls TLS callbacks when a new thread is created in a process - we can design a TLS callback which increments a global variable. This global variable holds number of threads in the current process. If value in this variable exceeds a specific number, this means that a foreign thread has just been created and the process has to exit as such.
This is a simple demonstrating example.
http://ollytlscatch.googlecode.com/files/example1.rar ("http://ollytlscatch.googlecode.com/files/example1.rar")
2526
To make things harder, we would use dynamic TLS callbacks instead.
To implement a dynamic TLS callback, follow these 2 steps:
1) Create a TLS structure and then store its rva and size in the TLS data directory at runtime.
2) Set the "_LdrpImageHasTls" global variable in ntdll.dll to true.
Source code can be found here.
http://ollytlscatch.googlecode.com/files/example2.rar ("http://ollytlscatch.googlecode.com/files/example2.rar")
It works on Win XPSP3 only. You can edit the source code to include other OSes.
N.B. This trick is still in progress and i am waiting for any feedback.