
{"id":243,"date":"2020-03-30T15:52:12","date_gmt":"2020-03-30T13:52:12","guid":{"rendered":"http:\/\/futex.re\/blog\/?p=243"},"modified":"2020-03-30T15:52:12","modified_gmt":"2020-03-30T13:52:12","slug":"agent-tesla-strings-decrypter","status":"publish","type":"post","link":"https:\/\/futex.re\/blog\/?p=243","title":{"rendered":"Agent Tesla Strings Decrypter"},"content":{"rendered":"<p>I wanted to take a look at <a href=\"https:\/\/github.com\/0xd4d\/dnlib\">dnlib<\/a> trying to understand what are its possibilities. at the same point I had to deal with an &#8216;Agent Tesla&#8217; sample, which used strings obfuscation with AES CBC 256 bits.<\/p>\n<p>The decryption method was easy to find, as can be seen in the capture below:<\/p>\n<p><a href=\"http:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings-300x87.png\" alt=\"\" class=\"alignnone size-medium wp-image-246\" width=\"300\" height=\"87\" srcset=\"https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings-300x87.png 300w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings-768x224.png 768w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings-1024x298.png 1024w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings-1000x288.png 1000w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings-500x146.png 500w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Crypted_strings.png 1371w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>A quick win consist to copy paste this method inside a new Visual Studio project, and it will do the job later.<\/p>\n<p>First we have to load the assembly binary.<\/p>\n<p><code>ModuleDefMD module = ModuleDefMD.Load(executable);<c ode=\"\"><\/c><\/code><\/p>\n<p><code>Next we parse the types and all the methods of the binary and search for a String opcode (value 114 in IL) and if the string is forwarded by a Call, that mean that the string is a method parameter.<br \/>\n<code><br \/>\nforeach (TypeDef type in module.GetTypes())<br \/>\n{<br \/>\ncountModule++;<\/code><\/p>\n<p><code>                    foreach (MethodDef method in type.Methods)<br \/>\n{<br \/>\n\/\/check if the method is not empty and if it not a constructor<br \/>\nif (!method.HasBody || method.IsConstructor)<br \/>\ncontinue;<\/p>\n<p>countMethod++;<\/p>\n<p><\/code><code>                        for (int i = 0; i &lt; method.Body.Instructions.Count; i++)<br \/>\n                        {<br \/>\n                            if (method.Body.Instructions[i].OpCode.Value == 114) \/\/OpCodes.Ldstr)<br \/>\n                            {<br \/>\n                                if (method.Body.Instructions[i + 1].OpCode == OpCodes.Call)<br \/>\n                                {<br \/>\n<\/code><\/p>\n<p>Now we can retreive the encrypted string, and past it to the DecryptString method. Afterwards, to get a clean output sample, we have to remove the string and the call to the real decryption method inside the loaded assembly<br \/>\n<code><br \/>\nvar cryptedstring = method.Body.Instructions[i].Operand.ToString();<\/code><\/p>\n<p><code>string decryptedstring = DecryptString(cryptedstring);<\/p>\n<p>\/\/For exception max stack value<br \/>\nmethod.Body.KeepOldMaxStack = true;<\/p>\n<p>method.Body.Instructions[i].OpCode = OpCodes.Ldstr;<br \/>\nmethod.Body.Instructions[i].Operand = decryptedstring;<\/p>\n<p>method.Body.Instructions.Remove(method.Body.Instructions[i + 1]);<\/p>\n<p><\/code><code><\/code><\/p>\n<p>And finally we save our new clean assembly<br \/>\n<code><br \/>\nString outputfilename = outputFolderName +\" \\\\\" + filename + \"_uncrypt.exe\";<\/code><\/p>\n<p><code><br \/>\n<\/code><code>module.Write(outputfilename);<br \/>\n<\/code><\/p>\n<p>Now opening the cleaned Assembly in Dnspy, we can see the clean strings:<\/p>\n<p><a href=\"http:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings.png\"><img decoding=\"async\" loading=\"lazy\" src=\"http:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings-300x87.png\" alt=\"\" class=\"alignnone size-medium wp-image-245\" width=\"300\" height=\"87\" srcset=\"https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings-300x87.png 300w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings-768x224.png 768w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings-1024x298.png 1024w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings-1000x288.png 1000w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings-500x146.png 500w, https:\/\/futex.re\/blog\/wp-content\/uploads\/2018\/07\/Clean_strings.png 1371w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>Of course, if the algorithm changes, we have to fix inside the source code and rebuild the binary, which can be a bit painful. An evolution could be used a kind of reflexion to call the decryption method inside the loaded assembly (with is RVA for example). We will explore this another time.<\/p>\n<p>As you can see, it's really easy to use the basic of DNlib, and we can do even more cool thinks!<\/p>\n<p>The source code of the decrypter can be found on my <a href=\"https:\/\/github.com\/futex\/Reverse\/tree\/master\/DotNet\/AgentTeslaStringsDecrypter\">github<\/a>, in case  it is useful to anyone.<\/p>\n<p>If you see any bugs or have further ideas to make it great again, don't hesitate to message me.<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted to take a look at dnlib trying to understand what are its possibilities. at the same point I had to deal with an &#8216;Agent Tesla&#8217; sample, which used strings obfuscation with AES CBC 256 bits. The decryption method &hellip; <a href=\"https:\/\/futex.re\/blog\/?p=243\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/futex.re\/blog\/index.php?rest_route=\/wp\/v2\/posts\/243"}],"collection":[{"href":"https:\/\/futex.re\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/futex.re\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/futex.re\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/futex.re\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=243"}],"version-history":[{"count":6,"href":"https:\/\/futex.re\/blog\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":253,"href":"https:\/\/futex.re\/blog\/index.php?rest_route=\/wp\/v2\/posts\/243\/revisions\/253"}],"wp:attachment":[{"href":"https:\/\/futex.re\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/futex.re\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/futex.re\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}