Abstract:
A method for preventing malicious attacks on software, using the patching method, includes providing a database of malicious known patches (malware). The database contains characteristic signatures of the malware. The method also includes detecting whether a patch is malicious by comparing it with a signature in the database and performing one or more activities needed to prevent the malicious patch from performing undesired activities.

Description:
CROSS-REFERENCE TO RELATED APPLICATION 
       [0001]    This application is a Continuation-in-Part of U.S. patent application Ser. No. 12/537,893, filed Aug. 7, 2009, which claims benefit of U.S. patent application Ser. No. 61/140,857, filed Dec. 25, 2008 and which applications are incorporated herein by reference. To the extent appropriate, a claim of priority is made to each of the above disclosed applications. 
     
    
     FIELD OF THE INVENTION 
       [0002]    The present invention relates to the detection and/or the prevention of functional patching. 
       BACKGROUND OF THE INVENTION 
       [0003]    Software modules interact by exporting a list of functions (API) by one module, and calling this API by another module. Thus, a program written by vendor A can load a library/module (possibly provided by another vendor, vendor B) that provides services to the main program. Both the program and the library are typically provided to the user in binary form (i.e. without the source code). The user may want to modify the way in which the software works, but the source code may not be available. Still, by knowing the API exported by the library, the user can interject its own code between the main program and the library. This is done by a technique called “patching” (or “hooking”). There are several variants, but the essence is that a user code can be run before, instead or after any function exported by the library (and invoked by the main program). 
         [0004]    The aforementioned patching or hooking technique has many legitimate uses, such as debugging, tweaking (e.g. add security checks, add Y 2 K checks, or other checks), etc. On the other hand, it is a very effective way to maliciously modify the manner in which legitimate browsing software behaves. In such case, malware first infects a user&#39;s PC, and then it modifies the user&#39;s browser (through patching) so that the malware is able to read user data and modify it. 
         [0005]    Another consideration is that, oftentimes, the same function/API is patched by several “players”—some of which may be legitimate, and some not. 
         [0006]    It should be stressed that patching techniques have been widely known for decades, and are applicable to a vast array of operating systems and architectures. For simplicity, for illustrative purposes and without loss of generality, the below text describes patching for Windows 32bit architecture (Win32), but the invention is applicable to many other platforms and architectures. 
         [0007]    There are several well-known ways in which patching can be applied:
       EAT patching: the original function address in the export table (of the exporting module) is replaced with an address for the surrogate function (which may, or may not call the original function). Any module that imports this module after patching is affected.   TAT patching: the original function address in the import table (of the importing module) is replaced with an address for the surrogate function (which may, or may not call the original function). Only the module whose IAT is modified is affected.   In-line patching: a branching instruction is written in the beginning of the function code (the original code is first saved). The target of the branching instruction is the surrogate function. The surrogate function may return control to the caller, or execute the saved instructions and branch back to the original function.       
 
         [0011]    There are benefits and drawbacks to each patching method, which would not be discussed herein in detail for the sake of brevity and which are well known to the skilled person. 
       SUMMARY OF THE INVENTION 
       [0012]    In copending patent application Ser. No. 12/537,893, the full specification of which is incorporated herein by reference, a method is described for preventing malicious attacks on software, using the patching method, comprising the steps of:
       i) providing a database of legitimate and known patches, which database contains characteristic code paths of said legitimate patches;   ii) detecting whether a patch is malicious by inspecting one or more characteristic paths of said patch and matching said one or more code paths against said database of legitimate and known patches; and   iii) making changes to said malicious patch to prevent it from performing undesired activities, or deleting it.       
 
         [0016]    The term “characteristic code path” is meant to indicate any code sequence that can be used as a “fingerprint” to determine the identity of the patch. Such determination must not necessarily be made with absolute certainty in all cases, and situations may arise in which the determination will be made on the basis of reasonable likelihood. It should be noted that the characteristic code path is not merely a concrete sequence of machine code/instructions. Rather, it can often be a complex pattern that needs to be matched. For example: ADD instruction, with two registers, followed by up to 3 instructions, followed by a JMP instruction to the target register of the ADD instruction. 
         [0017]    The present invention is an improvement of the method of said copending patent application Ser. No. 12/537,893 adding to its functionality, and is directed to a method for preventing malicious attacks on software (which will be termed hereinafter “malicious patch method”, for the sake of brevity), using the patching method, comprising the steps of:
       i) providing a database of malicious known patches (malware), which database contains characteristic signatures of said malware;   ii) detecting whether a patch is malicious by comparing it with a signature in said database; and   iii) performing one or more activities selected from making changes to said malicious patch to prevent it from performing undesired activities, deleting it or generating alerting information relative to its presence. Said alerting information can be provided to any interested party, such as the user or a server, or both.       
 
         [0021]    The term “signature” is meant to indicate any element of a code sequence or any transformation thereof that can be used as a “fingerprint” to determine the identity of the patch. Such determination must not necessarily be made with absolute certainty in all cases, and situations may arise in which the determination will be made on the basis of reasonable likelihood. It should be noted that the signature is not merely a concrete sequence of machine code/instructions. Rather, it can often be a complex pattern that needs to be matched. 
         [0022]    This is the reason why, as described above, a virtual machine is a suitable implementation—because each virtual machine describes a pattern. Running the actual data encountered in the field through the machine yields either a match (with the additional output that describe how to linearize the code), or a non-match. 
         [0023]    The characteristic code path inspected can be selected from different option and may include, for instance and without limitation, the pointer to the function in the EAT (Export Address Table), or the pointer to the function in the IAT of the currently loaded modules, or the code of the function itself. 
         [0024]    In an alternative embodiment of the invention the process further comprises detecting a malicious patch and correcting it by making appropriate changes to it, or deleting it. 
         [0025]    In one embodiment of the invention the malicious patch method is not used alone, but in conjunction with the method of U.S. patent application Ser. No. 12/537,893 (referred to hereinafter as the “legitimate patch method”, for the sake of brevity), which, as explained above, comprises the steps of:
       i) providing a database of legitimate and known patches, which database contains characteristic code paths of said legitimate patches;   ii) detecting whether a patch is malicious by inspecting one or more characteristic paths of said patch and matching said one or more code paths against said database of legitimate and known patches; and   iii) making changes to said malicious patch to prevent it from performing undesired activities, or deleting it.       
 
         [0029]    In one embodiment, the method of the invention comprises providing in the database also information regarding where to look for the next patch. 
         [0030]    In another embodiment, the method of the invention comprises providing the database also information on how to “linearize” the code being examined. 
         [0031]    In a further embodiment of the invention the process is repeated until no match is found in the database. 
         [0032]    In one embodiment of the invention the “legitimate patch method” process steps are performed before the “malicious patch method” steps, and in another embodiment of the invention the order is reversed. 
         [0033]    In another embodiment the invention comprises the steps of
       a) performing a quick scan using the known malicious patch signatures; and then   b) performing a deep scan, looking for legitimate patches and identifying unknown or malicious patches that were not identified in step a) above.       
 
         [0036]    The invention, which will be described in detail below, can be used with any software, but the obvious use-case is a browser software. The software may load with many libraries (e.g. DLLs), of which some may require the described protection method. The protection may be applied to all or some of the functions exported by such libraries. 
         [0037]    Thus, for example, if malware patches the function HttpSendResuestA (which resides in the module WinInet.dll), when it is used in Microsoft Internet Explorer (web browser), the malware will be able to monitor all user navigations. A protection may thus be desired for this function, which will remove (or at least detect) the malware patch and thus render the malware ineffective. 
         [0038]    The invention can be used to detect and/or prevent patching to (partial list):
       Functions and code inside programs, libraries, DLLs, etc.   COM objects and other objects, which have virtual function tables   System objects that have pointers to other objects/functions, such as Windows procedures       
 
         [0042]    The invention can be applied to memory (runtime) patching, as well as for disk (or other storage) patching. 
     
    
     DETAILED DESCRIPTION OF THE INVENTION 
       [0043]    The method of invention operates by monitoring a set of data items (pointers, code) that determine the identity of a function invoked. Each such data item is evaluated. For instance, when both the “legitimate patch method” process steps and the “malicious patch method” process steps are performed, it may be determined, for instance, that the patch is part of a chain of legitimate patching, followed by one or more non-legitimate patchings, or by the original function. If a known (non-legitimate) patch (malware) is detected, it can be reported, and/or fixed. When fixing is required, the original data items are simply restored from a genuine copy maintained according to the invention. 
       EXAMPLE 
       [0044]    The method of the invention will now be illustrated in the following use case of memory patching. It will be demonstrated on a single function, but it should be obvious that any number of functions can be thus monitored and/or protected. 
         [0045]    The software that implements the method of the invention will be referred to hereinafter as “PatchSentry”, for the sake of simplicity. PatchSentry consists of several components, and its mode of operation is as follows: 
         [0046]    Triggering an inspection are, in this particular example, one or more of the following partial list:
       Periodic timeout   Activity such as DLL load/unload   Activity such as thread start/exit   Invocation of a function, e.g. invocation of GetProcAddress.       
 
         [0051]    When an inspection is triggered, PatchSentry finds the action required (which can be a function of the monitored function identity, the triggering event, or other factors). An action can be e.g. “report” or “fix”, though other defining combinations and other actions can be devised as well, as will be apparent to the skilled person. 
         [0052]    To first detect whether a malicious patch is present, there are 3 “code paths” inspected:
       The pointer to the function in the EAT (Export Address Table)   The pointer to the function in the IAT of the currently loaded modules   The code of the function itself.       
 
         [0056]    Each code path is matched against a database of signatures of malicious known patches. In the context of this invention the term “signature” should be given the broadest possible definition, and includes, for instance specific code paths or combinations thereof, or the result of any manipulation or calculation made thereon. 
         [0057]    If a match is found the patch can simply be deleted or deactivated in any way or, optionally, the database can also provide information regarding where to look for the next patch (this is important so that PatchSentry can handle a chain of patches), as well as how to “linearize” the underlying code (as explained further below). 
         [0058]    The handling of a chain of patches and its analysis will be explained by way of an illustrative example in which no malicious patch is found. Assuming that a user has an anti-virus and the Google toolbar installed, both which patch some function X, wherein the anti-virus is the first to patch. The execution path would be as follows: 
         [0000]    First the Google toolbar (since it patched last, i.e. is at the top of the queue), then the anti-virus, and finally the original function code.
 
In this example PatchSentry is installed and has both Google Toolbar and the particular anti-virus in its database. Then the following events take place:
 
         [0059]    1) PatchSentry inspects function X. It detects that it&#39;s patched. It detects that the patch is Google Toolbar because it matches the Google Toolbar patching style (this is an assumption that PatchSentry makes, which is that it can determine the “owner” of the patch by its patching style. In practical systems this assumption holds true in most cases). Then it compares the signature of this patch with those found in its database of malicious patches and, if no match is found, it continues. 
         [0060]    2) The information available from the Google Toolbar that PatchSentry has in this example contains more than just information on how to detect the patching style of Google Toolbar—it also contains information that enables PatchSentry to virtually reconstruct the underlying function code, as if Google Toolbar never patched the function. Armed with this information, PatchSentry now inspects the function, and detects another patch. 
         [0061]    3) PatchSentry now again consults its database and looks for a matching signature in it. And again PatchSentry “peels off” this patch to reveal the underlying function code. 
         [0062]    4) Since the anti-virus is the bottom-most patch, there are no further patches. Therefore, PatchSentry detects that the function code is now identical to the original function (the one that can be found in the DLL image on disk). 
         [0063]    The result of the process described above is that PatchSentry now knows the chain of patches, which is: Google Toolbar→anti-virus→original function and knows that no malicious patches are present. 
         [0064]    As explained above, the process depends on PatchSentry having a signature for all malicious patches it will encounter in the field, in this case those purporting to be the Google Toolbar and the anti-virus. 
         [0065]    After a plurality of non-malicious patches are sequentially analyzed, the process reaches one of the following: 1) the genuine function code; or 2) a known malicious patch for which a signature is found in the data base; or 3) an unknown patch. In the latter case a decision must be made as to whether to treat said unknown patch as malicious or legitimate, and suitable criteria may be provided for making such decision. 
         [0066]    In “fix” mode (which is one of the modes of operation of the methods of the invention), the IAT pointer, EAT pointer and code prefix are restored to their original values. Of course, another strategy can be, for example, to undo the malicious patch so that the original chain remains intact. The following example will illustrate these strategies. 
         [0067]    Assume the following chain:
       Google Toolbar→anti-virus→malware→original function       
 
         [0069]    The first strategy is to drop the whole chain, so that the resulting situation is as follows:
       original function       
 
         [0071]    Whereas the second strategy is to carve out the malware patch, so that the resulting situation would be:
       Google Toolbar→anti-virus→original function       
 
         [0073]    Naturally the second strategy is better (because Google Toolbar and the anti-virus continue to function as usual), but technically it&#39;s much harder to accomplish. Since for practical purposes the first solution is acceptable and more easily performed, the invention will often opt to adopt it as the chosen solution in “fix mode”. 
         [0074]    As will be apparent to the skilled person, the process of matching a given code path to a database of known signatures of malwares can be implemented in various ways known per se in the art and therefore not described herein, for the sake of brevity. One implementation particularly suited for this task is to have the database contain “virtual machines” describing each known patch. The process of matching becomes running each virtual machine with the code path as input. The virtual machine&#39;s output can be a binary decision (does the code path given match the patch the virtual machine describes) and properties of the underlying code, if a match is found. These properties can be, for example, the address of the next patch, and instructions for linearization of the underlying code. 
         [0075]    It should be understood that the need for linearization stems from a common patching technique called “trampoline”. In this technique, patching the function code is done by copying the first few instructions in the function body into a buffer, followed by a branch back to the location in the original function just after those instructions. Then the first few instructions are overwritten with a branch to the surrogate function. Thus, when the function is called, execution is immediately transferred to the surrogate function, which runs its own code. If it needs to transfer execution back to the original function, it simply branches to the buffer, which executes the first few instructions in the original function and branches into it for continuation. 
         [0076]    When such patch is encountered, the underlying code is now non-linear. In order to derive a signature, in some cases the underlying code should undergo “linearization”, that is, it should be presented as a continuous code, although its memory layout is not continuous (its first part is in the buffer, and its second part is an offset into the original location). When the system matches a patch (which is known to use the trampoline technique), it needs to provide its caller with instruction re how to “linearize” the underlying code, e.g. where to pick from the various parts of the code and how to assemble them together so that they represent the linear version of the underlying code. 
         [0077]    In “fix mode”, when replacing the function&#39;s code prefix, care should be taken to avoid a situation in which execution occurs in the code region that is being overwritten. One approach to doing so in the Win32 environment is by first replacing the first byte of the fixed function with a privileged instruction and registering a vectored exception handler that detects attempts to execute this instruction; when an attempt to execute the instruction is detected, it is stalled until the replacement operation completes. The code replacement, on the other hand, is first done for code bytes following the first byte, and only then is the first byte overwritten (followed by removing the vectored exception handler registration). 
         [0078]    As will be appreciated by the skilled person, combining the “legitimate patch method” with the “malicious patch method” provides important advantages. For instance, when an unknown patch is encountered while using the “legitimate patch method” it can be examined using the “malicious patch method”. If the patch found is known, there is no need to proceed to handle it as an unknown and potentially harmful patch, but rather corrective action can be taken based on the available information. Such corrective action may include, for instance, uninstalling the patch or advising the user regarding actions that should or should not be taken in light of its existence. 
         [0079]    All the above description has been given for the purpose of illustration and is not meant to limit the invention in any way, except as defined in the claims to follow.