{"id":4143,"date":"2023-04-15T09:07:00","date_gmt":"2023-04-15T17:07:00","guid":{"rendered":"https:\/\/www.pnfsoftware.com\/blog\/?p=4143"},"modified":"2023-05-01T08:01:12","modified_gmt":"2023-05-01T16:01:12","slug":"control-flow-unflattening-in-the-wild","status":"publish","type":"post","link":"https:\/\/www.pnfsoftware.com\/blog\/control-flow-unflattening-in-the-wild\/","title":{"rendered":"Control-flow unflattening in the wild"},"content":{"rendered":"\n<p>Both JEB decompiler engines <sup class='footnote'><a href='#fn-4143-1' id='fnref-4143-1' onclick='return fdfootnote_show(4143)'>1<\/a><\/sup> ship with code optimizers capable of rebuilding methods whose control-flow was transformed by flattening obfuscators.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-11.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-11-1024x511.png\" alt=\"\" class=\"wp-image-4595\" width=\"460\" height=\"229\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-11-1024x511.png 1024w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-11-300x150.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-11-768x384.png 768w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-11-1536x767.png 1536w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-11-2048x1023.png 2048w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/a><figcaption class=\"wp-element-caption\">Image \u00a9 <a href=\"http:\/\/tigress.cs.arizona.edu\/transformPage\/docs\/flatten\/index.html\">Tigress<\/a> (University of Arizona)<\/figcaption><\/figure>\n<\/div>\n\n\n<p>Control-flow flattening, sometimes referred to as <em>chenxification<\/em><sup class='footnote'><a href='#fn-4143-2' id='fnref-4143-2' onclick='return fdfootnote_show(4143)'>2<\/a><\/sup>, is an obfuscation technique employed to destructure a routine control-flow. While a compiled routine is typically composed of a number of basic blocks having low ingress and egress counts, a flattened routine may exhibit an outlier node having high input and high output edge counts, and generally, a very high centrality in the graph (in terms of vertex betweenness). Practically speaking, the original method M is reduced to a many-way conditional block H evaluating an expression VPC, dispatching the flow of execution to units of code, each one performing a part of M, updating VPC, and looping back to H. In effect, the original structured code is reduced to a large switch-like block, whose execution is guided by a synthetic variable VPC. Therefore, the original flow of control, critical to infer meaning while performing manual reverse-engineering, is lost. <sup class='footnote'><a href='#fn-4143-3' id='fnref-4143-3' onclick='return fdfootnote_show(4143)'>3<\/a><\/sup><\/p>\n\n\n\n<p>We upgraded <em>dexdec<\/em>&#8216;s control flow unflattener earlier this year. <sup class='footnote'><a href='#fn-4143-4' id='fnref-4143-4' onclick='return fdfootnote_show(4143)'>4<\/a><\/sup> The v2 of the unflattener is more generic than our original implementation. It is able to cover cases in which the obfuscated does not map to the clean model presented above, e.g. cases where the dispatcher stands out.<\/p>\n\n\n\n<p>This week, we encountered an instance of code that was auto-deobfuscated to clean code and thought it&#8217;d be a good example to show how useful generic deobfuscation of such code can be. It seems that the obfuscator that was used to protect the original code was <a href=\"https:\/\/github.com\/CodingGay\/BlackObfuscator\/blob\/main\/README_EN.md\">BlackObfuscator<\/a>, a project used by clean apps and malware alike.<\/p>\n\n\n\n<p>Hash: <a href=\"https:\/\/www.virustotal.com\/gui\/file\/92ae23580c83642ad0e50f19979b9d2122f28d8b3a9d4b17539ce125ae8d93eb\">92ae23580c83642ad0e50f19979b9d2122f28d8b3a9d4b17539ce125ae8d93eb<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-14.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"686\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-14-1024x686.png\" alt=\"\" class=\"wp-image-4602\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-14-1024x686.png 1024w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-14-300x201.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-14-768x514.png 768w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-14-1536x1029.png 1536w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-14-2048x1371.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">Before deobfuscation.<\/figcaption><\/figure>\n\n\n\n<p>After deobfuscation, the code looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-15.png\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"578\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-15-1024x578.png\" alt=\"\" class=\"wp-image-4603\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-15-1024x578.png 1024w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-15-300x169.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-15-768x433.png 768w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-15-1536x867.png 1536w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2023\/04\/image-15-2048x1156.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption class=\"wp-element-caption\">After deobfuscation.<\/figcaption><\/figure>\n\n\n\n<p>If you encounter examples where the unflattener does not perform adequately, please let us know. We&#8217;ll see if they can be fixed or upgraded to cover obfuscation corner-cases.<\/p>\n\n\n\n<p>Thank you &amp; until next time &#8212; Nicolas.<\/p>\n\n\n\n<p>&#8212;<\/p>\n\n\n\n<p><\/p>\n\n\n<div class='footnotes' id='footnotes-4143'><div class='footnotedivider'><\/div><ol><li id='fn-4143-1'> <em>dexdec<\/em> is JEB&#8217; dex\/dalvik decompiler, <em>gendec<\/em> is JEB&#8217;s generic decompiler used for native code and any code other than dex\/dalvik <span class='footnotereverse'><a href='#fnref-4143-1'>&#8617;<\/a><\/span><\/li><li id='fn-4143-2'> A term coined by University of Arizona&#8217;s Pr. Christian Collberg for the fact that an early description of this technique was presented by Dr. Chenxi Wang in her PhD thesis <span class='footnotereverse'><a href='#fnref-4143-2'>&#8617;<\/a><\/span><\/li><li id='fn-4143-3'> Control-flow flattening can be seen as a particular case of code virtualization, which was covered in previous blog entries. <span class='footnotereverse'><a href='#fnref-4143-3'>&#8617;<\/a><\/span><\/li><li id='fn-4143-4'> JEB 4.25 released on <a href=\"https:\/\/www.pnfsoftware.com\/jeb\/changelist\">Jan 17 2023<\/a> <span class='footnotereverse'><a href='#fnref-4143-4'>&#8617;<\/a><\/span><\/li><\/ol><\/div>","protected":false},"excerpt":{"rendered":"<p>Both JEB decompiler engines 1 ship with code optimizers capable of rebuilding methods whose control-flow was transformed by flattening obfuscators. Control-flow flattening, sometimes referred to as chenxification2, is an obfuscation technique employed to destructure a routine control-flow. While a compiled routine is typically composed of a number of basic blocks having low ingress and egress &hellip; <a href=\"https:\/\/www.pnfsoftware.com\/blog\/control-flow-unflattening-in-the-wild\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Control-flow unflattening in the wild<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,22,2,5],"tags":[],"class_list":["post-4143","post","type-post","status-publish","format-standard","hentry","category-decompilation","category-jeb4","category-malware","category-obfuscation"],"_links":{"self":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/4143","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/comments?post=4143"}],"version-history":[{"count":0,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/4143\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/media?parent=4143"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/categories?post=4143"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/tags?post=4143"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}