If you need to hook functions in third party software, this trick can be used to hook the function without modifying any of the functions code. All you need to do is modify some pointer used by the function to zero, and it will raise an exception as soon as something like p-> is executed on that pointer, then your exception handler can execute whatever code you need (i.e. write over stack, write to memory, exfiltrate handles) and on exit all you need to do is restore the correct register containing the pointer and wind back the execution counter by the size of the de-reference instruction.
Please don't use this knowledge to hurt people ...
It's actually not so easy to find the VEH because if you are injecting the VEH into a third party process from another process, then not only does the VEH not exist during static analysis of the binary at rest, but its program address changes on each execution. Moreover, the VEH can be encrypted at rest before it is injected into the second process.
http://profile.maff1t.com/AntiDebugging/
Interestingly enough, VirtualBox does this too, and they call it "hardening", but IMHO it's quite an unexpected and hostile behaviour which is more characteristic of malware.
Yeah, Apple iTunes did that IIRC, and it was super easy to bypass.
Cat + mouse: Have your program catch any signals/stops (which debuggers do on Linux when they attach I believe)
As a bonus, write important state to memory supposed to be read-only. If someone hooked your ptrace, the hook has to reimplement ptrace in a lot more detail. Or use breakpoints as a mechanism to call subroutines.
int ptrace(int request, int pid, void *addr, void *data) {
return 0;
}
And compile it: gcc -shared myptrace.c -o myptrace.so
Afterwards you can eiher LD_PRELOAD=./mytrace.so ./thebinary # shell
ltrace -S -l ./mytrace.so ./thebinary # strace in shell
or for gdb set environment LD_PRELOAD=./mytrace.so