Dynamic obfuscation by self-modifying code

◇◆丶佛笑我妖孽 提交于 2019-12-06 11:49:49

Do you want to do this at runtime or during authorship?

You can probably instruct your C compiler to produce assembly language output, for example gcc has the -S option which will produce output in file.s Your compiler suite may also have a program like objdump which can decompile an object file or entire executable. However, you generally want to leave optimizations up to a modern compiler rather than do it yourself.

At runtime the & operator can take the address of a function and you can read through it, though you have to be prepared for the possibility of encountering a branch instruction before anything interesting, so you actually have to programatically "understand" at least a subset of the instruction set. What you will run into when reading function pointers will of course vary all over the place by machine, ABI, compiler, optimization flags, etc.

Put the functions into t1.c and t2.c use gcc -S to generate assembly output:

gcc -S t1.c
gcc -S t2.c

Now compare t1.s and t2.s.

If you are using Visual Studio, go to

Project Properties -> Configuration -> C/C++ -> Output Files -> Assembler output

or use compiler switches /FA, /FAc, /FAs, /FAcs. Lower-case c means output machine code, s-source code side-by-side with assembly code. And don't forget to disable compiler optimizations.

Having read through some of the answers and the comments there, I'm not sure I fully understand your question, but maybe you're looking for a gcc invocation like the following:
gcc -S -xc - -o -

This tells gcc to input C code from stdin and output assembly to stdout.

If you use a vi-like editor, you can highlight the function body in visual mode and then run the command:
:'<,'>!gcc -S -xc - -o - 2> /dev/null
...and this will replace the function body with assembly (the "stderr > /dev/null" business is to skip errors about #include's).

You could otherwise use this invocation of gcc as part of a pipeline in a script.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!