\n

The Ocean Lotus group, also known as APT32, is a threat actor which has been known to target East Asian countries such as Vietnam, Laos and the Philippines. The group strongly focuses on Vietnam, especially private sector companies that are investing in a wide variety of industrial sectors in the country. While private sector companies are the group’s main targets, APT32 has also been known to target foreign governments, dissidents, activists, and journalists.

\n\n\n\n

APT32’s toolset is wide and varied. It contains both advanced and simple components; it is a mixture of handcrafted tools and commercial or open-source ones, such as Mimikatz and Cobalt Strike. It runs the gamut from droppers, shellcode snippets, through decoy documents and backdoors. Many of these tools are highly obfuscated and seasoned, augmented with different techniques to make them harder to reverse-engineer.

\n\n\n\n

In this article, we get up and close with one of these obfuscation techniques. This specific technique was used in a backdoor of Ocean Lotus’ tool collection. We’ll describe the technique and the difficulty it presents to analysts — and then show how bypassing this kind of technique is a matter of writing a simple script, as long as you know what you are doing.

\n\n\n\n

The deobfuscation plugin requires Cutter, the official GUI of the open-source reverse engineering framework – radare2. Cutter is a cross-platform GUI that aims to expose radare2’s functionality as a user-friendly and modern interface.  Last month, Cutter introduced a new Python plugin system, which figures into the tool we’ll be constructing below. The plugin itself isn’t complicated, and neither is the solution we demonstrate below. If simple works, then simple is best.

\n

 

\n\n\n\n

Downloading and installing Cutter

\n\n\n\n

Cutter is available for all platforms (Linux, OS X, Windows). You can download the latest release here. If you are using Linux, the fastest way to get a working copy of Cutter is to use the AppImage file.

\n\n\n\n

If you want to use the newest version available, with new features and bug fixes, you should build Cutter from source. If you are up for that detour, follow this tutorial.

\n

\"\"

\n\n\n\n

Fig 1: Cutter interface

\n\n\n\n

 

\n

The Backdoor

\n\n\n\n

First, let’s have a look at the backdoor itself. The relevant sample (486be6b1ec73d98fdd3999abe2fa04368933a2ec) is part of a multi-stage infection chain, which we have lately seen employed in the wild. All these stages are quite typical for Ocean Lotus, especially the chain origin being a malicious document (115f3cb5bdfb2ffe5168ecb36b9aed54). The document purports to originate from Chinese security vendor Qihoo 360, and contains a malicious VBA Macro code that injects a malicious shellcode to rundll32.exe. The shellcode contains decryption routines to decrypt and reflectively load a DLL file to the memory. The DLL contains the backdoor logic itself.

\n\n\n\n

First, the backdoor decrypts a configuration file which is pulled from the file resource. The configuration file stores information such as the Command and Control servers. The binary then tries to load an auxiliary DLL to the memory using a custom-made PE loader. This DLL is called HTTPProv.dll and is capable of communicating with the C2 servers. The backdoor can receive dozens of different commands from the Command and Control servers, including shellcode execution, creation of new processes, manipulation of files and directories, and more.

\n\n\n\n

Many obfuscation techniques are used by Ocean Lotus in order to make their tools harder to reverse engineer. Most noticeable, Ocean Lotus is using an enormous amount of junk code in their binaries. The junk code makes the samples much bigger and more complicated, which distracts researchers trying to pry into the binary. Trying to decompile some of these obfuscated functions is a lost cause; the assembly often plays around with the stack pointer, and decompilers are not well-equipped to handle this kind of pathological code.

\n

 

\n\n\n\n

The Obfuscation

\n\n\n\n

Upon analysis of the backdoor, one obfuscation technique can be immediately noticed. It is the heavy use of control flow obfuscation which is created by inserting junk blocks into the flow of the function. These junk blocks are just meaningless noise and make the flow of the function confusing.

\n\n\n\n

\"\"

\n

Fig 2: An example of a junk block

\n

 

\n\n\n\n

As you can see in the image above, the block is full of junk code which has nothing to do with what the function actually does. It’s best to ignore these blocks, but that’s easier said than done. A closer look at these blocks will reveal something interesting. These junk blocks are always being fail-jumped to by a conditional jump from a previous block. Furthermore, these junk blocks will almost always end with a conditional jump which is the opposite of the conditional jump of the previous block. For example, if the condition above the junk block was jo <some_addr>, the junk block will most likely end with jno <some_addr>. If the block above ended with jne <another_addr>, the junk block will then end with… you guessed right – je <another_addr>.

\n\n\n\n

\"\"

\n

Fig 3: Opposite conditional jumps

\n

 

\n\n\n\n

With this in mind, we can begin structuring the characteristics of these junk blocks. The first characteristic of the obfuscation is the occurrence of two successive blocks which end with opposite conditional jumps to the same target address. The other characteristic requires the second block to contain no meaningful instructions such as string references or calls.

\n\n\n\n

When these two characteristics are met, we can say with a high chance that the second block is a junk block. In such a case, we would want the first block to jump over the junk block so the junk block would be removed from the graph. This can be done by patching the conditional jump with an unconditional jump, aka a simple JMP instruction.

\n

\"\"

\n\n\n\n

Fig 4: Modifying the conditional jump to a JMP instruction will ignore the junk block

\n\n\n\n

 

\n

Writing the Plugin

\n\n\n\n

So here is a heads up for you – the plugin we present below is written for Cutter, but was designed to be compatible with radare2 scripts, for those of you who are CLI gurus. That means that we are going to use some nifty radare2 commands through r2pipe – a Python wrapper to interact with radare2. This is the most effective and flexible way for scripting radare2.

\n\n\n\n

It’s not trivial to get the plugin to support both Cutter and radare2, since one is a GUI program and the other is a CLI. That means that GUI objects would be meaningless inside radare2. Luckily, Cutter supports r2pipe and is able to execute radare2 commands from inside its Python plugins.

\n\n\n\n

 

\n

Writing the Core Class

\n\n\n\n

The first thing we are going to do is to create a Python class which will be our core class. This class will contain our logic for finding and removing the junk blocks. Let’s start by defining its __init__ function. The function will receive a pipe, which will be either an r2pipe (available from import r2pipe) object from radare2 or a cutter (available from import cutter) object from Cutter.

\n\n
class GraphDeobfuscator:\n   def __init__(self, pipe):\n       \"\"\"an initialization function for the class\n      \n       Arguments:\n           pipe {r2pipe} -- an instance of r2pipe or Cutter's wrapper\n       \"\"\"\n\n       self.pipe = pipe\n
\n\n

 

\n

Now we can execute radare2 commands using this pipe. The pipe object contains two major ways to execute r2 commands. The first is pipe.cmd(<command>) which will return the results of the command as a string, and the second is pipe.cmdj(<command>j) which will return a parsed JSON object from the output of radare2’s command.

\n\n\n\n
Note: Almost every command of radare2 can be appended with a j to get the output as JSON.
\n\n\n\n

 

\n

The next thing we would want to do is to get all the blocks of the current function and then iterate over each one of them. We can do this by using the afbj command which stands for Analyze Function Blocks and will return a Json object with all the blocks of the function.

\n\n
   def clean_junk_blocks(self):\n       \"\"\"Search a given function for junk blocks, remove them and fix the flow.\n       \"\"\"\n       # Get all the basic blocks of the function\n       blocks = self.pipe.cmdj(\"afbj @ $F\")\n       if not blocks:\n           print(\"[X] No blocks found. Is it a function?\")\n           return\n       modified = False\n\n       # Iterate over all the basic blocks of the function\n       for block in blocks:\n           # do something
\n\n

 

\n

For each block, we want to know if there is a block which fails-to in a case where the conditional jump would not take place. If a block has a block to which it fails, the second block is an initial candidate to be a junk block.

\n\n
   def get_fail_block(self, block):\n       \"\"\"Return the block to which a block branches if the condition is fails\n      \n       Arguments:\n           block {block_context} -- A JSON representation of a block\n      \n       Returns:\n           block_context -- The block to which the branch fails. If not exists, returns None\n       \"\"\"\n       # Get the address of the \"fail\" branch\n       fail_addr = self.get_fail(block)\n       if not fail_addr:\n           return None\n       # Get a block context of the fail address\n       fail_block = self.get_block(fail_addr)\n       return fail_block if fail_block else None
\n\n
Note: Since our space is limited, we won’t explain every function that appears here. Functions as get_block (addr) or get_fail_addr (block) that are used in the snippet above are subroutines we wrote to make the code cleaner. The function implementations will be available in the final plugin that is shown and linked at the end of the article. Hopefully, you’ll find the function names self-explanatory.
\n\n\n\n

 

\n

Next, we would like to check whether our junk block candidate comes immediately after the block. If no, this is most likely not a junk block since from what we inspected, junk blocks are located in the code immediately after the blocks with the conditional jump.

\n\n
   def is_successive_fail(self, block_A, block_B):\n       \"\"\"Check if the end address of block_A is the start of block_B\n\n       Arguments:\n           block_A {block_context} -- A JSON object to represent the first block\n           block_B {block_context} -- A JSON object to represent the second block\n      \n       Returns:\n           bool -- True if block_B comes immediately after block_A, False otherwise\n       \"\"\"\n\n      return ((block_A[\"addr\"] + block_A[\"size\"]) == block_B[\"addr\"])
\n\n

 

\n

Then, we would want to check whether the block candidate contains no meaningful instructions. For example, it is unlikely that a junk block will contain CALL instructions or references for strings. To do this, we will use the command pdsb which stands for Print Disassembly Summary of a Block. This radare2 command prints the interesting instructions that appear in a certain block. We assume that a junk block would not contain interesting instructions.

\n\n
   def contains_meaningful_instructions (self, block):\n       '''Check if a block contains meaningful instructions (references, calls, strings,...)\n      \n       Arguments:\n           block {block_context} -- A JSON object which represents a block\n      \n       Returns:\n           bool -- True if the block contains meaningful instructions, False otherwise\n       '''\n\n       # Get summary of block - strings, calls, references\n       summary = self.pipe.cmd(\"pdsb @ {addr}\".format(addr=block[\"addr\"]))\n       return summary != \"\"\n
\n\n

 

\n

Last, we would like to check whether the conditional jumps of both blocks are opposite. This will be the last piece of the puzzle to determine whether we are dealing with a junk block. For this, we would need to create a list of opposite conditional jumps. The list we’ll show is partial since the x86 architecture contains many conditional jump instructions. That said, from our tests, it seems like the below list is enough to cover all the different pairs of opposite conditional jumps that are presented in APT32’s backdoor. If it doesn’t, adding additional instructions is easy.

\n\n
   jmp_pairs = [\n       ['jno', 'jo'],\n       ['jnp', 'jp'],\n       ['jb',  'jnb'],\n       ['jl',  'jnl'],\n       ['je',  'jne'],\n       ['jns', 'js'],\n       ['jnz', 'jz'],\n       ['jc',  'jnc'],\n       ['ja', 'jbe'],\n       ['jae', 'jb'],\n       ['je',  'jnz'],\n       ['jg',  'jle'],\n       ['jge', 'jl'],\n       ['jpe', 'jpo'],\n       ['jne', 'jz']]\n\n   def is_opposite_conditional(self, cond_A, cond_B):\n       \"\"\"Check if two operands are opposite conditional jump operands\n      \n       Arguments:\n           cond_A {string} -- the conditional jump operand of the first block\n           cond_B {string} -- the conditional jump operand of the second block\n      \n       Returns:\n           bool -- True if the operands are opposite, False otherwise\n       \"\"\"\n\n       sorted_pair = sorted([cond_A, cond_B])\n       for pair in self.jmp_pairs:\n           if sorted_pair == pair:\n               return True\n       return False\n
\n\n

 

\n

Now that we defined the validation functions, we can glue these parts together inside the clean_junk_blocks() function we created earlier.

\n\n
   def clean_junk_blocks(self):\n       \"\"\"Search a given function for junk blocks, remove them and fix the flow.\n       \"\"\"\n\n       # Get all the basic blocks of the function\n       blocks = self.pipe.cmdj(\"afbj @ $F\")\n       if not blocks:\n           print(\"[X] No blocks found. Is it a function?\")\n           return\n       modified = False\n\n       # Iterate over all the basic blocks of the function\n       for block in blocks:\n           fail_block = self.get_fail_block(block)\n           if not fail_block or \\\n           not self.is_successive_fail(block, fail_block) or \\\n           self.contains_meaningful_instructions(fail_block) or \\\n           not self.is_opposite_conditional(self.get_last_mnem_of_block(block), self.get_last_mnem_of_block(fail_block)):\n               continue\n
\n\n

 

\n

In case that all the checks are successfully passed, and we can say with a high chance that we found a junk block, we would want to patch the first conditional jump to JMP over the junk block, hence removing the junk block from the graph and thus, from the function itself.

\n\n\n\n

To do this, we use two radare2 commands. The first is aoj @ <addr> which stands for Analyze Opcode and will give us information on the instruction in a given address. This command can be used to get the target address of the conditional jump. The second command we use is wai <instruction> @ <addr> which stands for Write Assembly Inside. Unlike the wa <instruction> @ <addr> command to overwrite an instruction with another instruction, the wai command will fill the remaining bytes with NOP instructions. Thus, in a case where the JMP <addr> instruction we want to use is shorter than the current conditional-jump instruction, the remaining bytes will be replaced with NOPs.

\n\n
   def overwrite_instruction(self, addr):\n       \"\"\"Overwrite a conditional jump to an address, with a JMP to it\n      \n       Arguments:\n           addr {addr} -- address of an instruction to be overwritten\n       \"\"\"\n\n       jump_destination = self.get_jump(self.pipe.cmdj(\"aoj @ {addr}\".format(addr=addr))[0])\n       if (jump_destination):\n           self.pipe.cmd(\"wai jmp 0x{dest:x} @ {addr}\".format(dest=jump_destination, addr=addr))\n
\n\n

 

\n

After overwriting the conditional-jump instruction, we continue to loop over all the blocks of the function and repeat the steps described above. Last, if changes were made in the function, we re-analyze the function so that the changes we made appear in the function graph.

\n\n
   def reanalize_function(self):\n       \"\"\"Re-Analyze a function at a given address\n      \n       Arguments:\n           addr {addr} -- an address of a function to be re-analyze\n       \"\"\"\n       # Seek to the function's start\n       self.pipe.cmd(\"s $F\")\n       # Undefine the function in this address\n       self.pipe.cmd(\"af- $\")\n\n       # Define and analyze a function in this address\n       self.pipe.cmd(\"afr @ $\")\n
\n\n

 

\n

At last, the clean_junk_blocks() function is now ready to be used. We can now also create a function, clean_graph(), that cleans the obfuscated function of the backdoor.

\n\n
   def clean_junk_blocks(self):\n       \"\"\"Search a given function for junk blocks, remove them and fix the flow.\n       \"\"\"\n\n       # Get all the basic blocks of the function\n       blocks = self.pipe.cmdj(\"afbj @ $F\")\n       if not blocks:\n           print(\"[X] No blocks found. Is it a function?\")\n           return\n       # Have we modified any instruction in the function?\n       # If so, a reanalyze of the function is required\n       modified = False\n\n       # Iterate over all the basic blocks of the function\n       for block in blocks:\n           fail_block = self.get_fail_block(block)\n           # Make validation checks\n           if not fail_block or \\\n           not self.is_successive_fail(block, fail_block) or \\\n           self.contains_meaningful_instructions(fail_block) or \\\n           not self.is_opposite_conditional(self.get_last_mnem_of_block(block), self.get_last_mnem_of_block(fail_block)):\n               continue\n           self.overwrite_instruction(self.get_block_end(block))\n           modified = True\n       if modified:\n           self.reanalize_function()\n   \n\n   def clean_graph(self):\n       \"\"\"the initial function of the class. Responsible to enable cache and start the cleaning\n       \"\"\"\n\n       # Enable cache writing mode. changes will only take place in the session and\n       # will not override the binary\n       self.pipe.cmd(\"e io.cache=true\")\n       self.clean_junk_blocks()\n
\n\n

This concludes the core class.

\n\n\n\n

 

\n

Cutter or Radare2?

\n\n\n\n

As mentioned earlier, our code will be executed either as a plugin for Cutter, or straight from the radare2 CLI as a Python script. That means that we need to have a way to understand whether our code is being executed from Cutter or from radare2. For this, we can use the following simple trick.

\n\n
# Check if we're running from cutter\ntry:\n   import cutter\n   from PySide2.QtWidgets import QAction\n   pipe = cutter\n   cutter_available = True\n# If no, assume running from radare2\nexcept:\n   import r2pipe\n   pipe = r2pipe.open()\n   cutter_available = False\n
\n\n

The code above checks whether the cutter library can be imported. If it can, we are running from inside Cutter and can feel safe to do some GUI magic. Otherwise, we’re running from inside radare2, and so we opt to import r2pipe. In both statements, we are assigning a variable named pipe which will be later passed to the GraphDeobfuscator class we created.

\n\n\n\n

 

\n

Running from Radare2

\n\n\n\n

This is the simplest way to use this plugin. Checking if __name__ equals “__main__” is a common Python idiom that checks if the script was run directly or imported. If this script was run directly, we simply execute the clean_graph() function.

\n\n
if __name__ == \"__main__\":\n   graph_deobfuscator = GraphDeobfuscator(pipe)\n   graph_deobfuscator.clean_graph()\n
\n\n

 

\n

Running from Cutter

\n\n\n\n

Cutter’s documentation describes how to go about building and executing a Plugin for Cutter, and we follow its lead. First, we need to make sure that we are running from inside Cutter. We already created a boolean variable named cutter_variable. We simply need to check whether this variable is set to True. If it does, we proceed to define our plugin class.

\n\n
if cutter_available:\n   # This part will be executed only if Cutter is available.\n   # This will create the cutter plugin and UI objects for the plugin\n   class GraphDeobfuscatorCutter(cutter.CutterPlugin):\n       name = \"APT32 Graph Deobfuscator\"\n       description = \"Graph Deobfuscator for APT32 Samples\"\n       version = \"1.0\"\n       author = \"Itay Cohen (@Megabeets_)\"\n\n       def setupPlugin(self):\n           pass\n\n       def setupInterface(self, main):\n           pass\n   \n   def create_cutter_plugin():\n       return GraphDeobfuscatorCutter()\n
\n\n

 

\n

This is a skeleton of a Cutter plugin — it contains no proper functionality at all. The function create_cutter_plugin() is called by Cutter upon loading. At this point, if we will place our script in Cutter’s plugins directory, Cutter will recognize our file as a plugin.

\n\n\n\n

To make the plugin execute our functionality, we need to add a menu entry that the user can press to trigger our deobfuscator. We chose to add a menu entry, or an Action, to the “Windows -> Plugins” menu.

\n\n
if cutter_available:\n   # This part will be executed only if Cutter is available. This will\n   # create the cutter plugin and UI objects for the plugin\n   class GraphDeobfuscatorCutter(cutter.CutterPlugin):\n       name = \"APT32 Graph Deobfuscator\"\n       description = \"Graph Deobfuscator for APT32 Samples\"\n       version = \"1.0\"\n       author = \"Megabeets\"\n\n       def setupPlugin(self):\n           pass\n\n       def setupInterface(self, main):\n           # Create a new action (menu item)\n           action = QAction(\"APT32 Graph Deobfuscator\", main)\n           action.setCheckable(False)\n           # Connect the action to a function - cleaner.\n           # A click on this action will trigger the function\n           action.triggered.connect(self.cleaner)\n\n           # Add the action to the \"Windows -> Plugins\" menu\n           pluginsMenu = main.getMenuByType(main.MenuType.Plugins)\n           pluginsMenu.addAction(action)\n\n       def cleaner(self):\n           graph_deobfuscator = GraphDeobfuscator(pipe)\n           graph_deobfuscator.clean_graph()\n           cutter.refresh()\n\n\n   def create_cutter_plugin():\n       return GraphDeobfuscatorCutter()\n
\n\n

 

\n

The script is now ready, and can be placed in the Python folder, under Cutter’s plugins directory. The path to the directory is shown in the Plugins Options, under “Edit -> Preferences -> Plugins“. For example, on our machine the path is: “~/.local/share/RadareOrg/Cutter/Plugins/Python“.

\n\n\n\n

Now, when opening Cutter, we can see in “Plugins -> Preferences” that the plugin was indeed loaded.

\n

\"\"

\n\n\n\n

Fig 5: The plugin was successfully loaded

\n

 

\n\n\n\n

We can also check the “Windows -> Plugins” menu to see if the menu item we created is there. And indeed, we can see that the “APT32 Graph Deobfuscator” item now appears in the menu.

\n\n\n\n

\"\"

\n

Fig 6: The menu item we created was successfully added

\n

 

\n\n\n\n

We can now choose some function which we suspect for having these junk blocks, and try to test our Plugin. In this example, We chose the function fcn.00acc7e0. Going to a function in Cutter can be done either by selecting it from the left menu, or simply pressing “g” and typing its name or address in the navigation bar.

\n\n\n\n

Make sure you are in the graph view. Feel free to wander around and try to spot the junk blocks. We highlighted them in the image below which shows the Graph Overview (mini-graph)  window.

\n

\"\"

\n\n\n\n

Fig 7: Junk block highlighted in fcn.00acc7e0

\n\n\n\n

Since we have a candidate suspicious function, we can trigger our plugin and see if it successfully removes them. To do this, click on “Windows -> Plugins -> APT32 Graph Deobfuscator“. After a second, we can see that our plugin successfully removed the junk blocks.

\n

\"\"

\n\n\n\n

Fig 8: The same function after removing the junk blocks

\n

 

\n\n\n\n

In the following images, you can see more pairs of function graphs before and after the removal of junk blocks.

\n

\"\"

\n\n\n\n

Fig 9: Before and After of fcn.00aa07b0

\n

\"\"

\n\n\n\n

Fig 10: Before and After of fcn.00a8a1a0

\n\n\n\n

 

\n

Final Words

\n\n\n\n

Ocean Lotus’ obfuscation techniques are in no way the most complicated or hard to beat. In this article we went through understanding the problem, drafting a solution and finally implementing it using the python scripting capabilities of Cutter and Radare2. The full script can be found in our Github repository, and also attached to the bottom of this article.

\n\n\n\n

If you are interested in reading more about Ocean Lotus, we recommend this high-quality article published by ESET’s Romain Dumont. It contains a thorough analysis of Ocean Lotus’ tools, as well as some exposition of the obfuscation techniques involved.

\n\n\n\n

 

\n

Appendix

\n\n\n\n

Sample SHA-256 values

\n\n\n\n\n\n\n\n

 

\n

APT32 Graph Deobfuscator – Full Code

\n\n
\"\"\" A plugin for Cutter and Radare2 to deobfuscate APT32 flow graphs\nThis is a python plugin for Cutter that is compatible as an r2pipe script for\nradare2 as well. The plugin will help reverse engineers to deobfuscate and remove\njunk blocks from APT32 (Ocean Lotus) samples.\n\"\"\"\n\n__author__  = \"Itay Cohen, aka @megabeets_\"\n__company__ = \"Check Point Software Technologies Ltd\"\n\n# Check if we're running from cutter\ntry:\n    import cutter\n    from PySide2.QtWidgets import QAction\n    pipe = cutter\n    cutter_available = True\n# If no, assume running from radare2\nexcept:\n    import r2pipe\n    pipe = r2pipe.open()\n    cutter_available = False\n\n\nclass GraphDeobfuscator:\n    # A list of pairs of opposite conditional jumps\n    jmp_pairs = [\n        ['jno', 'jo'],\n        ['jnp', 'jp'],\n        ['jb',\t'jnb'],\n        ['jl',\t'jnl'],\n        ['je',\t'jne'],\n        ['jns', 'js'],\n        ['jnz', 'jz'],\n        ['jc',\t'jnc'],\n        ['ja', 'jbe'],\n        ['jae', 'jb'],\n        ['je',\t'jnz'],\n        ['jg',  'jle'],\n        ['jge', 'jl'],\n        ['jpe', 'jpo'],\n        ['jne', 'jz']]\n\n    def __init__(self, pipe, verbose=False):\n        \"\"\"an initialization function for the class\n        \n        Arguments:\n            pipe {r2pipe} -- an instance of r2pipe or Cutter's wrapper\n        \n        Keyword Arguments:\n            verbose {bool} -- if True will print logs to the screen (default: {False})\n        \"\"\"\n\n        self.pipe = pipe\n\n        self.verbose = verbose\n\n    def is_successive_fail(self, block_A, block_B):\n        \"\"\"Check if the end address of block_A is the start of block_B\n\n        Arguments:\n            block_A {block_context} -- A JSON object to represent the first block\n            block_B {block_context} -- A JSON object to represent the second block\n        \n        Returns:\n            bool -- True if block_B comes immediately after block_A, False otherwise\n        \"\"\"\n\n        return ((block_A[\"addr\"] + block_A[\"size\"]) == block_B[\"addr\"])\n\n    def is_opposite_conditional(self, cond_A, cond_B):\n        \"\"\"Check if two operands are opposite conditional jump operands\n        \n        Arguments:\n            cond_A {string} -- the conditional jump operand of the first block\n            cond_B {string} -- the conditional jump operand of the second block\n\n        Returns:\n            bool -- True if the operands are opposite, False otherwise\n        \"\"\"\n\n        sorted_pair = sorted([cond_A, cond_B])\n        for pair in self.jmp_pairs:\n            if sorted_pair == pair:\n                return True\n        return False\n\n    def contains_meaningful_instructions (self, block):\n        '''Check if a block contains meaningful instructions (references, calls, strings,...)\n        \n        Arguments:\n            block {block_context} -- A JSON object which represents a block\n        \n        Returns:\n            bool -- True if the block contains meaningful instructions, False otherwise\n        '''\n\n        # Get summary of block - strings, calls, references\n        summary = self.pipe.cmd(\"pdsb @ {addr}\".format(addr=block[\"addr\"]))\n        return summary != \"\"\n\n    def get_block_end(self, block):\n        \"\"\"Get the address of the last instruction in a given block\n        \n        Arguments:\n            block {block_context} -- A JSON object which represents a block\n        \n        Returns:\n            The address of the last instruction in the block\n        \"\"\"\n\n        # save current seek\n        self.pipe.cmd(\"s {addr}\".format(addr=block['addr']))\n        # This will return the address of a block's last instruction\n        block_end = self.pipe.cmd(\"?v $ @B:-1\")\n        return block_end\n\n    def get_last_mnem_of_block(self, block):\n        \"\"\"Get the mnemonic of the last instruction in a block\n        \n        Arguments:\n            block {block_context} -- A JSON object which represents a block\n        \n        Returns:\n            string -- the mnemonic of the last instruction in the given block\n        \"\"\"\n\n        inst_info = self.pipe.cmdj(\"aoj @ {addr}\".format(addr=self.get_block_end(block)))[0]\n        return inst_info[\"mnemonic\"]\n\n    def get_jump(self, block):\n        \"\"\"Get the address to which a block jumps\n        \n        Arguments:\n            block {block_context} -- A JSON object which represents a block\n        \n        Returns:\n            addr -- the address to which the block jumps to. If such address doesn't exist, returns False \n        \"\"\"\n\n        return block[\"jump\"] if \"jump\" in block else None\n\n    def get_fail_addr(self, block):\n        \"\"\"Get the address to which a block fails\n        \n        Arguments:\n            block {block_context} -- A JSON object which represents a block\n        \n        Returns:\n            addr -- the address to which the block fail-branches to. If such address doesn't exist, returns False \n        \"\"\"\n        return block[\"fail\"] if \"fail\" in block else None\n\n    def get_block(self, addr):\n        \"\"\"Get the block context in a given address\n        \n        Arguments:\n            addr {addr} -- An address in a block\n        \n        Returns:\n            block_context -- the block to which the address belongs\n        \"\"\"\n\n        block = self.pipe.cmdj(\"abj. @ {offset}\".format(offset=addr))\n        return block[0] if block else None\n\n    def get_fail_block(self, block):\n        \"\"\"Return the block to which a block branches if the condition is fails\n        \n        Arguments:\n            block {block_context} -- A JSON representation of a block\n        \n        Returns:\n            block_context -- The block to which the branch fails. If not exists, returns None\n        \"\"\"\n        # Get the address of the \"fail\" branch\n        fail_addr = self.get_fail_addr(block)\n        if not fail_addr:\n            return None\n        # Get a block context of the fail address\n        fail_block = self.get_block(fail_addr)\n        return fail_block if fail_block else None\n\n    def reanalize_function(self):\n        \"\"\"Re-Analyze a function at a given address\n        \n        Arguments:\n            addr {addr} -- an address of a function to be re-analyze\n        \"\"\"\n        # Seek to the function's start\n        self.pipe.cmd(\"s $F\")\n        # Undefine the function in this address\n        self.pipe.cmd(\"af- $\")\n\n        # Define and analyze a function in this address\n        self.pipe.cmd(\"afr @ $\")       \n\n    def overwrite_instruction(self, addr):\n        \"\"\"Overwrite a conditional jump to an address, with a JMP to it\n        \n        Arguments:\n            addr {addr} -- address of an instruction to be overwritten\n        \"\"\"\n\n        jump_destination = self.get_jump(self.pipe.cmdj(\"aoj @ {addr}\".format(addr=addr))[0])\n        if (jump_destination):\n            self.pipe.cmd(\"wai jmp 0x{dest:x} @ {addr}\".format(dest=jump_destination, addr=addr))\n\n    def get_current_function(self):\n        \"\"\"Return the start address of the current function\n\n        Return Value:\n            The address of the current function. None if no function found.\n        \"\"\"\n        function_start = int(self.pipe.cmd(\"?vi $FB\"))\n        return function_start if function_start != 0 else None\n\n    def clean_junk_blocks(self):\n        \"\"\"Search a given function for junk blocks, remove them and fix the flow.\n        \"\"\"\n\n        # Get all the basic blocks of the function\n        blocks = self.pipe.cmdj(\"afbj @ $F\")\n        if not blocks:\n            print(\"[X] No blocks found. Is it a function?\")\n            return\n        # Have we modified any instruction in the function?\n        # If so, a reanalyze of the function is required\n        modified = False\n\n        # Iterate over all the basic blocks of the function\n        for block in blocks:\n            fail_block = self.get_fail_block(block)\n            # Make validation checks\n            if not fail_block or \\\n            not self.is_successive_fail(block, fail_block) or \\\n            self.contains_meaningful_instructions(fail_block) or \\\n            not self.is_opposite_conditional(self.get_last_mnem_of_block(block), self.get_last_mnem_of_block(fail_block)):\n                continue\n            if self.verbose:\n                print (\"Potential junk: 0x{junk_block:x} (0x{fix_block:x})\".format(junk_block=fail_block[\"addr\"], fix_block=block[\"addr\"]))\n            self.overwrite_instruction(self.get_block_end(block))\n            modified = True\n        if modified:\n            self.reanalize_function()\n        \n    def clean_graph(self):\n        \"\"\"the initial function of the class. Responsible to enable cache and start the cleaning\n        \"\"\"\n\n        # Enable cache writing mode. changes will only take place in the session and\n        # will not override the binary\n        self.pipe.cmd(\"e io.cache=true\")\n        self.clean_junk_blocks()\n        \n\nif cutter_available:\n    # This part will be executed only if Cutter is available. This will\n    # create the cutter plugin and UI objects for the plugin\n    class GraphDeobfuscatorCutter(cutter.CutterPlugin):\n        name = \"APT32 Graph Deobfuscator\"\n        description = \"Graph Deobfuscator for APT32 Samples\"\n        version = \"1.0\"\n        author = \"Itay Cohen (@Megabeets_)\"\n\n        def setupPlugin(self):\n            pass\n\n        def setupInterface(self, main):\n            # Create a new action (menu item)\n            action = QAction(\"APT32 Graph Deobfuscator\", main)\n            action.setCheckable(False)\n            # Connect the action to a function - cleaner.\n            # A click on this action will trigger the function\n            action.triggered.connect(self.cleaner)\n\n            # Add the action to the \"Windows -> Plugins\" menu\n            pluginsMenu = main.getMenuByType(main.MenuType.Plugins)\n            pluginsMenu.addAction(action)\n\n        def cleaner(self):\n            graph_deobfuscator = GraphDeobfuscator(pipe)\n            graph_deobfuscator.clean_graph()\n            cutter.refresh()\n\n\n    def create_cutter_plugin():\n        return GraphDeobfuscatorCutter()\n\n\nif __name__ == \"__main__\":\n    graph_deobfuscator = GraphDeobfuscator(pipe)\n    graph_deobfuscator.clean_graph()\n\n
\n\n

 

\n

","status":"PUBLISHED","fileName":null,"link":"http://research.checkpoint.com/deobfuscating-apt32-flow-graphs-with-cutter-and-radare2/","tags":[],"score":0.1052798479795456,"topStoryDate":null}],"mapData":null,"topMalwareFamilies":null};