<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Lavínia Rodriguês</title>
    <description>The latest articles on DEV Community by Lavínia Rodriguês (@laviniasec).</description>
    <link>https://dev.to/laviniasec</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1200397%2F67073763-3b67-4781-8cde-03d78bf0457c.jpg</url>
      <title>DEV Community: Lavínia Rodriguês</title>
      <link>https://dev.to/laviniasec</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/laviniasec"/>
    <language>en</language>
    <item>
      <title>Deciphering MalwareTech's Static Analysis Challenges [shellcode1]</title>
      <dc:creator>Lavínia Rodriguês</dc:creator>
      <pubDate>Wed, 08 Nov 2023 23:51:16 +0000</pubDate>
      <link>https://dev.to/laviniasec/deciphering-malwaretechs-static-analysis-challenges-shellcode1-3fac</link>
      <guid>https://dev.to/laviniasec/deciphering-malwaretechs-static-analysis-challenges-shellcode1-3fac</guid>
      <description>&lt;p&gt;Up until recently, I'd never tried the "Beginner Reverse Engineering Challenges" from &lt;a href="https://malwaretech.com"&gt;MalwareTech&lt;/a&gt; as part of my studies, even after knowing their existence for quite some time. I don't have a exact reason, I just hadn't tried them before.&lt;/p&gt;

&lt;p&gt;But today I began solving those challenges for pure fun, here is my solution to the &lt;code&gt;shellcode1&lt;/code&gt; challenge.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we already know?
&lt;/h2&gt;

&lt;p&gt;The challenge description reads...&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Position independent code (AKA Shellcode) is assembly code which can simply be copied to a memory location and run. Due to the lack of need for complex loading &amp;amp; initialization, it is popular for many tasks such as code injection. These challenges are designed to test your ability to reverse engineer malware shellcode.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What we can deduce from this is that the provided binary will, obviously, run a shellcode. For a shellcode to be run, it needs to be mapped to some area in memory where it can be executed from.&lt;/p&gt;

&lt;p&gt;What this means is that the challenge provided binary needs to first allocate some space somewhere in memory, copy the position independent code to such space, and then pass execution to it.&lt;/p&gt;

&lt;p&gt;As the challenge is windows focused, I can infer it probably uses &lt;code&gt;VirtualAlloc&lt;/code&gt; for memory allocation.&lt;/p&gt;

&lt;p&gt;Well, we shall look into the code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finding the shellcode
&lt;/h2&gt;

&lt;p&gt;For this challenge, I used radare2.&lt;/p&gt;

&lt;p&gt;I began by disassembling the binary's entrypoint, which doesn't seem to be a stub to a &lt;code&gt;main&lt;/code&gt; function. So I looked out for a call to &lt;code&gt;VirtualAlloc&lt;/code&gt; followed by a call to &lt;code&gt;memcpy&lt;/code&gt;, and sure here it is!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtwuc9jzaawifed807vp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgtwuc9jzaawifed807vp.png" alt="Piece from the disassembly where a call to VirtualAlloc follows a call to memcpy" width="800" height="264"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We know that &lt;code&gt;memcpy&lt;/code&gt; should copy the shellcode from somewhere in the binary to the allocated memory. We can see in the code that it copies 13 (&lt;code&gt;0xd&lt;/code&gt;) bytes from address &lt;code&gt;0x404068&lt;/code&gt; to the new memory space.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfnn5lw33wmvx8i7alzi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frfnn5lw33wmvx8i7alzi.png" alt="Piece from the disassembly showing VirtualAlloc's returned address being passed as one of the three arguments to a call to memcpy" width="672" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;0x404068&lt;/code&gt; should be where our shellcode is stored in the binary, and it is 13 bytes long!&lt;/p&gt;

&lt;p&gt;Let's dump it...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jwh33g5zmvkeqgtyufy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6jwh33g5zmvkeqgtyufy.png" alt="Disassembly of the shellcode" width="708" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Analyzing the shellcode
&lt;/h2&gt;

&lt;p&gt;It begins by loading an address from &lt;code&gt;esi&lt;/code&gt; into &lt;code&gt;edi&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x00404068      8b3e           mov edi, dword [esi]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, it loads some value into &lt;code&gt;ecx&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x0040406a      8b4e04         mov ecx, dword [esi + 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;ecx&lt;/code&gt; register is commonly used as a loop counter, which the next two instructions effectively do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;0x0040406d      c0440fff05     rol byte [edi + ecx - 1], 5
0x00404072      e2f9           loop 0x40406d
0x00404074      c3             ret
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;loop 0x40406d&lt;/code&gt; instruction loops the execution to &lt;code&gt;0x40406d&lt;/code&gt; every time it is hit, decrementing the &lt;code&gt;ecx&lt;/code&gt; register. When &lt;code&gt;ecx&lt;/code&gt; becomes 0, it stops looping back and passes execution to the next instruction (&lt;code&gt;ret&lt;/code&gt;) which exits off the shellcode.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rol&lt;/code&gt; is the bitwise left-rotation operation. Here, it left-rotates the byte at address pointed by &lt;code&gt;edi + ecx - 1&lt;/code&gt;, 5 times.&lt;/p&gt;

&lt;p&gt;As &lt;code&gt;rol&lt;/code&gt; instruction loops and the &lt;code&gt;ecx&lt;/code&gt; register decrements, the given string is iterated backwards, left-rotating every byte on it 5 times in a roll.&lt;/p&gt;

&lt;p&gt;We can infer from this is that the encoded flag string is stored in the address pointed by &lt;code&gt;edi&lt;/code&gt;, which is set before the shellcode being called, and all (or some) bytes from it needs to be left-rotated for decoding.&lt;/p&gt;

&lt;h2&gt;
  
  
  Decoding the flag
&lt;/h2&gt;

&lt;p&gt;By looking back at the entrypoint function, we see that &lt;code&gt;edi&lt;/code&gt; is set to &lt;code&gt;0x404040&lt;/code&gt; right before the execution being passed to the shellcode.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa9ol6cndaj4807zni2kp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa9ol6cndaj4807zni2kp.png" alt="Piece of disassembly showing the execution being passed to the shellcode" width="491" height="73"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zwfnh92xrsqyp3k71ww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7zwfnh92xrsqyp3k71ww.png" alt="Piece of disassembly showing var_4h being set to 0x404040, which later is used as a value to edi" width="615" height="59"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, the encoded flag should be stored at &lt;code&gt;0x404040&lt;/code&gt;! Dumping it...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn56qu7r6l48augfvro6p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn56qu7r6l48augfvro6p.png" alt="Dumped encoded flag" width="615" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here it is! I copied the bytes from &lt;code&gt;0x404040&lt;/code&gt; to &lt;code&gt;0x404066&lt;/code&gt; (where a &lt;code&gt;0x00&lt;/code&gt; byte appears, which indicates a string's end).&lt;/p&gt;

&lt;p&gt;Finally, here's a python script I made for decoding it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;#!/usr/bin/env python3
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;rotate_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="nf"&gt;return &lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;byte&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="mh"&gt;0xFF&lt;/span&gt;

&lt;span class="n"&gt;ENCODED_FLAG&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytearray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x32\x62\x0a\x3a\xdb\x9a\x42\x2a\x62\x62\x1a\x7a\x22\x2a\x69\x4a\x9a\x72\xa2\x69\x52\xaa\x9a\xa2\x69\x32\x7a\x92\x69\x2a\xc2\x82\x62\x7a\x4a\xa2\x9a\xeb\x00&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;decoded_flag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;bytearray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sa"&gt;b&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;X&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENCODED_FLAG&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;# the letter "X" is a placeholder
&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENCODED_FLAG&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
    &lt;span class="n"&gt;decoded_flag&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;rotate_left&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ENCODED_FLAG&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;decoded_flag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;ascii&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we get our flag!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsf7sc982khge6p24w91a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsf7sc982khge6p24w91a.png" alt="Decoded flag" width="437" height="53"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yaaayyyyy!!&lt;/p&gt;

</description>
      <category>writing</category>
      <category>reverseengineering</category>
      <category>hacking</category>
    </item>
  </channel>
</rss>
