This application claims priority from Korean Patent Application No. 10-2019-0144010 filed on Nov. 12, 2019 in the Korean Intellectual Property Office, and all the benefits accruing therefrom under 35 U. S. C. 119, the contents of the application in its entirety is herein incorporated by reference.
The present disclosure relates to a method and device for patching a binary in which a vulnerability exists. More specifically, the present disclosure relates to a method and device for directly inserting patch code that supplement a vulnerability of a binary into the binary.
Various vulnerabilities may exist in a program binary. For example, various types are known, including use of an uninitialized variable, buffer overflow, integer overflow, memory exception, malformed-input, race condition, symbolic link, null pointer references, etc. Due to the vulnerability, some functions of the binary may be modified without permission for an inappropriate purpose, and a system in which the binary is executed may cause security problems. Therefore, the vulnerability of the binary running on security-critical systems will need to be resolved quickly and accurately. Also, if such a vulnerability exists in the binary, an unexpected error occurs at the time of execution of the binary. For example, use of an uninitialized variable may result in an unexpected result value, or a control flow of a program may proceed to a wrong path. Further, segmentation faults or the like frequently occur due to omission of an exception regarding buffer overflow.
In the process of developing a program, generally, one logically analyzes and verifies contents of source code of a program, or one uses a debugging program to set breakpoints in the source code of the program where there is a possibility of vulnerability. Then, when the execution is suspended according to the break point during the execution of the program, the vulnerability of the program is identified by observing the execution environment of the program. Then, the vulnerability of the program may be removed by rebuilding the program by modifying source code so that the identified vulnerability is removed.
However, after a program is developed, especially if a third party who is not the program's developer analyzes and patches the program's vulnerability, vulnerability analysis and patching are mostly performed on program binaries in environments where source code cannot be obtained. Therefore, the method for removing the vulnerability by modifying the source code may not be utilized. Therefore, it would be useful for a technique for directly modifying and patching a binary in which a vulnerability exists.
Aspects of the present disclosure provide a method and device for patching a binary in which a vulnerability exists.
Aspects of the present disclosure also provide a method and device for directly inserting patch code that supplement a vulnerability of a binary into the binary.
Aspects of the present disclosure also provide a method and device for effectively patching a vulnerability of a binary due to the use of an uninitialized variable.
Aspects of the present disclosure also provide a method and device for effectively patching a vulnerability of a binary due to null pointer reference.
However, aspects of the present disclosure are not restricted to those set forth herein. The above and other aspects of the present disclosure will become more apparent to one of ordinary skill in the art to which the present disclosure pertains by referencing the detailed description of the present disclosure given below.
According to an aspect of the present disclosure, a method for patching a vulnerability of a binary performed on a computing device is provided. The method comprises, inserting a patch code block in a target binary, and replacing at least some instructions in an area where the vulnerability exists in the target binary with a first instruction to jump to the patch code block, wherein the patch code block comprises a second instruction to resolve the vulnerability, and a third instruction to jump to a fourth instruction next to the first instruction replaced in the area where the vulnerability exists.
According to an aspect of the present disclosure, a method for patching a vulnerability of a binary performed on a computing device is provided. The method comprises, inserting a patch code block in a target binary, and replacing at least some instructions in an area where the vulnerability exists in the target binary with a first instruction to jump to the patch code block, wherein the patch code block comprises a second instruction to determine whether a crash occurs at runtime.
According to an aspect of the present disclosure, a device for patching a vulnerability of a binary is provided. The device comprises a patch area generation unit, and a patch execution unit, wherein the patch area generation unit is configured to search for a free area among text areas of a target binary, add an area for a patch code to the target binary based on a size of the free area being less than a size of the patch code, and determine a patch area into which the patch code is to be inserted in the target binary, wherein patch execution unit is configured to insert the patch code in the patch area, and replace at least some instructions in an area where the vulnerability exists in the target binary with a first instruction to jump to the patch area, wherein the patch code comprises a second instruction to resolve the vulnerability, and a third instruction to jump to a fourth instruction next to the first instruction replaced in the area where the vulnerability exists.
The above and other aspects and features of the present disclosure will become more apparent by describing in detail exemplary embodiments thereof with reference to the attached drawings, in which:
Hereinafter, embodiments of the present disclosure will be described with reference to the attached drawings. Advantages and features of the present disclosure and methods of accomplishing the same may be understood more readily by reference to the following detailed description of embodiments and the accompanying drawings. The present disclosure may, however, be embodied in many different forms and should not be construed as being limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete and will fully convey the concept of the disclosure to those skilled in the art, and the present disclosure will be defined by the appended claims.
In adding reference numerals to the components of each drawing, it should be noted that the same reference numerals are assigned to the same components as much as possible even though they are shown in different drawings. In addition, in describing the present disclosure, based on it being determined that the detailed description of the related well-known configuration or function may obscure the gist of the present disclosure, the detailed description thereof will be omitted.
Unless otherwise defined, all terms used in the present specification (including technical and scientific terms) may be used in a sense that can be commonly understood by those skilled in the art. In addition, the terms defined in the commonly used dictionaries are not ideally or excessively interpreted unless they are specifically defined clearly. The terminology used herein is for the purpose of describing embodiments and is not intended to be limiting of the present disclosure. In this specification, the singular also includes the plural unless specifically stated otherwise in the phrase.
In addition, in describing the component of this present disclosure, terms, such as first, second, A, B, (a), (b), can be used. These terms are for distinguishing the components from other components, and the nature or order of the components is not limited by the terms. If a component is described as being “connected,” “coupled” or “contacted” to another component, that component may be directly connected to or contacted with that other component, but it should be understood that another component also may be “connected,” “coupled” or “contacted” between each component.
Hereinafter, some embodiments of the present disclosure will be described in detail with reference to the accompanying drawings.
First, with reference to
First, vulnerability identification and patch target binaries 10 may be given, and vulnerability area search or a vulnerability search process 110 for the target binaries 10 is performed. The vulnerability search 110 may be performed using various conventional techniques such as fuzzing, symbolic execution, and hybrid fuzzing combining them.
The fuzzing is a method that generates a situation where a program crashes by repeatedly generating arbitrary values (test cases) and inputting them into a target program. The symbol execution is a method for specifying an input value of a target program as a symbol and finding a value that may reach a path of the program's vulnerability. The hybrid fuzzing is a technique that combines fuzzing and symbolic execution. It is a way to use fuzzing in shallow paths to quickly explore vulnerabilities, and to search for vulnerabilities by using symbolic execution based on path search no longer being possible.
By the vulnerability search process 110, an address of an area where the vulnerability was found in the target binary, an address of an instruction causing the vulnerability, or a name of a function where the vulnerability was found may be identified, and information on crash caused by the vulnerability may be written.
Next, a process of analyzing 120 a cause of the vulnerability in
Next, a process 130 of patching to remove vulnerabilities in
So far, it has been described with reference to
First, in step S210, the target binary 10 is loaded, and information on a found vulnerability is identified. The information on the vulnerability may include the type of vulnerability and an address of an area where the vulnerability occurred. The information on the vulnerability may be provided as a result of performing the process of searching 110 for the vulnerability and the process of analyzing 120 the cause of the vulnerability as shown in, for example,
In step S220, an area into which patch code is to be inserted is secured in the target binary 10, and the address of the area is identified. According to some embodiments of the present disclosure, the entire patch code for removing the vulnerability may be integrated into a file of the target binary 10. Further, according to some embodiments of the present disclosure, a free space present in the target binary 10 is identified and may be used for inserting the patch code. Further, according to some other embodiments of the present disclosure, an additional area for the patch code may be generated in the target binary 10. A method for securing an area in which patch code is to be inserted in the target binary 10 will be described in more detail with reference to
First, in step S222, a code section (.text) in the target binary 10 is searched. For example, a free space in the code section (.text) of the target binary 10 is searched. Here, the free space includes spaces filled with, for example, NOP commands. The NOP command may correspond to opcodes such as 0x90, 0x00, 0x00000000, depending on the architecture of the CPU.
In step S224, a size of the free space found as a result of the search and a size of a patch code block may be compared with each other. Based on a free space of the size sufficient to insert a patch code block being found, the found free space is determined as an area into which the patch code block is to be inserted (step S226). In addition, an address where the free space starts is identified, and may be used in step S230 of
If the code section (.text) in the target binary 10 is searched, and based on no free space being found or the size of the found free space is smaller than the size of the patch code block, it proceeds to step S228.
In step S228, an additional area for the patch code block is generated in the target binary 10. In some embodiments, a new section may be generated for the additional area in the target binary 10. In some other embodiments, the additional area may be generated in a suitable space among existing sections in the target binary 10. In addition, in order to reflect the generation of the additional area, a new header may be added in the file of the target binary 10, or some of existing headers in the file of the target binary 10 may be modified. The header includes a section header, a program header, or PHDR. The additional area generated in step S228 is determined as an area into which the patch code block is to be inserted (step S226). In addition, an address where the additional area starts is identified, and may be used in step S230 of
Referring again to
In step S230, a patch code block including a patch code for removing the vulnerability is inserted into the target binary 10. Here, the area within the target binary 10 secured in step S220 described above may be used. For example, the patch code block may be inserted into a free space of a code section in the target binary 10 or a space additionally generated for inserting a patch code block in the target binary 10. In some embodiments, the patch code is machine code written in machine language, and the patch code may be written in the area in the file of the target binary 10.
In some embodiments, based on the vulnerability to be removed being a vulnerability due to the use of an uninitialized variable, the patch code may include instructions for initializing values stored in a memory space allocated to uninitialized variables. In some other embodiments, based on the vulnerability to be removed being a vulnerability due to the reference of a null pointer, the patch code may include instructions for determining whether a value of a register or operand that may store a null value (NULL) is a null value or 0, and depending on the result, stopping execution of the target binary 10 while outputting an appropriate error message, or continuously executing the target binary 10.
In addition, the patch code includes instructions for causing a control flow of the binary 10 to return to a point where the vulnerability occurred, following instructions for performing the operations as described above. The instruction for returning to the point where the vulnerability occurred includes an opcode indicating a jump command and an address of the point where the vulnerability occurred. For the address of the point where the vulnerability occurred, the information identified in step S210 described above may be used.
In step S240, at least a part of an instruction in the area in which the vulnerability is found in the target binary 10 is replaced with a new instruction. The new instruction includes an instruction to jump to the patch code block. The instruction to jump to the patch code block includes an opcode indicating a jump command and an address of the patch code block. Machine code corresponding to the instruction to jump to the patch code block may be written in a location where an instruction to be replaced in the area in which the vulnerability is found is written, in the file of the target binary 10.
Although
In step S250, a binary to which a patch is applied to the target binary 10 is stored or transmitted through steps S210 to S240 described above. In some embodiments, the patched binary may be stored as a separate file from the file of the target binary 10. In some other embodiments, the patched binary may replace the file of the target binary 10. In some other embodiments, the patched binary may be transmitted over a network to another computing device.
So far, with reference to
Hereinafter, a method for patching a binary having a vulnerability of using an uninitialized variable according to some embodiments of the present disclosure will be described with reference to
The binary 500 before patching the vulnerability in
The prologue 511 is an area including instructions for setting a stack frame of the ‘foo’ function and securing a memory space for local variables declared in the ‘foo’ function. For example, the prologue 511 includes an instruction to store a value of a base pointer register on a stack (push % ebp), an instruction to store a value stored in the stack pointer register in the base pointer register (mov % esp, % ebp), and an instruction to secure a memory space (stack) for the local variables declared in the ‘foo’ function by subtracting a memory address indicated by the stack pointer register by 0x28, for example (sub 0x28, % esp).
The body 512 includes instructions corresponding to operations performed by the ‘foo’ function (e.g., a printf statement in the example code shown in
The binary 520 after patching the vulnerability in
In more detail, first, the patch code block 540 is inserted into the binary 520 after patching the vulnerability. An area into which the patch code block 540 is inserted may be an free space existing in a code section (.text) of the binary 510 before patching the vulnerability, or an area generated additionally in the binary 510 before patching the vulnerability, determined by the method described with reference to
The patch code block 540 includes a block 541 in which instructions of the prologue 511 of the vulnerable function block 510 may be copied and written as it is. Also, the patch code block 540 includes a block 542 in which instructions for removing a vulnerability of the ‘foo’ function may be written.
For example, the block 542 includes instructions for initializing the local variables of the ‘foo’ function. In some embodiments, instructions to remove the vulnerability of the ‘foo’ function include instructions to store, for example, a value of 0 in a memory space from a memory address indicated by the base pointer register to a memory address indicated by the stack pointer register. In other words, an initial value (e.g., 0) is stored in the local variable of the ‘foo’ function. The vulnerability existing in the ‘foo’ function may be removed. Also, an instruction to jump to a block 532 is written in the block 542. The block 532 will be described later.
As described above, the binary 520 after patching the vulnerability includes the block 530 in which the contents of the vulnerable function block 510 of the binary 510 before patching the vulnerability may be modified. The block 530 includes a block 531 in which the instructions of the prologue 511 of the vulnerable function block 510 may be replaced with new instructions, and a block 532 including instructions of the body 512 of the vulnerable function block 510 as it is.
The block 531 includes an instruction to jump to the patch code block 540 instead of the instructions of the prologue 511. In some embodiments, an appropriate number of NOP instructions may be written to the block 531 so that the block 531 has the same size as the prologue 511.
Meanwhile, the instructions of the body 512 of the vulnerable function block 510 may be written in the block 532 as it is. For example, the instructions corresponding to the operations performed by the ‘foo’ function (e.g., the printf statement in the example code shown in
So far, instructions written in the binary before and after performing a patch that removes the vulnerability due to the use of uninitialized variables, for the binary of the program shown in
First, based on the ‘foo’ function being called in a main( ) function of the program shown in
Referring to
Subsequently, instructions written in machine code in the prologue of the vulnerable function may be read and stored in a variable ‘b_prologue,’ and the file descriptor is moved back to a location of the prologue.
Subsequently, a first jump instruction is written in machine code at the location of the prologue. Here, the first jump instruction is an instruction to jump to an address where a patch code block is inserted. The address where the patch code block is inserted may be an address of a free space existing in a code section (.text) of the binary 510 before patching the vulnerability, or an area additionally generated in the binary 510 before patching the vulnerability, for example, as determined by the method described with reference to
Subsequently, the file descriptor is moved to the address where the patch code block is inserted, and the machine codes of the prologue of the vulnerable function stored in the variable ‘b_prologue’ may be written to the address.
Subsequently, instructions for initializing the local variables of the vulnerable function may be written in machine code. For example, the instructions may be, for example, instructions that store a value of 0 in a memory space from a memory address indicated by a base pointer register to a memory address indicated by a stack pointer register. In other words, the instructions may be instructions to remove the vulnerability of the target binary.
Then, a second jump instruction is written in machine code. Here, an address of a destination of the second jump instruction may be determined based on an address in which the instructions of the prologue portion of the vulnerable function may be written in the target binary. For example, an address immediately following the address in which the instructions of the prologue of the vulnerable function may be written may be used as an operand indicating the destination of the second jump instruction.
Through the patch program as described above, a vulnerability may be removed from a binary having a vulnerability of using an uninitialized variable.
So far, the method for patching the binary having the vulnerability of using the uninitialized variable according to some embodiments of the present disclosure is described with reference to
If binary code compiled with the example code shown in
Hereinafter, a method for patching a binary having a vulnerability due to abnormal memory access will be described according to some other embodiments of the present disclosure. In the following description, a method for patching a vulnerability due to the reference of a null pointer among vulnerabilities caused by abnormal memory access will be described. However, it is noted that a method to be described later may also be applied to patch other types of vulnerabilities caused by abnormal memory access (e.g., buffer over-write, buffer under-write, buffer over-read, buffer under-read, etc.).
The binary 800 before patching a vulnerability in
In the binary 820 after patching the vulnerability in
The patch code block 840 includes an instruction to verify a value of the register ‘eax’ used as an operand of an instruction accessing a memory. For example, the patch code block 840 includes an instruction 841 to determine whether the value of the register ‘eax’ is a NULL value. The patch code block 840 includes a second instruction 842 to moves a control flow to an instruction 844 based on the value of the register ‘eax’ not being a NULL value. In addition, based on the value of the register ‘eax’ being a NULL value, the patch code block 840 includes instructions 843 to output an error message (“[ERROR] null ptr”) and stop the execution of the binary 820 after patching the vulnerability.
In addition, the patch code block 840 includes an instruction 844 that directly copies the instructions 811 that may cause the crash (mov (% eax), % eax), and subsequently includes a third jump instruction 845 that jumps to an address where an instruction following the instruction 811 is written.
So far, instructions written in binaries before and after performing a patch that removes a vulnerability due to the reference of a null pointer have been described by the method according to some other embodiments of the present disclosure. Hereinafter, an execution flow of the binary 820 after patching the vulnerability will be described.
Based on the ‘foo2’ function being called in the binary 820 after patching the vulnerability first, the control flow moves to a block 830. The first jump instruction 831 is executed that is written in place of the instructions 811 that have the potential to cause a crash due to the reference of a null pointer (mov (% eax), % eax). The control flow is moved to the patch code block 840 by the first jump instruction 831.
The instruction 841 is an instruction to determining whether the value of the register ‘eax’ used as an operand of an instruction for accessing a memory is a NULL value, as described above. Based on the value of the register ‘eax’ not being a NULL value, the control flow is moved to the instruction 844 by the second jump instruction 842.
The instruction 844 is an instruction that copies the instruction 811 having the possibility of causing a crash due to the reference of a null pointer (mov (% eax), % eax), as it is. However, since it has already been determined that the value of the register ‘eax’ is not a NULL value, a crash due to the reference of the null pointer will not occur even based on the instruction 844 being executed. After the instruction 844 is executed, by the third jump instruction 845, the control flow is moved back to the next address of the instruction 831 of the block 830.
On the other hand, as a result of executing the instruction 841 to determine whether the value of the register ‘eax’ is a NULL value, based on it being determined that the value of the register ‘eax’ is a NULL value, the instruction (mov (% eax), % eax) will cause a crash. The instructions 843 to output an error message (“[ERROR] null ptr”) and stop execution of the binary 820 after patching the vulnerability may be executed.
So far, referring to
Hereinafter, a device for patching a vulnerability of a binary according to another embodiment of the present disclosure will be described with reference to
A device for patching the vulnerability of the binary 900 according to the present embodiment may include a binary analysis unit 910, a patch area generation unit 920, and a patch execution unit 930. The device for patching the vulnerability of the binary 900 receives a target binary 10 having a vulnerability, removes the vulnerability, and outputs the binary 20 to which the patch to remove the vulnerability is applied. Although not shown, in some embodiments, information on the vulnerability found in the target binary 10 may also be provided as an input to the device for patching 900. The information on the vulnerability may include the type of vulnerability and an address of an area where the vulnerability occurred. The information on the vulnerability may be provided as a result of performing the process of searching 110 for the vulnerability and the process of analyzing 120 the cause of the vulnerability as shown in, for example,
The binary analysis unit 910 may disassemble the target binary 10 to calculate assembly code. In addition, the binary analysis unit 910 may receive information on the vulnerability found in the target binary 10, and identify an address of an area where the vulnerability occurred or a function where the vulnerability occurred. In addition, the binary analysis unit 910 may identify an address in which a prologue is written, by searching the prologue of the function in which the vulnerability occurred, from the assembly code for the target binary 10. The binary analysis unit 910 may provide the assembly code and the information on the vulnerability to the patch area generation unit 920 and the patch execution unit 930.
The patch area generation unit 920 may secure an area in which patch code is to be inserted in the target binary 10. The patch area generation unit 920 may determine the area in which the patch code is to be inserted by searching for a free space having a size sufficient to insert the patch code in the target binary 10. Here, the patch area generation unit 920 may identify a space filled with an NOP instruction in a code section (.text) in the target binary 10 as the free space. On the other hand, based on the free space having the size sufficient to insert the patch code in the target binary 10 not being found, the patch area generation unit 920 may generate an additional area in the target binary 10, and may modify a program header or section header corresponding to a segment or section in which the additional area is generated, in the file of the target binary 10, so as to reflect the generation of the additional area. The patch area generation unit 920 may provide the patch execution unit 930 with information (e.g., an address and size of the area) on the area in which the patch code is to be inserted in the target binary 10.
The patch execution unit 930 may automatically generate patch code based on the information on the vulnerability. Here, the patch code may be automatically generated in the form of machine code, or automatically generated in the form of assembly code and then converted into machine code.
The patch code includes instructions to remove or resolve the vulnerability found in the target binary 10. For example, based on the vulnerability found in the target binary 10 being a vulnerability due to the use of an uninitialized variable, the instructions may include instructions to initialize a stack frame of a function using an uninitialized variable. As another example, based on the vulnerability found in the target binary 10 being a vulnerability due to the reference of a null pointer, the instructions may include an instruction to determine a value stored in a register used as an operand of an instruction that access a memory, and an instruction to output an appropriate error message based on a value stored in the register being a null value. As another example, based on the vulnerability found in the target binary 10 being a vulnerability due to buffer overwrite, the instructions may include an instruction to determine a value stored in a register that may be likely to be used to access an area beyond a memory range allocated to a buffer.
In addition, the patch code may include a jump instruction after instructions to remove or resolve the vulnerability. The jump instruction is an instruction to move a control flow to an area where the vulnerability is found in the target binary 10. For example, the jump instruction includes an instruction to jump to a next address of the last instruction in the area where the vulnerability is found.
The patch execution unit 930 may insert the patch code into an area in the target binary 10 identified by the patch area generation unit 920. For example, the patch execution unit 930 may write the patch code in the form of machine code in the address of the area where the patch code is to be inserted in the target binary 10.
In addition, the patch execution unit 930 may replace at least some instructions in the area in which the vulnerability of the target binary 10 occurs, with an instruction to jump to the patch code block. For example, the patch execution unit 930 may write the instruction to jump to the patch code block at an address in which the at least some instructions in the area in which the vulnerability occurred in the target binary 10 may be written. Here, based on a size of a space used for writing the jump instruction being smaller than a size of a space where the replaced some instructions may be written, an appropriate number of NOP instructions may be additionally written. Due to the writing of the NOP instruction, it is possible to prevent a malfunction during execution of the patched binary while minimizing the modification or rewriting of the target binary 10.
Also, the patch execution unit 930 may store the binary 20 to which the patch is applied to the target binary 10 or transmit it to another computing device through a network.
The technical features of the present disclosure described so far may be embodied as computer readable codes on a computer readable medium. The computer readable medium may be, for example, a removable recording medium (CD, DVD, Blu-ray disc, USB storage device, removable hard disk) or a fixed recording medium (ROM, RAM, computer equipped hard disk). The computer program recorded on the computer readable medium may be transmitted to other computing device via a network such as internet and installed in the other computing device, thereby being used in the other computing device.
In the above description, it is described that all the components constituting the embodiments of the present disclosure are combined or operated as one, but the technical features of the present disclosure are not limited to these embodiments. That is, within the scope of the present disclosure, all of the components may be selectively combined and operated in one or more combinations.
Although the operations are shown in an order in the drawings, those skilled in the art will appreciate that many variations and modifications can be made to the embodiments without substantially departing from the principles of the present disclosure. The disclosed embodiments of the present disclosure are used in a generic and descriptive sense and not for purposes of limitation. The scope of protection of the present disclosure should be interpreted by the following claims, and all technical ideas within the scope equivalent thereto should be construed as being included in the scope of the technical idea defined by the present disclosure.
Number | Date | Country | Kind |
---|---|---|---|
10-2019-0144010 | Nov 2019 | KR | national |