How to write in an assembler program with overlapping instructions (another bytecode obfuscation technique)
We present to your attention the technique of creating assembler programs with overlapping instructions, - to protect the compiled byte-code from disassembling. This technique is able to withstand both static and dynamic analysis of bytecode. The idea is to pick up such a stream of bytes, when disassembling which starting from two different offsets, two different chains of instructions were obtained, that is, two different ways of executing the program. To do this, we take multibyte assembly instructions, and hide the protected code in the variable parts of the byte code of these instructions. In order to deceive the disassembler, setting it on the wrong track (by disguising the chain of instructions), and protecting the hidden chain of instructions from its gaze.
Three prerequisites for creating an effective "overlap" 3r324444.
In order to deceive the disassembler, the overlapping code must satisfy the following three conditions: 1) The instructions from the masking chain and the hidden chain must always intersect with each other, i.e. Must not be aligned relative to each other (their first and last bytes should not be the same). Otherwise, part of the hidden code will be visible in the masking chain. 2) Both chains should consist of plausible assembler instructions. Otherwise, the masking will be detected already at the stage of static analysis (having encountered an unsuitable code, the disassembler will correct the command pointer and expose the masking). 3) All instructions of both chains must be not only plausible, but also correctly executable (to avoid this, the program crashed when you try to execute them). Otherwise, during the dynamic analysis, bad places will attract close attention of the reverser, and the disguise will be revealed.
Description of the technique of "overlapping" assembler instructions
In order for the process of creating an overlapping code to be as flexible as possible, it is necessary to select only those multibyte instructions that have as many as possible bytes can take an arbitrary value. These multibyte instructions will constitute a masking instruction chain.
Pursuing the goal of creating an overlapping code that will satisfy the three conditions listed above, we consider each masking instruction as a sequence of bytes of the form: XX YY ZZ.
Here XX is the instruction prefix (instruction code and other static bytes, which cannot be changed).
YY are bytes that can be changed arbitrarily (as a rule, these bytes store the immediate numeric value transmitted in the instruction; or the address stored in the operand's memory). YY bytes should be as large as possible - so that they contain more hidden instructions.
ZZ are also bytes that can be changed arbitrarily, with the only difference that the combination of ZZ bytes with the following bytes XX (ZZ XX) should form a valid instruction that satisfies the three conditions formulated at the beginning of the article. Ideally, ZZ should occupy only one byte, so that on YY (this is in fact the most important part - here our hidden code is located) there are as many bytes as possible. The last hidden instruction must end in ZZ, - creating a convergence point for two chains of execution.
The combination of ZZ XX - we will call the gluing instruction. The gluing instruction is necessary, firstly, for joining hidden instructions that are located in the adjacent masking instructions and, secondly, to fulfill the first necessary condition voiced at the beginning of the article: the instructions of both chains must always intersect with each other (therefore, the gluing instruction always is located at the intersection of two masking instructions).
The gluing instruction is executed in a hidden chain of commands, and therefore must be chosen in such a way as to impose as few restrictions on the hidden code. Suppose if, when it is executed, the general purpose registers and the EFLAGS register change, then the hidden code will not be able to effectively use the corresponding registers and conditional commands (for example, if there is a comparison operator before the gluing instruction, and the gluing instruction itself changes the value of the EFLAGS register, which is after the gluing instruction - it will work incorrectly).
The above description of the ceiling technique is illustrated in the following figure. If execution begins with the initial bytes (XX), then the masking instruction chain is activated. And if from YY bytes, the hidden chain of instructions is activated.
Assembly instructions suitable for the role of “masking instructions” 3r324444.
The longest of the instructions, which at first glance suits us best, is the 10-byte version of MOV, where the first operand is the offset specified by the register and the 32-bit address, and the second operand is the 32-bit number. In this manual, the most bytes that can be changed arbitrarily (as many as 8 pieces).
However, although this instruction looks plausible (theoretically, it can be executed correctly), it still does not suit us, because its first operand will usually indicate an unavailable address, and therefore when trying to perform such a MOV, the program will collapse. So This 10-byte MOV does not satisfy the third necessary condition: all instructions of both chains must be correctly executed.
Therefore, we will choose to play the role of masking instructions only those applicants who do not create the risk of a program crash. This condition significantly narrows the range of instructions suitable for creating an overlapping code, but there are still suitable ones. Below are four of them. Each of these four instructions has five bytes, which can be changed arbitrarily, without the risk of a program crash.
This instruction calculates the memory address specified by the expression in the second operand, and stores the result in the first operand. Since we can refer to the memory without actually accessing it (and, accordingly, without the risk of a program crashing), the last five bytes of this instruction can take arbitrary values.
This instruction performs the MOV operation if the condition “cc” is met. In order for this instruction to satisfy the third requirement, the condition must be chosen in such a way that under any circumstances it will be FALSE. Otherwise, this instruction may attempt to access an unavailable memory address, and so on. bring down the program.
Acts on the same principle as CMOVcc: sets a byte to one if the "cc" condition is met. This instruction has the same problem as CMOVcc: accessing an invalid address will result in a program crash. Therefore, the choice of the condition "cc" must be approached very carefully.
NOPs can be of different lengths (from 2 to 15 bytes), depending on which operands are indicated in them. At the same time, there will be no risk of collapsing the program (due to accessing an invalid memory address). Because the only thing that NOPs do is to increase the instruction counter, (they do not perform any operations on the operands). Therefore, NOP’s bytes, in which operands are specified, can take an arbitrary value. A 9-byte NOP is best suited for our purposes.
For reference, here are a few other NOP’s.
3r3158. Assembly instructions suitable for the role of “gluing instructions” 3r324444.
The list of instructions suitable for the role of gluing instruction is unique for each specific masking instruction. Below is a list (generated by the algorithm shown in the figure below) using the example of a 9-byte NOP.
In forming this list, we took into account only those variants in which ZZ occupies 1 byte (otherwise there will be little space left for the hidden code). Here is a list of suitable gluing instructions for the 9-byte NOP’a.
Among this list of instructions there is no one that would be free from side effects. Each of them changes either EFLAGS or general registers, or both. This list is divided into 4 categories, - according to the side effect the instruction has.
The first category is
included instructions that change the register EFLAGS, but do not change the general registers. Instructions from this category can be used when there are no conditional jumps in the chain of hidden instructions and any instructions that are based on the evaluation of information from the EFLAGS register. This category in this case (for 9-byte NOP'a) includes only two instructions: TEST and CMP.
Below is a simple example of hidden code that uses TEST as the gluing instruction. This example makes the exit system call, which for all Linux versions returns 1. In order to correctly form the TEST instruction for our needs, we will have to set the last byte of the first NOP’a value 0xA9. This byte, when concatenated with the first four bytes of the next NOP’a (66 0F 1F 84), turns into a TEST EAX instruction, 0x841F0F66. The following two figures show the corresponding assembler code (for the masking chain and hidden chain). The hidden chain is activated when control is transferred to the 4th byte of the first NOP.
In the second category
includes instructions that change the values of general-purpose registers or available memory (stack, for example), but do not change the register EFLAGS. When executing the PUSH instruction or any variant of MOV, where the immediate operand is specified as the second operand, the EFLAGS register remains unchanged. So The gluing instructions of the second category can be placed even between the comparison instruction (TEST, for example) and the instruction evaluating the EFLAGS register. However, instructions in this category limit the use of the register, which appears in the corresponding gluing instructions. For example, if MOV EBP, 0x841F0F66 is used as a gluing instruction, then the possibilities of using the EBP register (from the rest of the hidden code) are significantly limited.
The third category is
includes instructions that change the EFLAGS register, and registers the general purpose (or memory) change. These instructions have no clear advantages compared with the instructions from the first two categories. However, they can also be used, because they do not conflict with the three conditions formulated at the beginning of the article. The fourth category includes instructions, under which there is no guarantee that the program will not collapse - there is a risk of improper access to memory. They are extremely undesirable. they do not satisfy the third condition.
Assembler instructions that can be used in a hidden chain
In our case (when 9-byte NOPs are used as masking instructions), the length of each instruction from the hidden chain should not exceed four bytes (this restriction does not apply to gluing instructions that take 5 bytes). However, this is not a very critical limitation, because most instructions that have more than four bytes in length can be decomposed into several shorter instructions. Below is an example of a 5-byte MOV’a that is too large to be placed in a hidden chain.
However, this five-byte MOV can be decomposed into three instructions, the length of which does not exceed four bytes.
Strengthening masking by scattering masking NOPs throughout the program 3-333244.
A large number of consecutive NOP’ov looks, from the point of view of the reverse, very suspicious. By focusing his interest on these suspicious NOPs, an experienced reverser can get to the bottom of the code hidden in them. To avoid such exposure, you can disperse the masking NOPs - throughout the program.
The correct chain of execution of the hidden code in this case can be supported by two-byte unconditional branch instructions. In this case, the last two bytes of each NOP’a will occupy a 2-byte JMP.
Such a trick allows you to break one long sequence of NOPs into several short ones (or even use one NOP at all). In the last NOP’e of such a short sequence, only 3 bytes of the payload can be placed (the 4th byte will be taken by the unconditional branch instruction). So here there is an additional limit on the size of valid instructions. However, as mentioned above, long instructions can be laid out on a chain of shorter instructions. Below is an example of the same 5-byte MOV’a, which we have already laid out in order to meet the limit of 4 bytes. However, now we will decompose this MOV so that it is within the limit of 3 bytes.
Having laid out all the long instructions on shorter ones on the same principle, we can, for the purpose of greater camouflage, in general only single NOP'ami scattered throughout the program, use it. Double-byte JMP instructions can jump forward and back 127 bytes, which means that two consecutive NOP'a (consecutive, in terms of a chain of hidden instructions) must be within 127 bytes.
Such a trick has another significant advantage (besides enhanced masking): it can be used to place hidden code in the already existing NOPs of the compiled binary file (that is, insert the payload into the binary after compiling it). However, it is not necessary that these abandoned NOPs be 9-byte. For example, if there are several single-byte NOPs in a row in a binary, then they can be converted to multibyte NOPs without disturbing the functionality of the program. The following is an example of tthe NOP'ov dispersion technicians (this code is functionally equivalent to the example just above).
Such hidden code, hidden in scattered throughout the program NOP’ah, is much more difficult to detect.
An attentive reader probably noticed that the first byte of the first NOP is not in demand. However, there is nothing wrong with that. Because this unclaimed byte is preceded by an unconditional jump. So control will never be transferred to it. So everything is fine.
This is the technique for creating overlapping code. Use on health. Hide your precious code from prying eyes. But only adopt some other instruction, and not a 9-byte NOP. Because reversers will probably also read this article.
It may be interesting
This is my first time i visit here. I found such a substantial number of interesting stuff in your blog especially its examination. Really its inconceivable article. Keep it up.Gulf Coast Western Reviews
I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success in your business.GSM Solutions
Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thanksthc vape juice
Thankyou for sharing the data which is beneficial for me and others likewise to see.Gulf Coast Western Reviews
Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.GSM Solutions
Hello I am so delighted I located your blog, I really located you by mistake, while I was watching on google for something else, Anyways I am here now and could just like to say thank for a tremendous post and a all round entertaining website. Please do keep up the great work.thc vape juice