{"id":2669,"date":"2019-10-31T10:06:49","date_gmt":"2019-10-31T18:06:49","guid":{"rendered":"https:\/\/www.pnfsoftware.com\/blog\/?p=2669"},"modified":"2019-10-31T10:14:42","modified_gmt":"2019-10-31T18:14:42","slug":"analyzing-golang-executables","status":"publish","type":"post","link":"https:\/\/www.pnfsoftware.com\/blog\/analyzing-golang-executables\/","title":{"rendered":"Analyzing Golang Executables"},"content":{"rendered":"\n<p>The <a href=\"https:\/\/golang.org\/\">Go programming language<\/a> (also known as Golang) has gained popularity during the last few years <a href=\"https:\/\/unit42.paloaltonetworks.com\/the-gopher-in-the-room-analysis-of-golang-malware-in-the-wild\/\">among malware developers<\/a> . This can certainly be explained by the relative simplicity of the language, and the cross-compilation ability of its compiler, allowing multi-platform malware development without too much effort.<\/p>\n\n\n\n<p>In this blog post, we dive into Golang executables reverse engineering, and present a Python extension for JEB decompiler to ease Golang analysis; here is the table of content:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><a href=\"#title_basics\">Golang Basics for Reverse Engineers<\/a><\/li><li><a href=\"#title_jeb\">Making JEB Great for Golang<\/a><ol><li><a href=\"#title_status\">Current Status<\/a><\/li><li><a href=\"#title_routines\">Finding (and Naming) Routines<\/a><\/li><li><a href=\"#title_strings\">Strings Recovery<\/a><\/li><li><a href=\"#title_types\">Types Recovery<\/a><\/li><\/ol><\/li><li><a href=\"#title_stealth\">Use-Case: Analysis of StealthWorker Malware<\/a><\/li><\/ol>\n\n\n\n<p><strong>The JEB Python script presented in this blog can be found <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\">on our GitHub page<\/a>. Make sure to update JEB to version 3.7+ before running it. <\/strong><\/p>\n\n\n\n<p><strong><em>Disclaimer: the analysis in this blog post refers to the current Golang version (1.13) and part of it might become outdated with future releases.<\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"title_basics\">Golang Basics for Reverse Engineers<\/h2>\n\n\n\n<p><em>Feel free to <a href=\"#title_jeb\">skip this part<\/a> if you&#8217;re already familiar with Golang reverse engineering.<\/em><\/p>\n\n\n\n<p>Let&#8217;s start with some facts that reverse engineers might find interesting to know before analyzing their first Golang executable.<\/p>\n\n\n\n<p><strong>1. Golang is an <a href=\"https:\/\/go.googlesource.com\/go\">open-source<\/a><\/strong> <strong>language with a pretty active development community<\/strong>. The language was originally created at Google around 2007, and version 1.0 was released in <a href=\"https:\/\/golang.org\/doc\/devel\/release.html#go1\">March 2012<\/a>. Since then, two major versions are released <a href=\"https:\/\/golang.org\/doc\/devel\/release.html\">each year<\/a>.<\/p>\n\n\n\n<p><strong>2. Golang has a long lineage<\/strong>: in particular many low-level implementation choices &#8212; some would say oddities &#8212; in Golang can be traced back to <a href=\"https:\/\/en.wikipedia.org\/wiki\/Plan_9_from_Bell_Labs\">Plan9<\/a>, a distributed operating system on which some Golang creators were previously working. <\/p>\n\n\n\n<p><strong>3. Golang has been designed for concurrency<\/strong>, in particular by providing so-called &#8220;<a href=\"https:\/\/tour.golang.org\/concurrency\/1\">goroutines<\/a>&#8220;, which are <em>lightweight<\/em> threads executing concurrently (but <a href=\"https:\/\/blog.golang.org\/concurrency-is-not-parallelism\">not necessarily in parallel<\/a>). <\/p>\n\n\n\n<p>Developers can start a new goroutine simply by prefixing a function call by <code>go<\/code>. A new goroutine will then start executing the function, while the caller goroutine returns and continues its execution concurrently with the callee. Let&#8217;s illustrate that with the following Golang program:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nfunc myDummyFunc(){\n\ttime.Sleep(1 * time.Second)\n\tfmt.Println(&quot;dummyFunc executed&quot;)\n}\n\nfunc main(){\n\t\n\tmyDummyFunc() \/\/ normal call\n\tfmt.Println(&quot;1 - back in main&quot;)\n\t\n\tgo myDummyFunc() \/\/ !! goroutine call\n\tfmt.Println(&quot;2 - back in main&quot;)\n\n\ttime.Sleep(3 * time.Second)\n}\n<\/pre><\/div>\n\n\n<p>Here, <code>myDummyFunc()<\/code> is called once normally, and then as a goroutine. Compiling and executing this program results in the following output:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dummyFunc executed\n1 - back in main\n2 - back in main\ndummyFunc executed<\/code><\/pre>\n\n\n\n<p>Notice how the execution was back in <code>main()<\/code> before executing the second call to <code>dummyFunc()<\/code>.<\/p>\n\n\n\n<p>Implementation-wise, <em>many<\/em> goroutines can be executed on a <em>single<\/em> operating system thread. Golang runtime takes care of switching goroutines, e.g. whenever one executes a blocking system call. According to the <a href=\"https:\/\/golang.org\/doc\/faq#goroutines\">official documentation<\/a> &#8220;<em>It is practical to create <strong>hundreds of thousands of goroutines<\/strong> in the same address space<\/em>&#8220;.<\/p>\n\n\n\n<p>What makes goroutines so &#8220;cheap&#8221; to create is that they start with a very limited stack space (2048 bytes &#8212; <a href=\"https:\/\/golang.org\/doc\/go1.4#runtime\">since Golang 1.4<\/a>), which will be increased when needed. <\/p>\n\n\n\n<p><strong>One of the noticeable consequence for reverse engineers is that native routines (almost) all start with the same prologue<\/strong>. Its purpose is to check if the current goroutine&#8217;s stack is large enough, as can be seen in the following CFG:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"312\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack_grow_prologue-1024x312.png\" alt=\"\" class=\"wp-image-2906\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack_grow_prologue-1024x312.png 1024w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack_grow_prologue-300x92.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack_grow_prologue-768x234.png 768w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack_grow_prologue.png 1167w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Fig. 1: Simplified x86 CFG with Golang prologue for stack growth<\/figcaption><\/figure>\n\n\n\n<p>When the stack space is nearly exhausted, more space will be allocated &#8212; actually, the stack will be copied somewhere with enough free space. This particular prologue is present only in routines with local variables. <\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\"><strong>How to distinguish a goroutine call from a &#8220;normal&#8221; call when analyzing a binary?<\/strong> Goroutine calls are implemented by calling <code><a href=\"https:\/\/golang.org\/pkg\/runtime\/?m=all#newproc\">runtime.newproc<\/a><\/code>, which takes in input the address of the native routine to call, the size of its arguments, and then the actual routine&#8217;s arguments.<\/p>\n\n\n\n<p><strong>4. Golang has a <em>concurrent<\/em> garbage collector<\/strong> <strong>(GC)<\/strong>: Golang&#8217;s GC can free memory <em>while <\/em>other goroutines are modifying it.<\/p>\n\n\n\n<p>Roughly speaking, when the GC is freeing memory, goroutines report to it all their memory writes &#8212; to prevent concurrent memory modifications to be missed by the current freeing phase. Implementation-wise, when the GC is in the process of marking used memory, <strong>all memory writes pass through a &#8220;<\/strong><em><a href=\"https:\/\/github.com\/golang\/go\/blob\/master\/src\/runtime\/mbarrier.go\"><strong>write barrier<\/strong><\/a><\/em><strong>&#8220;<\/strong>, which performs the write and informs the GC.<\/p>\n\n\n\n<p><strong>For reverse engineers this can result in particularly convoluted control flow graphs (CFG).<\/strong> For example, here is the CFG when a global variable <code>globalString<\/code> is set to <code>newValue<\/code>:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"220\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/write_barrier-1-1024x220.png\" alt=\"\" class=\"wp-image-3358\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/write_barrier-1-1024x220.png 1024w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/write_barrier-1-300x65.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/write_barrier-1-768x165.png 768w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/write_barrier-1.png 1297w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption> Fig. 2: Write to global variable <code>globalString<\/code> (x86 CFG):<br>before doing the memory write, the code checks if the write barrier is activated,<br> and if yes calls <code>runtime.gcWriteBarrier()<\/code><\/figcaption><\/figure>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">Not all memory writes are monitored in that manner; the rules for write barriers&#8217; insertion are described in <code><a href=\"https:\/\/github.com\/golang\/go\/blob\/8bb47a5eecf57b88c1b9cc088a21ae869c6a6764\/src\/runtime\/mbarrier.go#L106\">mbarrier.go<\/a><\/code>.<\/p>\n\n\n\n<p><strong>5. Golang comes with a custom compiler tool chain (parser, compiler, assembler, linker), all implemented in Golang<\/strong>. <sup class='footnote'><a href='#fn-2669-1' id='fnref-2669-1' onclick='return fdfootnote_show(2669)'>1<\/a><\/sup> <sup class='footnote'><a href='#fn-2669-2' id='fnref-2669-2' onclick='return fdfootnote_show(2669)'>2<\/a><\/sup><\/p>\n\n\n\n<p>From a developer&#8217;s perspective, it means that once Go is installed on a machine, one can compiled for <em>any<\/em> supported platform (making Golang a language of choice for IoT malware developers). Examples of supported platforms include Windows x64, Linux ARM and Linux MIPS (see &#8220;<a href=\"https:\/\/golang.org\/doc\/install\/source#environment\">valid combinations of&nbsp;<\/a><code><a href=\"https:\/\/golang.org\/doc\/install\/source#environment\">$GOOS<\/a><\/code><a href=\"https:\/\/golang.org\/doc\/install\/source#environment\">&nbsp;and&nbsp;<\/a><code><a href=\"https:\/\/golang.org\/doc\/install\/source#environment\">$GOARCH<\/a><\/code>&#8220;).<\/p>\n\n\n\n<p><strong>From a reverse engineer&#8217;s perspective, the custom Go compiler toolchain means Golang binaries sometimes come with &#8220;exotic&#8221; features <\/strong>(which therefore can give a hard time to reverse engineering tools). <\/p>\n\n\n\n<p>For example, symbols in Golang Windows executables are implemented using the <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/debug\/pe-format#coff-symbol-table\">COFF symbol table<\/a> (while <a href=\"https:\/\/docs.microsoft.com\/en-us\/windows\/win32\/debug\/pe-format\">officially<\/a> &#8220;<em>COFF debugging information [for executable] is deprecated<\/em>&#8220;). The Golang COFF symbol implementation is pretty liberal: <a href=\"https:\/\/github.com\/golang\/go\/blob\/1820cca7d735574b4ac9647d3a8b996b5f97f3cc\/src\/cmd\/link\/internal\/ld\/pe.go#L696\">symbols&#8217; type is set to a default value<\/a> &#8212; i.e. there is no clear distinction between code and data. <\/p>\n\n\n\n<p>As another example,  Windows PE read-only data section&nbsp;&#8220;<code>.rdata<\/code>&#8221;&nbsp;has been&nbsp;<a href=\"https:\/\/github.com\/golang\/go\/issues\/25926\">defined as <em>executable<\/em> in past Go versions<\/a>. <\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">\nInterestingly, Golang compiler internally uses <a href=\"https:\/\/golang.org\/doc\/asm#introduction\"><em>pseudo<\/em> assembly instructions<\/a> (with architecture-specific registers). For example, here is a snippet of pseudo-code for ARM (operands are ordered with source first):<br><br>\n\n<code>MOVW  $go.string.\"hello, world\\n\"(SB), R0<br>\nMOVW  R0, 4(R13)<br>\nMOVW  $13, R0<br>\nMOVW  R0, 8(R13)<br>\nCALL  \"\".dummyFunc(SB)<br>\nMOVW.P  16(R13), R15<\/code>\n<br><br>\nThese pseudo-instructions could not be understood by a classic ARM assembler (e.g. there is no <code>CALL<\/code> instruction on ARM). Here are the disassembled ARM instructions from the corresponding binary:<br><br>\n<code>LDR       R0, #451404h \/\/ \"hello, world\\n\" address <br>\nSTR       R0, [SP, #4]<br>\nMOV       R0, #13<br>\nSTR       R0, [SP, #8]<br>\nBL        main.dummyFunc<br>\nLDR       PC, [SP], #16<br><\/code>\n<br>\nNotice how the same pseudo-instruction <code>MOVW<\/code> got converted either as <code>STR<\/code> <b>or<\/b> <code>MOV<\/code> machine instructions. The use of pseudo-assembly comes from Plan9, and allows Golang assembler parser to easily handle all architectures: the only architecture-specific step is the selection of machine instructions (more details <a href=\"https:\/\/talks.golang.org\/2016\/asm.slide#1\">here<\/a>).\n<\/p>\n\n\n\n<p><strong>6. Golang uses by default a <em>stack-only<\/em> calling convention<\/strong>. <\/p>\n\n\n\n<p>Let&#8217;s illustrate that with the following diagram, showing the stack&#8217;s state when a routine with two integer parameters <code>a<\/code> and <code>b<\/code>, and <em>two <\/em>return values &#8212; declared in Go as &#8220;<code>func myRoutine(a int, b int) (int, int)<\/code>&#8221; &#8212; is called:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"375\" height=\"390\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack-1.png\" alt=\"\" class=\"wp-image-2920\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack-1.png 375w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stack-1-288x300.png 288w\" sizes=\"auto, (max-width: 375px) 100vw, 375px\" \/><figcaption>Fig. 3: Simplified stack view (stack grows downward), when a routine with two parameters and two return values is called . The return values are reserved slots for the callee.<\/figcaption><\/figure><\/div>\n\n\n\n<p>It is the caller&#8217;s responsibilities to reserve space for the callees&#8217; parameters and returned values, and to free it later on. <\/p>\n\n\n\n<p>Note that Golang&#8217;s calling convention situation might soon change: since version 1.12, <a href=\"https:\/\/go.googlesource.com\/proposal\/+\/master\/design\/27539-internal-abi.md\">several calling conventions can coexist<\/a> &#8212; the stack-only calling convention remaining the default one for backward compatibility reasons.<\/p>\n\n\n\n<p><strong>7. Golang executables are <em>usually<\/em> statically-linked, i.e. do not rely on external dependencies<\/strong> <sup class='footnote'><a href='#fn-2669-3' id='fnref-2669-3' onclick='return fdfootnote_show(2669)'>3<\/a><\/sup>. In particular they embed a pretty large runtime environment. Consequently, Golang binaries tend to be large: for example, a &#8220;hello world&#8221; program compiled with Golang 1.13 is around 1.5MB with its symbols stripped.<\/p>\n\n\n\n<p><strong>8. Golang executables embed <em>lots<\/em> of symbolic information<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Debug symbols<\/strong>, implemented as DWARF symbols. These can be stripped at compilation time (command-line option <code>-ldflags \"-w\"<\/code>) .<\/li><li><strong>Classic symbols<\/strong> for each executable file format (PE\/ELF\/Mach-O). These can be stripped at compilation time (command-line option <code>-ldflags \"-s\"<\/code>).<\/li><li><strong>Go-specific metadata<\/strong>, including for example all functions&#8217; entry points and names, and complete type information. <strong>These metadata cannot (easily) be stripped<\/strong>, because Golang runtime needs them: for example, functions&#8217; information are needed to walk the stack for errors handling or for garbage collection, while types information serve for runtime type checks.<\/li><\/ul>\n\n\n\n<p>Of course, <strong>Go-specific metadata are very good news for reverse engineers<\/strong>, and parsing these will be one of the purpose of the JEB&#8217;s Python extension described in this blog post.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"title_jeb\">Making JEB Great for Golang<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"title_status\">Current Status<\/h3>\n\n\n\n<p>What happens when opening a Golang executable in JEB? Let&#8217;s start from the usual &#8220;hello world&#8221; example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage main\n\nimport &quot;fmt&quot;\n\nfunc main() {\n\tfmt.Printf(&quot;hello, world\\n&quot;)\n}\n<\/pre><\/div>\n\n\n<p>If we compile it for as a Windows x64 PE file, and open it in JEB, we can notice that <strong>its code has only been partially disassembled<\/strong>. Unexplored memory areas can indeed be seen next to code areas in the native navigation bar (right-side of the screen by default):<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_1.png\" alt=\"\" class=\"wp-image-2685\" width=\"640\" height=\"56\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_1.png 365w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_1-300x26.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption> Fig.4: Navigation bar for Golang PE file <br>(blue is code, green is data, grey represents area without any code or data) <\/figcaption><\/figure>\n\n\n\n<p>We can confirm that the grey areas surrounding the blue areas are code, by manually disassembling them (hotkey &#8216;C&#8217; by default).<\/p>\n\n\n\n<p><strong>Why did JEB disassembler miss this code?<\/strong> As can be seen in the Notifications window, the disassembler used a <em>CONSERVATIVE <\/em>strategy, meaning that it only followed safe control flow relationships (i.e. branches with known targets) <sup class='footnote'><a href='#fn-2669-4' id='fnref-2669-4' onclick='return fdfootnote_show(2669)'>4<\/a><\/sup>. <\/p>\n\n\n\n<p>Because Go runtime calls most native routines <em>indirectly<\/em>, in particular when creating goroutines, JEB disassembler finds little reliable control flow relationships, explaining why some code areas remain unexplored. <\/p>\n\n\n\n<p>Before going on, let&#8217;s take a look at the corresponding Linux executable, which we can obtain simply by setting environment variable <code>$GOOS<\/code> to <code>linux<\/code> before compiling. Opening the resulting ELF file in JEB brings us in a more positive situation:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_elf.png\" alt=\"\" class=\"wp-image-2692\" width=\"642\" height=\"60\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_elf.png 388w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_elf-300x28.png 300w\" sizes=\"auto, (max-width: 642px) 100vw, 642px\" \/><figcaption>  Fig. 5: Navigation bar for Golang ELF file <br>(blue is code, green is data, grey represents area without any code or data) <\/figcaption><\/figure>\n\n\n\n<p>Due to the use by default of <em>AGGRESSIVE <\/em>strategy for disassembling ELF files, JEB disassembler found the whole code area (all code sections were linearly disassembled). In particular this time we can see our main routine, dubbed <code>main.main<\/code> by the compiler:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"445\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/main_main.png\" alt=\"\" class=\"wp-image-2694\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/main_main.png 640w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/main_main-300x209.png 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><figcaption>Fig. 6: Extract of main.main routine&#8217;s disassembly<\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-background has-light-gray-background-color\"><strong>Are data mixed with code in Golang executables?<\/strong> If yes, that would make <em>AGGRESSIVE <\/em>disassembly a risky strategy. At this moment (version 1.13 with default Go compiler), this does not seem to be the case: <br><br>&#8211; Data are explicitly stored in different sections than code, on PE and ELF. <br><br>&#8211; <a href=\"https:\/\/tour.golang.org\/flowcontrol\/9\">Switch statements<\/a> are not implemented with jumptables &#8212; a common case of data mixed with code, e.g. in Visual Studio or GCC ARM. Note that Golang provides several switch-like statements, as the <a href=\"https:\/\/tour.golang.org\/concurrency\/5\">select<\/a> statement or the <a href=\"https:\/\/tour.golang.org\/methods\/16\">type switch statement<\/a>.<br><br>As anything Golang related, the situation might change in future releases (for example, there is still an <a href=\"https:\/\/github.com\/golang\/go\/issues\/5496\">open discussion to implement jumptables for switch<\/a>).<\/p>\n\n\n\n<p>Yet, there is still something problematic in our ELF disassembly: the &#8220;hello world&#8221; string was not properly defined. Following the reference made by <code>LEA<\/code> instruction in the code, we reach a memory area where many strings have indeed been misrepresented as 1-byte data items:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"432\" height=\"346\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/strings.png\" alt=\"\" class=\"wp-image-2695\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/strings.png 432w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/strings-300x240.png 300w\" sizes=\"auto, (max-width: 432px) 100vw, 432px\" \/><figcaption>Fig. 7: Dump of the memory area containing strings. Only the first byte of the strings is defined.<\/figcaption><\/figure><\/div>\n\n\n\n<p>Now that we have a better idea of JEB&#8217;s current status, we are going to explain how we extended it with a Python script to ease Golang analysis. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"title_routines\">Finding and Naming Routines<\/h3>\n\n\n\n<p>The first problem on our road is the incomplete control flow, specially on Windows executables. At first, it might seem that PE files disassembly could be improved simply by setting disassembler&#8217;s strategy to <em>AGGRESSIVE<\/em>, exactly as for ELF files. While it might be an acceptable quick solution, we can actually improve the control flow in a much safer way by parsing Go metadata.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Parsing &#8220;Pc Line Table&#8221;<\/h4>\n\n\n\n<p><a href=\"https:\/\/docs.google.com\/document\/d\/1lyPIbmsYbXnpNj57a261hgOYVpNRcgydurVQIyZOz_o\/pub\">Since version 1.2<\/a>, Golang executables embed a structure called &#8220;pc line table&#8221;, also known as <code>pclntab<\/code>. Once again, this structure (and its name) is an heritage from Plan9, where its original purpose was to associate a program counter value (&#8220;pc&#8221;) to another value (e.g. a line number in the source code).<\/p>\n\n\n\n<p>The structure has evolved, and now contains a <em>function symbol table<\/em>, which stores in particular <strong>the entry points and names of all routines defined in the binary<\/strong>. The Golang runtime uses it in particular for stack unwinding, call stack printing and garbage collection. <\/p>\n\n\n\n<p>In others words, <code>pclntab<\/code> cannot be easily stripped from a binary, and provide us a reliable way to improve our disassembler&#8217;s control flow!<\/p>\n\n\n\n<p>First, our script locates <code>pclntab<\/code> structure (refer to <code><a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/Commons.py#L104\">locatePclntab()<\/a><\/code> for the details):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n  # non-stripped binary: use symbol\n  if findSymbolByName(golangAnalyzer.codeContainerUnit, &#039;runtime.pclntab&#039;) != None:\n      pclntabAddress = findSymbolByName(..., &#039;runtime.pclntab&#039;)\n  \n  # stripped binary\n  else:\n    # PE: brute force search in .rdata. or in all binary if section not present\n    if &#x5B;...].getFormatType() == WellKnownUnitTypes.typeWinPe\n    &#x5B;...]\n  \n    # ELF: .gopclntab section if present, otherwise brute force search\n    elif &#x5B;...].getFormatType() == WellKnownUnitTypes.typeLinuxElf:\n    &#x5B;...]\n<\/pre><\/div>\n\n\n<p>On stripped binaries (i.e. without classic symbols), we search memory for the magic constant <code>0xFFFFFFFB<\/code> starting <code>pclntab<\/code>, and then runs some checks on the possible fields. Note that it is usually easier to parse Golang ELF files, as important runtime structures are stored in distinct sections.<\/p>\n\n\n\n<p>Second, we parse <code>pclntab<\/code> and use its function symbol table to disassemble all functions and rename them:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&#x5B;...]\n# enqueue function entry points from pclntab and register their names as labels\nfor myFunc in pclntab.functionSymbolTable.values():   \n nativeCodeAnalyzer.enqueuePointerForAnalysis(EntryPointDescription(myFunc.startPC), INativeCodeAnalyzer.PERMISSION_FORCEFUL)\n if rename:\n   labelManager.setLabel(myFunc.startPC, myFunc.name, True, True, False)\n\n# re-run disassembler with the enqueued entry points\nself.nativeCodeAnalyzer.analyze()\n\n<\/pre><\/div>\n\n\n<p>Running this on our original PE file allows to discover all routines, and gives the following navigation bar:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_pe_script.png\" alt=\"\" class=\"wp-image-2753\" width=\"608\" height=\"54\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_pe_script.png 357w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/navigation_bar_pe_script-300x27.png 300w\" sizes=\"auto, (max-width: 608px) 100vw, 608px\" \/><figcaption> Fig. 8: Navigation bar for Golang PE file after running the script <br>(blue is code, green is data, grey represents area without any code or data) <\/figcaption><\/figure><\/div>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">Interestingly, <strong>a few Golang&#8217;s runtime routines provide hints about the machine used to compile the binary<\/strong>, for example:<br><br>&#8211; <code><a href=\"http:\/\/\tif buildVersion == &quot;&quot; {\">runtime.schedinit()<\/a><\/code>: references Go&#8217;s build version. Knowing the exact version allows to investigate possible script parsing failures (as some internal structures might change depending on Go&#8217;s version).<br><br>&#8211; <code><a href=\"https:\/\/github.com\/golang\/go\/blob\/ec4e8517cd17aaa2c4224815444e7d28c81ec673\/src\/runtime\/extern.go#L216\">runtime.GOROOT()<\/a><\/code>: references Go&#8217;s installation folder used during compilation. This might be useful for malware tracking.<br><br>These routines are present only if the rest of the code relies on them. If it is the case, <code><a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/master\/FunctionsFinder.py\">FunctionsFinder<\/a><\/code> module highlights them in JEB&#8217;s console, and the user can then examine them.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Remaining Unnamed Routines<\/h4>\n\n\n\n<p>Plot twist! A few routines found by the disassembler remain nameless even after <code>FunctionsFinder<\/code> module parsed <code>pclntab<\/code> structure. All these routines are adjacent in memory and composed of the same instructions, for example: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"300\" height=\"278\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/duff_zero-300x278.png\" alt=\"\" class=\"wp-image-3266\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/duff_zero-300x278.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/duff_zero.png 553w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><figcaption>Fig. 9: Series of unnamed routines in x86<\/figcaption><\/figure><\/div>\n\n\n\n<p>Long story short, these routines are made for zeroing or copying memory blobs, and are part of two large routines respectively named <code>duff_zero<\/code> and <code>duff_copy<\/code>. <\/p>\n\n\n\n<p>These large routines are <a href=\"https:\/\/en.wikipedia.org\/wiki\/Duff%27s_device\">Duff&#8217;s devices<\/a> made for zeroing\/copying memory. They are generated as <a href=\"https:\/\/golang.org\/src\/runtime\/mkduff.go\">long unrolled loops of machine instructions<\/a>. Depending on how many bytes need to be copied\/zeroed the compiler will call <em>directly<\/em> on a particular instruction. For each of these calls, a nameless routine will then be created by the disassembler.<\/p>\n\n\n\n<p><code><a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/master\/DuffDevicesFinder.py\">DuffDevicesFinder<\/a><\/code> module identifies such routines with pattern matching on assembly instructions. By counting the number of instructions, it then renames them <code>duff_zero_N<\/code>\/<code>duff_copy_N<\/code>, with <code>N<\/code> the number of bytes zeroed\/copied. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Source Files<\/h4>\n\n\n\n<p>Interestingly, <code>pclntab<\/code> structure also stores <strong>original source files<\/strong>&#8216; <strong>paths<\/strong>. This supports various Golang&#8217;s runtime features, like printing meaningful stack traces, or providing information on callers from a callee (see <a href=\"https:\/\/golang.org\/pkg\/runtime\/#Caller\">runtime.Caller()<\/a>). Here is an example of a stack trace obtained after a <code><a href=\"https:\/\/gobyexample.com\/panic\">panic()<\/a><\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PANIC\ngoroutine 1 [running]:\nmain.main()\n        C:\/Users\/[REDACTED]\/go\/src\/hello_panic\/hello_panic.go:4 +0x40<\/code><\/pre>\n\n\n\n<p>The script <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/Commons.py#L226\">extracts the list of source files<\/a> and print them in logs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"title_strings\">Strings Recovery<\/h2>\n\n\n\n<p>The second problem we initially encountered in JEB was the badly defined strings. <\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What Is a String?<\/h4>\n\n\n\n<p>Golang&#8217;s strings are stored at runtime in a particular structure called <a href=\"https:\/\/golang.org\/pkg\/reflect\/#StringHeader\">StringHeader<\/a> with two fields:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\ntype StringHeader struct {\n        Data uintptr       \/\/ string value\n        Len  int           \/\/ string size \n}\n<\/pre><\/div>\n\n\n<p>The string&#8217;s characters (pointed by the <code>Data<\/code> field) are stored in data sections of the executables, as <strong>a series of UTF-8 encoded characters <\/strong><em><strong>without<\/strong><\/em><strong> null-terminators<\/strong>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Dynamic Allocation<\/h4>\n\n\n\n<p><code>StringHeader<\/code> structures can be built dynamically, in particular when the string is local to a routine. For example:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"615\" height=\"62\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/stringheader.png\" alt=\"\" class=\"wp-image-2758\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/stringheader.png 615w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/09\/stringheader-300x30.png 300w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><figcaption>Fig. 10: <code>StringHeader<\/code> instantiation in x86<\/figcaption><\/figure><\/div>\n\n\n\n<p>By default JEB disassembler defines a 1-byte data item (<code>gvar_4AFB52<\/code> in previous picture) for the string value, rather than a proper string, because:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> As the string value is referenced only by <code>LEA<\/code> instruction, without any hints on the data type (<code>LEA<\/code> is just loading an &#8220;address&#8221;), the disassembler cannot type the pointed data accordingly.  <br><\/li><li> The string value does not end with a null-terminator, making JEB&#8217;s standard strings identification algorithms unable to determine the string&#8217;s length when scanning memory. <\/li><\/ul>\n\n\n\n<p>To find these strings, <strong><code>StringsBuilder<\/code> module searches <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/StringsBuilder.py#L78\">for the particular assembly instructions<\/a> usually used for instantiating <code>StringHeader<\/code> structures (for x86\/x64, ARM and MIPS architectures).<\/strong> We can then properly define a string by fetching its size from the assembly instructions. Here is an example of recovered strings:<\/p>\n\n\n\n<ul class=\"wp-block-gallery columns-2 wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\"><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"280\" height=\"684\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/str_before-1.png\" alt=\"\" data-id=\"3234\" data-link=\"https:\/\/www.pnfsoftware.com\/blog\/?attachment_id=3234\" class=\"wp-image-3234\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/str_before-1.png 280w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/str_before-1-123x300.png 123w\" sizes=\"auto, (max-width: 280px) 100vw, 280px\" \/><figcaption>Before strings recovery<\/figcaption><\/figure><\/li><li class=\"blocks-gallery-item\"><figure><img loading=\"lazy\" decoding=\"async\" width=\"310\" height=\"220\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/str_after.png\" alt=\"\" data-id=\"3235\" data-link=\"https:\/\/www.pnfsoftware.com\/blog\/?attachment_id=3235\" class=\"wp-image-3235\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/str_after.png 310w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/str_after-300x213.png 300w\" sizes=\"auto, (max-width: 310px) 100vw, 310px\" \/><figcaption>After strings recovery<\/figcaption><\/figure><\/li><\/ul>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">Of course, this heuristic will fail if different assembly instructions are employed to instantiate <code>StringHeader<\/code> structures in future Golang compiler release (such change happened in the past, e.g. x86 instructions changed with Golang 1.8).<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Static Allocation<\/h4>\n\n\n\n<p><code>StringHeader<\/code> can also be statically allocated, for example for global variables; in this case the complete structure is stored in the executable. The code referencing such strings employs many different instructions, making pattern matching not suitable. <\/p>\n\n\n\n<p>To find these strings, <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/StringsBuilder.py#L369\">we scan data sections for possible <\/a><code><a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/StringsBuilder.py#L369\">StringHeader<\/a><\/code><a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/StringsBuilder.py#L369\"> structures<\/a> (i.e. a <code>Data<\/code> field pointing to a <em>printable<\/em> string of size <code>Len<\/code>). Here is an example of recovered structures:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"534\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/srings_static.png\" alt=\"\" class=\"wp-image-3204\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/srings_static.png 902w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/srings_static-300x178.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/srings_static-768x455.png 768w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><figcaption>Fig. 13: Reconstructed <code>StringHeader<\/code><\/figcaption><\/figure>\n\n\n\n<p>The script employs <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/StringsBuilder.py#L389\">two<\/a> <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/StringsBuilder.py#L422\">additional<\/a> final heuristics, which scan memory for printable strings located between two already-defined strings. This allows to recover strings missed by previous heuristics. <\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">When a small local string is used for comparison only, no <code>StringHeader<\/code> structure gets allocated. The string comparison is done directly by machine instructions; for example, <code>CMP [EAX], 0x64636261<\/code> to compare with &#8220;abcd&#8221; on x86. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"title_types\">Types Recovery<\/h3>\n\n\n\n<p>Now that we extended JEB to handle the &#8220;basics&#8221; of Golang analysis, we can turn ourselves to what makes Golang-specific metadata particularly interesting: types. <\/p>\n\n\n\n<p><strong>Golang executables indeed embed descriptions for <em>all <\/em>types<\/strong> <strong>manipulated in the binary<\/strong>, including in particular those defined by developers.<\/p>\n\n\n\n<p>To illustrate that, let&#8217;s compile the following Go program, which defines a <a href=\"https:\/\/gobyexample.com\/structs\">Struct<\/a> (Golang&#8217;s replacement for classes) with two fields:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\npackage main\n\ntype DummyStruct struct{\n\tboolField bool\n\tintField int\n}\n\nfunc dummyFunc(s DummyStruct) int{\n\treturn 13 * s.intField\n}\n\nfunc main(){\n\ts := DummyStruct{boolField: true, intField:37}\n\tt := dummyFunc(s)\n\tt += 1\n}\n<\/pre><\/div>\n\n\n<p>Now, if we compile this source code as a stripped x64 executable, and analyze it with <code><a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/master\/TypesBuilder.py\">TypesBuilder<\/a><\/code> module, the following structure will be reconstructed:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"844\" height=\"365\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/mystruct_type_editor-1.png\" alt=\"\" class=\"wp-image-2988\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/mystruct_type_editor-1.png 844w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/mystruct_type_editor-1-300x130.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/mystruct_type_editor-1-768x332.png 768w\" sizes=\"auto, (max-width: 844px) 100vw, 844px\" \/><figcaption>Fig. 14: Structure reconstructed by <code>TypesBuilder<\/code>, as seen in JEB&#8217;s type editor<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Not only did we get the structure and its fields&#8217; original names, but we also retrieved the structure&#8217;s exact memory layout<\/strong>, including the padding inserted by the compiler to align fields. We can confirm <code>DummyStruct<\/code>&#8216;s layout by looking at its initialization code in <code>main()<\/code>: <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"399\" height=\"65\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/mystruct_code.png\" alt=\"\" class=\"wp-image-2995\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/mystruct_code.png 399w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/mystruct_code-300x49.png 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><figcaption>Fig. 15: <code>DummyStruct<\/code> initialization: <code>intField<\/code> starts at offset 8, as extracted from type information <\/figcaption><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Why So Much Information? <\/h4>\n\n\n\n<p>Before explaining how <code>TypesBuilder<\/code> parses types information, let&#8217;s first understand <em>why<\/em> these information are needed at all. Here are a few Golang features that rely on types at runtime:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Dynamic memory allocation<\/strong>, usually through a call to <code><a href=\"https:\/\/golang.org\/pkg\/runtime\/?m=all#newobject\">runtime.newobject()<\/a><\/code>, which takes in input the description of the type to be allocated<\/li><li><strong>Dynamic type checking<\/strong>, with statements like <a href=\"https:\/\/tour.golang.org\/methods\/15\">type assertions<\/a> or <a href=\"https:\/\/tour.golang.org\/methods\/16\">type switches<\/a>. Roughly speaking, two types will be considered equals if they have the same type descriptions.<\/li><li><strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Reflection_(computer_programming)\">Reflection<\/a><\/strong>, through the built-in package <code><a href=\"https:\/\/golang.org\/pkg\/reflect\/\">reflect<\/a><\/code>, which allows to manipulate objects of unknown types from their type descriptions<\/li><\/ul>\n\n\n\n<p>Golang type descriptions can be considered akin to C++ <a href=\"https:\/\/en.wikipedia.org\/wiki\/Run-time_type_information\">Run-Time Type Information<\/a>, except <strong>that there is no easy way to prevent their generation by the compiler<\/strong>. In particular, even when not using reflection, types descriptors remain present.<\/p>\n\n\n\n<p>For reverse engineers, this is another very good news: knowing types (and their names) will help understanding the code&#8217;s purpose. <\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">Of course, it is certainly doable to obfuscate types, for example by giving them meaningless names at compilation. We did not find any malware using such technique.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">What Is A Type?<\/h4>\n\n\n\n<p>In Golang each type has an associated <a href=\"https:\/\/golang.org\/pkg\/reflect\/#Kind\">Kind<\/a>, which can take one the following values:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\nconst (\n    Invalid Kind = iota\n    Bool\n    Int\n    Int8\n    Int16\n    Int32\n    Int64\n    Uint\n    Uint8\n    Uint16\n    Uint32\n    Uint64\n    Uintptr\n    Float32\n    Float64\n    Complex64\n    Complex128\n    Array\n    Chan\n    Func\n    Interface\n    Map\n    Ptr\n    Slice\n    String\n    Struct\n    UnsafePointer\n)\n<\/pre><\/div>\n\n\n<p>Alongside types usually seen in programming languages (integers, strings, boolean, maps, etc), one can notice some Golang-specific types:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/tour.golang.org\/moretypes\/6\">Array<\/a>: fixed-size array<\/li><li><a href=\"https:\/\/tour.golang.org\/moretypes\/7\">Slice<\/a>: variable-size view of an Array<\/li><li><a href=\"https:\/\/tour.golang.org\/basics\/4\">Func<\/a>: functions; Golang&#8217;s functions are <a href=\"https:\/\/en.wikipedia.org\/wiki\/First-class_citizen\">first-class citizens<\/a> (for example, they can be passed as <a href=\"https:\/\/tour.golang.org\/moretypes\/24\">arguments<\/a>)<\/li><li><a href=\"https:\/\/tour.golang.org\/concurrency\/2\">Chan<\/a>: communication channels for goroutines<\/li><li><a href=\"https:\/\/gobyexample.com\/structs\">Struct<\/a>: collection of fields, Golang&#8217;s replacement for classes<\/li><li><a href=\"https:\/\/tour.golang.org\/methods\/9\">Interface<\/a>: collection of methods, implemented by Structs<\/li><\/ul>\n\n\n\n<p><strong>The type&#8217;s kind is the type&#8217;s &#8220;category&#8221;<\/strong>; what identifies the type is its complete description, which is stored in the following <a href=\"https:\/\/github.com\/golang\/go\/blob\/ee3f768d3861e00dbc6a81392a711209f66e235c\/src\/internal\/reflectlite\/type.go#L150\"><code>rtype<\/code><\/a> structure:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    type rtype struct {\n      size       uintptr\n      ptrdata    uintptr  \/\/ number of bytes in the type that can contain pointers\n      hash       uint32   \/\/ hash of type; avoids computation in hash tables\n      tflag      tflag    \/\/ extra type information flags\n      align      uint8    \/\/ alignment of variable with this type\n      fieldAlign uint8    \/\/ alignment of struct field with this type\n      kind       uint8    \/\/ enumeration for C\n      alg        *typeAlg \/\/ algorithm table\n      gcdata     *byte    \/\/ garbage collection data\n      str        nameOff  \/\/ string form\n      ptrToThis  typeOff  \/\/ type for pointer to this type, may be zero\n    }\n<\/pre><\/div>\n\n\n<p class=\"has-background has-light-gray-background-color\">The type&#8217;s name is part of its description (<code>str<\/code> field). This means that, for example, one could define an alternate integer type with <code>type myInt int<\/code>, and <code>myInt<\/code> and <code>int<\/code> would then be <em>distinct <\/em>types (with distinct<em> <\/em>type descriptors, each of <code>Int<\/code> kind). In particular, assigning a variable of type <code>myInt<\/code> to a variable of type <code>int<\/code> would necessitate an explicit cast.  <\/p>\n\n\n\n<p>The <code>rtype<\/code> structure only contains general information, and for non-primary types (Struct, Array, Map,&#8230;) it is actually embedded into another structure (as the first field), whose remaining fields provides type-specific information.<\/p>\n\n\n\n<p>For example, here is <a href=\"https:\/\/github.com\/golang\/go\/blob\/ee3f768d3861e00dbc6a81392a711209f66e235c\/src\/internal\/reflectlite\/type.go#L282\">strucType<\/a>, the type descriptor for types with <code>Struct<\/code> kind:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    type structType struct {\n      rtype\n      pkgPath name          \n      fields  &#x5B;]structField\n    }\n<\/pre><\/div>\n\n\n<p>Here, we have in particular a slice of <a href=\"https:\/\/github.com\/golang\/go\/blob\/ee3f768d3861e00dbc6a81392a711209f66e235c\/src\/internal\/reflectlite\/type.go#L267\">structField<\/a>, another structure describing the structure fields&#8217; types and layout.<\/p>\n\n\n\n<p>Finally, types can have <a href=\"https:\/\/tour.golang.org\/methods\/1\">methods<\/a> defined on them: a method is a function with a special argument, called the <em>receiver<\/em>, which describes the type on which the methods applies. For example, here is a method on <code>MyStruct<\/code> structure (notice receiver&#8217;s name after <code>func<\/code>):<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunc (myStruct MyStruct) method1() int{\n    ...\n}\n<\/pre><\/div>\n\n\n<p>Where are methods&#8217; types stored? Into yet another structure called <a href=\"https:\/\/github.com\/golang\/go\/blob\/ee3f768d3861e00dbc6a81392a711209f66e235c\/src\/internal\/reflectlite\/type.go#L178\">uncommonType<\/a>, which is appended to the receiver&#8217;s type descriptor. In other words, a structure <em>with methods<\/em> will be described by the following structure:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntype UncommonStructType struct {\n      rtype\n      structType\n      uncommonType\n}\n<\/pre><\/div>\n\n\n<p>Here is an example of such structure, as seen in JEB after running <code>TypesBuilder<\/code> module:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"766\" height=\"773\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/struct_descriptor_raw.png\" alt=\"\" class=\"wp-image-3101\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/struct_descriptor_raw.png 766w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/struct_descriptor_raw-150x150.png 150w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/struct_descriptor_raw-297x300.png 297w\" sizes=\"auto, (max-width: 766px) 100vw, 766px\" \/><figcaption> Fig. 16: Type descriptor for a structure with methods:<br>StrucType (with embedded rtype, and referencing StructField), <br>followed by UncommonType (referencing MethodType)<\/figcaption><\/figure>\n\n\n\n<p>Parsing type descriptors can therefore be done by starting from <code>rtype<\/code> (present for all types), and adding wrapper structures around it, if needed. Properly renaming type descriptors in memory greatly helps the analysis, as these descriptors are passed as arguments to many runtime routines (as we will see in StealthWorker&#8217;s malware analysis).<\/p>\n\n\n\n<p>The final step is to transform the type descriptors into the actual types &#8212; for example, translating a structType into the memory representation of the corresponding structure &#8211;, which can then be imported in JEB types. For now, <code>TypesBuilder<\/code> do this final import step <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/TypesBuilder.py#L237\">for named structures only<\/a>.<\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">Describing in details all Golang&#8217;s type descriptors is out-of-scope for this blog. Refer to <code><a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/master\/TypesBuilder.py\">TypesBuilder<\/a><\/code> module for gory details.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Locating Type Descriptors<\/h4>\n\n\n\n<p>The last question we have to examine is how to actually locate type descriptors in Golang binaries. This starts with a structure called <a href=\"https:\/\/github.com\/golang\/go\/blob\/2686e7494845dae877e0efb4ff786c672b2cd2ef\/src\/runtime\/symtab.go#L265\">moduledata<\/a>, whose purpose is to &#8220;<em>record information about the layout of the executable<\/em>&#8220;:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n    type moduledata struct {\n      pclntable    &#x5B;]byte\n      ftab         &#x5B;]functab\n      filetab      &#x5B;]uint32\n      findfunctab  uintptr\n      minpc, maxpc uintptr\n\n      text, etext           uintptr\n      noptrdata, enoptrdata uintptr\n      data, edata           uintptr\n      bss, ebss             uintptr\n      noptrbss, enoptrbss   uintptr\n      end, gcdata, gcbss    uintptr\n      types, etypes         uintptr\n\n      textsectmap &#x5B;]textsect\n      typelinks   &#x5B;]int32 \/\/ offsets from types\n      itablinks   &#x5B;]*itab\n\n      &#x5B;...REDACTED...]\n    }\n<\/pre><\/div>\n\n\n<p>This structure defines in particular a range of memory dedicated to storing type information (from <code>types<\/code> to <code>etypes<\/code>). Then, <code>typelink<\/code> field stores offsets in the range where type descriptors begin. <\/p>\n\n\n\n<p>So first we locate <code>moduledata<\/code>, either from a specific symbol for non-stripped binaries, or through a brute-force search. For that, we search for the address of <code>pclntab<\/code> previously found (first <code>moduledata<\/code> field), and then apply <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/Commons.py#L68\">some checks on its fields<\/a>. <\/p>\n\n\n\n<p>Second, <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\/blob\/76224b3996734ede7587f0362543f8b1dd42e935\/TypesBuilder.py#L111\">we start the actual parsing of the types range<\/a>, which is a recursive process as some types reference others types, during which we apply the type descriptors&#8217; structures.<\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">There is no backward compatibility requirement on runtime&#8217;s internal structures &#8212; as Golang executables embed their own runtime. In particular, <code>moduledata<\/code> and type descriptions are not guaranteed to stay backward compatible with older Golang release (and they were already largely modified since their inception). <br><br>In others words, <code>TypesBuilder<\/code> module&#8217;s current implementation might become outdated in future Golang releases (and might not properly work on older versions).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"title_stealth\">Use-Case: StealthWorker<\/h2>\n\n\n\n<p>We are now going to dig into a malware dubbed StealthWorker. This malware infects Linux\/Windows machines, and mainly attempts to brute-force web platforms, such as WordPress, phpMyAdmin or Joomla. Interestingly, StealthWorker heavily relies on concurrency, making it a target of choice for a first analysis.<\/p>\n\n\n\n<p>The sample we will be analyzing is a x86 Linux version of StealthWorker, version 3.02, whose symbols have been stripped (SHA1: <code>42ec52678aeac0ddf583ca36277c0cf8ee1fc680<\/code>)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Reconnaissance<\/h3>\n\n\n\n<p>Here is JEB&#8217;s console after disassembling the sample and running the script with all modules activated (<code>FunctionsFinder<\/code>, <code>StringsBuilder<\/code>, <code>TypesBuilder<\/code>, <code>DuffDevicesFinder<\/code>, <code>PointerAnalyzer<\/code>):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&gt;&gt;&gt; <b>Golang Analyzer<\/b> &lt;&lt;&lt;\n&gt; pclntab parsed (0x84B79C0)\n&gt; first module data parsed (0x870EB20)\n&gt; <b>FunctionsFinder: 9528 function entry points enqueued (and renamed)<\/b>\n&gt; FunctionsFinder: running disassembler... OK\n &gt; point of interest: routine runtime.GOROOT (0x804e8b0): references Go root path of developer's machine (sys.DefaultGoroot)\n &gt; point of interest: routine runtime.schedinit (0x8070e40): references Go version (sys.TheVersion)\n&gt; <b>StringsBuilder: building strings... OK (4939 built strings)<\/b>\n&gt; <b>TypesBuilder: reconstructing types... OK (5128 parsed types - 812 types imported to JEB - see logs)<\/b>\n&gt; <b>DuffDevicesFinder: finding memory zero\/copy routines... OK (93 routines identified)<\/b>\n&gt; <b>PointerAnalyzer: 5588 pointers renamed<\/b>\n&gt; see logs (C:\\[REDACTED]\\log.txt)<\/code><\/pre>\n\n\n\n<p>Let&#8217;s start with some reconnaissance work: <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The binary was compiled with <strong>Go version 1.11.4<\/strong> (referenced in <code>runtime.schedinit<\/code>&#8216;s code, as mentioned by the script&#8217;s output)<br><\/li><li>Go&#8217;s root path on developer&#8217;s machine is <code>\/usr\/local\/go<\/code> (referenced by <code>runtime.GOROOT<\/code>&#8216;s code)<br><\/li><li>Now, let&#8217;s turn to the reconstructed strings; there are too many to draw useful conclusions at this point, but at least we got an interesting IP address (spoiler alert: that&#8217;s the C&amp;C&#8217;s address):<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"477\" height=\"287\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_strings.png\" alt=\"\" class=\"wp-image-3165\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_strings.png 477w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_strings-300x181.png 300w\" sizes=\"auto, (max-width: 477px) 100vw, 477px\" \/><figcaption>Fig. 17: Extract of StealthWorker&#8217;s strings <br>as seen in JEB after running the script<\/figcaption><\/figure><\/div>\n\n\n\n<ul class=\"wp-block-list\"><li>More interestingly, the list of source files extracted from <code>pclntab<\/code> (outputted in the script&#8217;s <code>log.txt<\/code>) shows a modular architecture:<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>> \/home\/user\/go\/src\/AutorunDropper\/Autorun_linux.go\n> \/home\/user\/go\/src\/Check_double_run\/Checker_linux.go\n> \/home\/user\/go\/src\/Cloud_Checker\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerAdminFinder\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerBackup_finder\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerBitrix_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerBitrix_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerCpanel_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerCpanel_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerDrupal_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerDrupal_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerFTP_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerFTP_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerHtpasswd_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerHtpasswd_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerJoomla_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerJoomla_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerMagento_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerMagento_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerMysql_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerOpencart_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerOpencart_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerPMA_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerPMA_check\/WorkerPMA_check.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerPostgres_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerSSH_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerWHM_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerWHM_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerWP_brut\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/WorkerWP_check\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/Worker_WpInstall_finder\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/Worker_wpMagOcart\/main.go\n> \/home\/user\/go\/src\/StealthWorker\/main.go<\/code><\/pre>\n\n\n\n<p>Each <code>main.go<\/code> corresponds to a Go package, and its quite obvious from the paths that each of them targets a specific web platform. Moreover, there seems to be mainly two types of packages: <code>WorkerTARGET_<em>brut<\/em><\/code>, and <code>WorkerTARGET_<em>check<\/em><\/code>. <\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">There are no information regarding the time of compilation in Golang executables. In particular <a href=\"https:\/\/github.com\/golang\/go\/commit\/98376204e8aa8abb159fc8d5a752c2c07fb431a3\">executables&#8217; timestamps have been set to a fixed value at compilation<\/a>, in order to always generate the same executable from a given input.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Let&#8217;s dig a bit further by looking at <code>main<\/code> package, which is where <a href=\"https:\/\/golang.org\/ref\/spec#Program_execution\">execution begins<\/a>; here are its routines with pretty informative names:<\/li><\/ul>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"174\" height=\"142\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_methods.png\" alt=\"\" class=\"wp-image-3174\"\/><figcaption>Fig. 18: main&#8217;s package routines<\/figcaption><\/figure><\/div>\n\n\n\n<p>Additionally there is a series of <code>type..hash*<\/code> and <code>type..eq*<\/code> methods for <code>main<\/code> package:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"269\" height=\"163\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_struct_methods-2.png\" alt=\"\" class=\"wp-image-3185\"\/><figcaption>Fig. 19: Hashing methods (automatically generated for complex types)<\/figcaption><\/figure><\/div>\n\n\n\n<p>These methods are automatically generated for types equality and hashing, and therefore their presence indicates that non-trivial custom types are used in <code>main<\/code> package (as we will see below).<\/p>\n\n\n\n<p>We can also examine <code>main.init()<\/code> routine. The <code>init()<\/code> routine is generated for each package by Golang&#8217;s compiler to initialize others packages that this package relies on, and the package&#8217;s global variables:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_init.png\"><img loading=\"lazy\" decoding=\"async\" width=\"479\" height=\"761\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_init.png\" alt=\"\" class=\"wp-image-3178\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_init.png 479w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_init-189x300.png 189w\" sizes=\"auto, (max-width: 479px) 100vw, 479px\" \/><\/a><figcaption>Fig. 20: Packages initialization from <code>main.init()<\/code><\/figcaption><\/figure><\/div>\n\n\n\n<p>Along the previously seen packages, one can notice some interesting custom packages:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"http:\/\/github.com\/remeh\/sizedwaitgroup\">github.com\/remeh\/sizedwaitgroup<\/a>: a re-implementation of Golang&#8217;s <a href=\"https:\/\/gobyexample.com\/waitgroups\">WaitGroup<\/a> &#8212; a mechanism to wait for goroutines termination &#8211;, but with a limit in the amount of goroutines started concurrently. As we will see, StealthWorker&#8217;s developer takes special care to not overload the infected machine.<\/li><li><a href=\"https:\/\/github.com\/sevlyar\/go-daemon\">github.com\/sevlyar\/go-daemon<\/a>: a library to write daemon processes in Go.<\/li><\/ul>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">Golang packages&#8217; paths are part of a global namespace, and it is considered best practice to use <a href=\"https:\/\/golang.org\/doc\/code.html#Workspaces\">GitHub&#8217;s URLs as package paths<\/a> for external packages to avoid conflicts.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Concurrent Design<\/h3>\n\n\n\n<p>In this blog, we will not dig into each StealthWorker&#8217;s packages implementation, as it has been already been done <a href=\"https:\/\/www.fortinet.com\/blog\/threat-research\/unveiling-stealthworker-campaign.html\">several<\/a> <a href=\"https:\/\/blog.yoroi.company\/research\/new-gobrut-version-in-the-wild\/\">times<\/a>. Rather, we will focus on the concurrent design made to organize the work between these packages.<\/p>\n\n\n\n<p>Let&#8217;s start with an overview of StealthWorker&#8217;s architecture:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_archi-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"987\" height=\"833\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_archi-1.png\" alt=\"\" class=\"wp-image-3158\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_archi-1.png 987w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_archi-1-300x253.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_archi-1-768x648.png 768w\" sizes=\"auto, (max-width: 987px) 100vw, 987px\" \/><\/a><figcaption>Fig. 21: StealthWorker&#8217;s design overview<\/figcaption><\/figure>\n\n\n\n<p>At first, a goroutine executing <code>getActiveProject()<\/code> regularly retrieves a list of &#8220;projects&#8221; from the C&amp;C server. Each project is identified by a keyword (<code>wpChk<\/code> for WordPress checker, <code>ssh_b<\/code> for SSH brute-forcer, etc).<\/p>\n\n\n\n<p>From there, the real concurrent work begins: five goroutines executing  <code>PrepareTaskFunc()<\/code> retrieve a list of targets for each project, and then distribute work to &#8220;Workers&#8221;.  There are several interesting quirks here:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To allow <code>PrepareTaskFunc()<\/code> goroutines to communicate with <code>Worker()<\/code> goroutines, a Channel is instantiated: <\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"752\" height=\"100\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_chan.png\" alt=\"\" class=\"wp-image-3272\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_chan.png 752w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_main_chan-300x40.png 300w\" sizes=\"auto, (max-width: 752px) 100vw, 752px\" \/><figcaption>Fig. 22: Channel&#8217;s instantiation<\/figcaption><\/figure>\n\n\n\n<p> As can be seen from the channel type descriptor &#8212; parsed and renamed by the script &#8211;, the Channel is made for objects of type <code>interface {}<\/code>, the <a href=\"https:\/\/tour.golang.org\/methods\/14\">empty interface<\/a>. In others words, <strong>objects of <\/strong><em><strong>any<\/strong><\/em><strong> type can be sent and received through it<\/strong> (because &#8220;direction:both&#8221;).<\/p>\n\n\n\n<p><code>PrepareTaskFunc()<\/code> will then receive from the C&amp;C server a list of targets for a given project &#8212; as JSON objects &#8211;, and for each target will instantiate a specific structure. We already noticed these structures when looking at <code>main<\/code> package&#8217;s routines, here are their reconstructed form in the script&#8217;s logs:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> struct main.StandartBrut (4 fields):\n    - string Host (offset:0)\n    - string Login (offset:8)\n    - string Password (offset:10)\n    - string Worker (offset:18)\n\n> struct main.StandartChecker (5 fields):\n    - string Host (offset:0)\n    - string Subdomains (offset:8)\n    - string Subfolder (offset:10)\n    - string Port (offset:18)\n    - string Worker (offset:20)\n\n> struct main.WPBrut (5 fields):\n    - string Host (offset:0)\n    - string Login (offset:8)\n    - string Password (offset:10)\n    - string Worker (offset:18)\n    - int XmlRpc (offset:20)\n\n> struct main.StandartBackup (7 fields):\n    - string Host (offset:0)\n    - string Subdomains (offset:8)\n    - string Subfolder (offset:10)\n    - string Port (offset:18)\n    - string FileName (offset:20)\n    - string Worker (offset:28)\n    - int64 SLimit (offset:30)\n\n> struct main.WpMagOcartType (5 fields):\n    - string Host (offset:0)\n    - string Login (offset:8)\n    - string Password (offset:10)\n    - string Worker (offset:18)\n    - string Email (offset:20)\n\n> struct main.StandartAdminFinder (6 fields):\n    - string Host (offset:0)\n    - string Subdomains (offset:8)\n    - string Subfolder (offset:10)\n    - string Port (offset:18)\n    - string FileName (offset:20)\n    - string Worker (offset:28)\n\n> struct main.WPChecker (6 fields):\n    - string Host (offset:0)\n    - string Subdomains (offset:8)\n    - string Subfolder (offset:10)\n    - string Port (offset:18)\n    - string Worker (offset:20)\n    - int Logins (offset:28)<\/code><\/pre>\n\n\n\n<p>Note that all structures have <code>Worker<\/code> and <code>Host<\/code> fields. The structure (one per target) will then be sent through the channel.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>On the other side of the channel, a <code>Worker()<\/code> goroutine will fetch the structure, and use reflection to generically process it (i.e. without knowing <em>a priori<\/em> which structure was sent):<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"637\" height=\"241\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_reflect.png\" alt=\"\" class=\"wp-image-3281\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_reflect.png 637w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_reflect-300x114.png 300w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><figcaption>Fig. 23: StealthWorker&#8217;s use of reflection to retrieve a field from an unknown structure<\/figcaption><\/figure>\n\n\n\n<p>Finally, depending on the value in <code>Worker<\/code> field, the corresponding worker&#8217;s code will be executed. There are two types of workers: <em>brute-forcing workers<\/em>, which try to login into the target through a known web platform, and <em>checking workers<\/em>, which test the existence of a certain web platform on the target.<\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">From a design point-of-view, there is a difference between the two types of workers: <em>checking workers<\/em> internally relies on another Channel, in which the results are going to be written, and fetched by another goroutine named <code>saveGood()<\/code>, which reports to the C&amp;C. On the other hand, <em>brute-forcing workers<\/em> do their task and directly report to the C&amp;C server.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li> Interestingly, the <em>maximum<\/em> number of <code>Worker()<\/code> goroutines can be configured by giving a parameter to the executable (preceded by the argument <code>dev<\/code>). According to the update mechanism, it seems that the usual value for this maximum is 400. Then, the previously mentioned <a href=\"https:\/\/github.com\/remeh\/sizedwaitgroup\">SizedWaitGroup<\/a> package serves to ensure the number of goroutines stay below this value:<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"244\" src=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_worker_loop.png\" alt=\"\" class=\"wp-image-3274\" srcset=\"https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_worker_loop.png 838w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_worker_loop-300x87.png 300w, https:\/\/www.pnfsoftware.com\/blog\/wp-content\/uploads\/2019\/10\/stealthworker_worker_loop-768x224.png 768w\" sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><figcaption>Fig. 24: Worker&#8217;s creation loop<br><code>SizeWaitGroup.Add()<\/code> is blocking when the maximum number of goroutines has been reached. Each <code>main.Worker()<\/code> will release its slot when terminating.<\/figcaption><\/figure>\n\n\n\n<p>We can imagine that the maximum amount of workers is tuned by StealthWorker&#8217;s operators to lower the risk of overloading infected machines (and drawing attention).<\/p>\n\n\n\n<p class=\"has-background has-light-gray-background-color\">There are two additional goroutines, respectively executing routines <code>KnockKnock()<\/code> and <code>CheckUpdate()<\/code>. Both of them simply run specific tasks concurrently (and infinitely): the former sends a &#8220;ping&#8221; message to the C&amp;C server, while the latter asks for an updated binary to execute. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What&#8217;s Next? Decompilation!<\/h2>\n\n\n\n<p>The <a href=\"https:\/\/github.com\/pnfsoftware\/jeb-golang-analyzer\">provided Python script<\/a> should allow users to properly analyze Linux and Windows Golang executables with JEB. It should also be a good example of what can be done with JEB API to handle &#8220;exotic&#8221; native platforms. <\/p>\n\n\n\n<p>Regarding Golang reverse engineering, for now we remained at disassembler level, but decompiling Golang native code to <em>clean <\/em>pseudo-C is clearly a reachable goal for JEB. There are a few important steps to implement first, like properly handling Golang stack-only calling convention (with multiple return values), or generating type libraries for Golang runtime.<\/p>\n\n\n\n<p>So&#8230; stay tuned for more Golang reverse engineering!<\/p>\n\n\n\n<p>As usual, if you have questions, comments or suggestions, feel free to:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>leave a comment on this post<\/li><li>email&nbsp;<a href=\"mailto:support@pnfsoftware.com\">support@pnfsoftware.com<\/a><\/li><li>message us on&nbsp;<a href=\"https:\/\/jebdecompiler.slack.com\/\">Slack<\/a><\/li><li>or send us a Tweet&nbsp;<a href=\"https:\/\/twitter.com\/jebdec\">@jebdec<\/a><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<p>A few interesting reading for reverse engineers wanting to dig into Golang&#8217;s internals:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/rednaga.io\/2016\/09\/21\/reversing_go_binaries_like_a_pro\/\">https:\/\/rednaga.io\/2016\/09\/21\/reversing_go_binaries_like_a_pro\/<\/a> <\/li><li><a href=\"https:\/\/2016.zeronights.ru\/wp-content\/uploads\/2016\/12\/GO_Zaytsev.pdf\">https:\/\/2016.zeronights.ru\/wp-content\/uploads\/2016\/12\/GO_Zaytsev.pdf<\/a> <\/li><li> <a href=\"https:\/\/go-re.tk\/\">https:\/\/go-re.tk<\/a> <\/li><li><a href=\"https:\/\/blog.stalkr.net\/2015\/04\/golang-data-races-to-break-memory-safety.html\">https:\/\/blog.stalkr.net\/2015\/04\/golang-data-races-to-break-memory-safety.html<\/a> <\/li><li><a href=\"https:\/\/cmc.gitbook.io\/go-internals\/\">https:\/\/cmc.gitbook.io\/go-internals<\/a>\/<\/li><li> <a href=\"http:\/\/home.in.tum.de\/~engelke\/pubs\/1709-ma.pdf\">http:\/\/home.in.tum.de\/~engelke\/pubs\/1709-ma.pdf<\/a> <\/li><li><a href=\"https:\/\/science.raphael.poss.name\/go-calling-convention-x86-64.html\"><\/a><a href=\"https:\/\/science.raphael.poss.name\/go-calling-convention-x86-64.html\">https:\/\/science.raphael.poss.name\/go-calling-convention-x86-64.html<\/a><\/li><\/ul>\n\n\n<div class='footnotes' id='footnotes-2669'><div class='footnotedivider'><\/div><ol><li id='fn-2669-1'> The Golang compiler was originally inherited from Plan9 and was written in C, in order to solve the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Bootstrapping_(compilers)\">bootstrapping problem<\/a> (how to compile a new language?), and also to &#8220;easily&#8221; implement segmented stacks &#8212; the original way of dealing with goroutines stack. The process of translating the original C compiler to Golang for release 1.5 has been described in details <a href=\"https:\/\/docs.google.com\/document\/d\/1P3BLR31VA8cvLJLfMibSuTdwTuF7WWLux71CYD0eeD8\/edit\">here <\/a>and <a href=\"https:\/\/talks.golang.org\/2015\/gogo.slide#1\">here<\/a>. <span class='footnotereverse'><a href='#fnref-2669-1'>&#8617;<\/a><\/span><\/li><li id='fn-2669-2'> There are alternate compilers, e.g. <a href=\"https:\/\/golang.org\/doc\/install\/gccgo\">gccgo<\/a> and a <a href=\"https:\/\/go.googlesource.com\/gollvm\/\">gollvm<\/a> <span class='footnotereverse'><a href='#fnref-2669-2'>&#8617;<\/a><\/span><\/li><li id='fn-2669-3'> Golang also allows to compile &#8216;modules&#8217;, which can be loaded dynamically. Nevertheless, for malware writers statically-linked executables remain the usual choice. <span class='footnotereverse'><a href='#fnref-2669-3'>&#8617;<\/a><\/span><\/li><li id='fn-2669-4'> Readers interested in the internals of JEB disassembler engine should refer to our recent <a href=\"https:\/\/www.pnfsoftware.com\/blog\/the-long-journey-to-a-multi-architecture-disassembler\/\">REcon presentation<\/a> <span class='footnotereverse'><a href='#fnref-2669-4'>&#8617;<\/a><\/span><\/li><\/ol><\/div>","protected":false},"excerpt":{"rendered":"<p>The Go programming language (also known as Golang) has gained popularity during the last few years among malware developers . This can certainly be explained by the relative simplicity of the language, and the cross-compilation ability of its compiler, allowing multi-platform malware development without too much effort. In this blog post, we dive into Golang &hellip; <a href=\"https:\/\/www.pnfsoftware.com\/blog\/analyzing-golang-executables\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Analyzing Golang Executables<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,18,2,13],"tags":[],"class_list":["post-2669","post","type-post","status-publish","format-standard","hentry","category-api-jeb3","category-jeb3","category-malware","category-native-code"],"_links":{"self":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/2669","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\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/comments?post=2669"}],"version-history":[{"count":0,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/posts\/2669\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/media?parent=2669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/categories?post=2669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pnfsoftware.com\/blog\/wp-json\/wp\/v2\/tags?post=2669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}