<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by SecHaq on Medium]]></title>
        <description><![CDATA[Stories by SecHaq on Medium]]></description>
        <link>https://medium.com/@sechaq?source=rss-177836e78f66------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*YrIXbOnnN_s0m6Mu</url>
            <title>Stories by SecHaq on Medium</title>
            <link>https://medium.com/@sechaq?source=rss-177836e78f66------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:41:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sechaq/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building my own write blocker]]></title>
            <link>https://sechaq.medium.com/building-my-own-write-blocker-cbf687eb0fd0?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbf687eb0fd0</guid>
            <category><![CDATA[forensics]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[cybercrime]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Mon, 08 Mar 2021 17:28:18 GMT</pubDate>
            <atom:updated>2021-03-08T17:28:18.638Z</atom:updated>
            <content:encoded><![CDATA[<p>Spoiler — It’s cheaper than buying one</p><p>I was looking to buy a write blocker to do data recovery/forensics tasks but I quickly noticed that I was window shopping write blockers due to their cost.<br>Some starting at £300, others that cost less were no longer being built or sold, maybe you could find a 2nd hand one with or without the wires.<br>Most of these write blockers were industry standard, used by law enforcement but was it necessary for me to buy such an expensive write blocker….or is it possible to build my own…..</p><p>So the research began, reading through articles, publications, and so on, and with the information gained, I felt that I could build my own write blocker. So what do I need:</p><ol><li>A Raspberry Pi</li><li>A Linux distro.</li><li>HDD/SSD to test the write blocker</li><li>And to put the information I gained into practice</li></ol><h3>Building the write blocker</h3><p>So, I brought a Raspberry Pi 4 Model B that came with a power supply, HDMI cables, 32GB SD card, a case, and some extras. (<a href="https://www.okdo.com/c/pi-shop/">https://www.okdo.com/c/pi-shop/</a>)<br>What I do like about the package I bought, is it comes with heat sinks, so if we are to transfer data from a HDD, that could take a while. And while performing a lengthy task, we don’t have to worry about the heat generated from the PI with the heat sinks. Maintaining your equipment will have positive effects all around…Oh and it comes with a fan :)</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RJ0b50cjDgWaNYj9YfI2NA.jpeg" /><figcaption>PI with heat sinks</figcaption></figure><p>Next, was to install a Linux distro. for the RaspberryPI and the distro. I am going to use is Kali Linux. Kali, I felt will be best suited for my needs to do data recovery, forensics, and much more with everything they provide and they have their own ARM image which is what we need for the PI— <a href="https://www.offensive-security.com/kali-linux-arm-images/">https://www.offensive-security.com/kali-linux-arm-images/</a><br>So they are ticking all the boxes, plus their continued development and support mean further enhancements. <br>The image I will be using <a href="https://images.kali.org/arm-images/kali-linux-2021.1-rpi4-nexmon-64.img.xz"><strong>Kali Linux RaspberryPi 2 (v1.2), 3, 4 and 400 (64-Bit) (img.xz)</strong></a></p><p>With the image downloaded, we just need to flash it to our SD card using balenaEtcher (<a href="https://www.balena.io/etcher/">https://www.balena.io/etcher/</a>) which is an amazing and simple tool to use.</p><p>Once flashed, we can now set up the Raspberry PI, insert the SD card, and boot up!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-oGVq9SWoBiOGSea-mjVnQ.jpeg" /><figcaption>Kali boot up</figcaption></figure><p>Once I logged in, then I went through setting up Kali process which consists of changing the default password, removing the ssh key, setting up new users, and updating….all the fun stuff.</p><p>Now that I’ve finished setting up my Kali Linux, it’s time to configure the write-blocker config.</p><h3>Write-block</h3><p>I am using the following project: <a href="https://github.com/msuhanov/Linux-write-blocker">https://github.com/msuhanov/Linux-write-blocker</a></p><p>In short, the project owner has built this project to “enable Linux software write blocking” which has they stated is “Useful for computer forensics, incident response, and data recovery”.</p><p>We need to copy the rules script (<a href="https://github.com/msuhanov/Linux-write-blocker/tree/master/userspace/udev">https://github.com/msuhanov/Linux-write-blocker/tree/master/userspace/udev</a>) to /etc/udev/rules.d and the tools folder (<a href="https://github.com/msuhanov/Linux-write-blocker/tree/master/userspace/tools">https://github.com/msuhanov/Linux-write-blocker/tree/master/userspace/tools</a>) to /usr/sbin, and then reboot. (Simple enough….). Once rebooted we then run: “blockdev — setro /dev/sdb” (sdb being the device to write block).</p><p>After that, I plugged in a USB drive with a single text document to see if I could make changes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JYUqmMEyjLL6azeOJ6xT8A.jpeg" /></figure><p>That’s a positive step in the right direction, but we will need to run some more tests. Next, I will take a md5sum of the drive, then do a bit copy of the drive, do another md5sum of the copy and compare the hashes.</p><p>First hash — sudo md5sum /dev/sda &gt; sda.md5 = e0e47e5a2f12173991f52c8ae9d532d2</p><p>Copy of the usb — sudo dd if=/dev/sda of=sda-copy.dd conv=noerror,sync bs=4096</p><p>Second hash = sudo md5sum sda-copy.dd &gt; sda-copy.md5 =e0e47e5a2f12173991f52c8ae9d532d2</p><p>So that’s another positive, I can do a bit copy of the USB and we can verify that no changes have been made through the MD5 hashes being the same.</p><h3>Conclusion</h3><p>Although more thorough testing needs to be done, I’m happy that I’m heading in a positive direction of being able to build my own write blocker for less than £100!</p><h3>References</h3><ol><li><a href="https://commons.erau.edu/cgi/viewcontent.cgi?article=1373&amp;context=jdfsl">https://commons.erau.edu/cgi/viewcontent.cgi?article=1373&amp;context=jdfsl</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbf687eb0fd0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Malware Analysis of a Cryptocurrency Miner — Part 5]]></title>
            <link>https://sechaq.medium.com/malware-analysis-of-a-cryptocurrency-miner-part-5-424c334175e2?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/424c334175e2</guid>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[malware]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Wed, 17 Feb 2021 12:33:48 GMT</pubDate>
            <atom:updated>2021-02-17T12:33:48.859Z</atom:updated>
            <content:encoded><![CDATA[<h3>Malware Analysis of a Cryptocurrency Miner — Part 5</h3><p>Debugging</p><p>In this write-up, I want to bypass some checks our binary does. I’ll be using x32dbg…..Well…let’s just dive straight in.</p><h4>Debugging a bitcoin miner</h4><p>From the previous analysis, we know we need to concentrate on bypassing the following 2 conditions:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*qCpMqp9pOXZjxRqsxWsuvw.png" /></figure><p>So let’s load the binary into x32dbg and set 2 breaks points, one at each condition:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_Txy65jCVL-XUSM3LRJ14g.png" /></figure><p>but I will skip the first condition by filling the condition with NOPs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VX7wUMa194JchvRYwbck8Q.png" /></figure><p>This way, we can bypass the size check. <br>Just before the call to “strstr”, we can see that “haystack” and “needle” being passed in, which is something I covered in the last writeup.</p><p>As I tried to continue to see what will happen (before changing the passed arguments), I recevied an “expection_access_violation” —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*AS5Z7K4xgWYthqauJ6Vf5A.png" /></figure><p>After a bit of researching and going through each instruction step by step, I found that the instruction causing the issue:</p><p>test ecx,3 — and the problem was that ecx held no value which looks to have caused the violation. This will be due to the Malware getting a HTTP response it’s not expecting.<br>I had put in a value that I saw it used in a previous test: sjlj_once and then I was able to get past it, but I had hit “run” and then got the following error :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UWdqZ0cy5ZM338QNaxux1g.png" /></figure><p>From here we see the name: “NsCpuCNMiner32.exe. Now if you Google the exe you will find a lot of information about this miner.<br>Now, thankfully I had Wireshark running which shows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1GzUpgwt2IERbTMDoPjh5g.png" /></figure><p>From the research I did on this Malware, I can tell that it’s trying to find an open FTP port (21) across the internet and if it can log in, it will copy itself to that machine! This could explain all the username and password strings we saw in our static analysis. (This shows the importance of running Malware within a secure network!)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=424c334175e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Malware Analysis of a Cryptocurrency Miner — Part 4]]></title>
            <link>https://sechaq.medium.com/malware-analysis-of-a-cryptocurrency-miner-part-4-74861208a8dc?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/74861208a8dc</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[cybercrime]]></category>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[malware-analysis]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Mon, 08 Feb 2021 16:07:28 GMT</pubDate>
            <atom:updated>2021-02-08T16:07:28.239Z</atom:updated>
            <content:encoded><![CDATA[<h3>Malware Analysis of a Cryptocurrency Miner — Part 4</h3><p>More static code analysis</p><p>In this write up I wanted to concentrate on some of the activities we saw the Malware perform in our dynamic analysis, the first being the HTTP calls to certain websites and why it was in a loop.</p><h3>.RU</h3><p>So, it was not too hard to find the location of the call being made. By checking the strings tab in IDA and finding it in the .data section, we can do a XREF (cross-reference) to find where it’s used:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/830/1*_C_jh_d1ZAhvCMk34aj_yQ.png" /></figure><p>and it takes us to:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*Ngrf83D4UGlXvPTfpgY2wg.png" /></figure><p>The “sub_40DAE0” function takes 3 parameters, just before it we see 3 push instructions. ESI holds the stafftest. ru string and then we see previously a declared pointer variables being set. Note I won’t be delving into the subroutines you see because that will be time-consuming.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/549/1*A30v4G9c9zUwj2dI6EDJ7g.png" /></figure><p>Further down, we can see InternetOpenA being called and on success:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*YRNde2c3zekniDKGplvC4g.png" /></figure><p>Thanks to IDA, we can see the following string “http://%s/test.html?%d and from our dynamic analysis, we know that it will replace %s with a new host and %d will keep incrementing IF a certain condtion is not met. In the above screenshot we can the intrusction “rep movsd”, so taking all this into considartion, we know we are in a do-while loop.</p><p>But what is the condition to get past the loop we couldn’t in our dynamic anaylysis:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*KVd1M8z_fwPKZu1k_7NDyw.png" /></figure><p>It does a “cmp ebp+dwNumberOfBytesRead, 800h (2048)”, if the defined “numberofbytesread” is below fo equal then it will jump, if not then it will go to 00402019 (JBE= jump if below or equal- <a href="https://faydoc.tripod.com/cpu/jbe.htm">https://faydoc.tripod.com/cpu/jbe.htm</a>)</p><p>And then it will do a “strstr” which will find a “needle in a haystack” (<a href="https://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm">https://www.tutorialspoint.com/c_standard_library/c_function_strstr.htm</a> — so a string within a larger string) and in this case the string needle is “Sr&amp;w09.”</p><p>and then it calls strlen on it and compares it to “400h” and if it’s the lenght is above “400h” then it will jump to loc_40207C which is outside of the loop.<br>Also, a note, it won’t call “InternetCloseHandle” but it will if it fails (meaning, we’re going to close this connection because we did not get what we want, so let’s try another url and increase the parameter we are passing).</p><h3>Conclusion</h3><p>This may be a short write up but it did take me some time to analyse but with this information I want to try and debug the binary, get to the above and see if we can play around with the values to get past this loop.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74861208a8dc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Malware Analysis of a Cryptocurrency Miner — Part 3]]></title>
            <link>https://sechaq.medium.com/malware-analysis-of-a-cryptocurrency-miner-part-3-bd5f63baa476?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd5f63baa476</guid>
            <category><![CDATA[reverse-engineering]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[malware-analysis]]></category>
            <category><![CDATA[assembly-language]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Wed, 03 Feb 2021 12:11:08 GMT</pubDate>
            <atom:updated>2021-02-03T12:11:08.037Z</atom:updated>
            <content:encoded><![CDATA[<h3>Malware Analysis of a Cryptocurrency Miner — Part 3</h3><p>Static code analysis</p><p>In this write-up, we’ll load the binary file into IDA (free version) to disassemble it and do a static code analysis. And where to being then…</p><h3>Start</h3><p>Ok, I’ve loaded the binary file into IDA:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sFhK2OujwqoSbGfO0PWKDg.png" /></figure><p>So we can see it calls a subroutine (aka function) sub_401000:</p><blockquote>; Input SHA256 : 807126CBAE47C03C99590D081B82D5761E0B9C57A92736FC8516CF41BC564A7D<br>; Input MD5 : ABA2D86ED17F587EB6D57E6C75F64F05<br>; Input CRC32 : 7944603F</blockquote><blockquote>Format : Portable executable for 80386 (PE)<br>; Imagebase : 400000<br>; Timestamp : 56B664A6 (Sat Feb 06 21:24:54 2016)<br>; Section 1. (virtual address 00001000)<br>; Virtual size : 000137D0 ( 79824.)<br>; Section size in file : 00013800 ( 79872.)<br>; Offset to raw data for section: 00000400<br>; Flags 60500060: Text Data Executable Readable<br>; Alignment : 16 bytes</blockquote><blockquote>.text:00401000 sub_401000 proc near<br>.text:00401000<br>.text:00401000 lpTopLevelExceptionFilter= dword ptr -3Ch<br>.text:00401000 var_38= dword ptr -38h<br>.text:00401000 var_34= dword ptr -34h<br>.text:00401000 var_30= dword ptr -30h<br>.text:00401000 var_2C= dword ptr -2Ch<br>.text:00401000 var_14= byte ptr -14h<br>.text:00401000 var_10= dword ptr -10h<br>.text:00401000<br>.text:00401000 push ebx<br>.text:00401001 sub esp, 38h<br>.text:00401004 mov eax, ds:off_417D40<br>.text:00401009 test eax, eax<br>.text:0040100B jz short loc_401029</blockquote><p>We already know the information at the start, thanks to our static analysis.<br>We can see that this function is “near” the start function, indicating that it must be one of the first called/executed functions.</p><p>The “lpTopLevelExceptionFilter” is something to note as it is an anti-reversing technique employed by the Malware author. We will cover this in a moment.<br>After that we see variables being created but with no values.</p><p>Next, we see the “push ebx” which will push ebx onto the stack but I can’t see what ebx contains.<br>Further down we see “eax, ds:off_417D40” ( which stores the memory address at the data segment of off_417D40 into eax).<br>test eax, eax will then set the zero flag to 1, so that the jz short loc_401029 will be true, if the zero flag is set to 0 then it will jump to location 0040100D.</p><p>Now, the off_417D40 is the location of “tls_callback” which is used as an ant-debugging technique, (<a href="https://isc.sans.edu/diary/How+Malware+Defends+Itself+Using+TLS+Callback+Functions/6655">https://isc.sans.edu/diary/How+Malware+Defends+Itself+Using+TLS+Callback+Functions/6655</a>)<br>(Note- I am hoping to demonstrate these techniques when I go through the debugging process of this binary)</p><p>If we skip past these, we arrive at the following function: sub_40C350-</p><blockquote>.text:0040C350 sub_40C350 proc near<br>.text:0040C350 pushf<br>.text:0040C351 pushf<br>.text:0040C352 pop eax<br>.text:0040C353 mov edx, eax<br><strong>.text:0040C355 xor eax, 200000h</strong><br>.text:0040C35A push eax<br>.text:0040C35B popf<br>.text:0040C35C pushf<br>.text:0040C35D pop eax<br>.text:0040C35E popf<br>.text:0040C35F xor eax, edx<br>.text:0040C361 test eax, 200000h<br>.text:0040C366 jz locret_40C411</blockquote><p>Now in the above, I’ve highlighted xor, a bitwirse operation which will do the following — eax = eax ^200000h (at the moment I can’t tell what 200000h signifies) and then it will clear eax.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*tHHuJegRamrBgvrDm3fKBg.png" /></figure><p>Now, I’m not too sure what the binary is doing at the start here but if we look further down we see it is getting the cpuid and then testing it against 1 (and then 2,3,4 which you can’t see as I’ve not added it here :) )</p><p>It’s trying to compare the result of the cpuid dh (dh being the higher 8 bits of the ax register, which is the lower 16 bits of eax).<br>Also, after reading through <a href="https://c9x.me/x86/html/file_module_x86_id_45.html">https://c9x.me/x86/html/file_module_x86_id_45.html</a> to get more information on the above, I have a feeling that the xor eax, 2000000h is trying to find out the cpuid.<br>After the binary has found the “cpuid” it’s looking for, it then returns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/181/1*CmivN7y4KBYFpbZInwVNVg.png" /></figure><p>One thing I noted, was before the “rep retn”, we see pop ebx, which means the value at the top of the stack is popped to ebx, the above pop won’t happen if the very first “if” is not true (test exa, 2000000h”).<br>So I’ll rename this function to “get_cpuid”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/540/1*l-D11Yys6fM5U84slz4HGg.png" /></figure><p>The next set of instructions look to be setting the variables that have been declared above and then it calls “getmainargs” (<a href="https://docs.microsoft.com/en-us/cpp/c-runtime-library/getmainargs-wgetmainargs?view=msvc-160">https://docs.microsoft.com/en-us/cpp/c-runtime-library/getmainargs-wgetmainargs?view=msvc-160</a>) I noticed the return value for “getmainargs” — “0 if successful; a negative value if unsuccessful.”<br>Assuming that the return value is stored in “dword_41EA7C”, then copied into “eax”.<br>It will do “test eax,eax” to see if the zflag is set or not.</p><p>If it is calling “getmainargs”, that indicates that the binary is being fed arguments. Also if the zflag is not set, then it does the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*SJU_nuz447BqBkvFkD5Gkw.png" /></figure><p>We see ebx being utilized here, also. So I feel that this process is to set the correct environment variables for the target machine.</p><h3>End</h3><p>This is only the end for the starting analysis of this binary, going through it instruction by instruction and then writing about it will take a long time.<br>So in the next write-up, I’m only going to write up about what we saw during our dynamic analysis.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd5f63baa476" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Malware Analysis of a Cryptocurrency Miner— Part 2]]></title>
            <link>https://sechaq.medium.com/malware-analysis-of-a-cryptocurrency-miner-part-2-e68cc2bfe664?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/e68cc2bfe664</guid>
            <category><![CDATA[bitcoin-mining]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[monero]]></category>
            <category><![CDATA[malware-analysis]]></category>
            <category><![CDATA[malware]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Mon, 25 Jan 2021 12:10:43 GMT</pubDate>
            <atom:updated>2021-01-25T12:10:43.387Z</atom:updated>
            <content:encoded><![CDATA[<p>Dynamic analysis</p><p>So in the last write up, I did a static analysis of a “Bitcoin miner”, so the next step in our analysis process is to do dynamic analysis. <br>In the dynamic analysis, we’ll execute the malware within a safe environment and monitor it’s process, file system, registry, and network activity.</p><h3>Environment Set up</h3><p>It goes without saying but we’ll need to ensure we have a safe environment set up before we execute the malware and have all the monitoring tools ready before execution. I’m using Oracle VirtualBox and I’ve set up 2 VMs in host-only mode.</p><ol><li>A Linux VM which is running Ubuntu 20.10 and a static IP of 192.168.1.100 assigned. The Linux VM will have Wireshark listening on the “enp03s” interface and INETSIM running to simulate internet services.</li><li>A Windows 10 VM and a static IP of 192.168.1.50 assigned. The Windows VM will have process hacker and Noriben with Procmon running before I execute the malware. We could run more tools, also (like Regshot) but for now, I just want to keep it simple but effective. The Windows VM security policy to turn off real-time protection is enabled.</li></ol><h3>Executing the malware</h3><p>Now that the environment is set up, it’s time to execute the malware.</p><p>I first renamed the file from 02ca4397da55b3175aaa1ad2c99981e792f66151.bin to 02ca4397da55b3175aaa1ad2c99981e792f66151.exe<br>and the file icon changed from</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/296/1*ku6rIhhPbtF-U0pCoheaSA.png" /></figure><p>to</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/261/1*DQAOr20alJ5eG_GQC2wSdw.png" /></figure><p>So we can tell it’s designed to disguise itself as a folder to trick a user to double click it to open.</p><p>Now that’s running, let’s check process hacker:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/933/1*ZmOk_wrdMvdagRkjLWmyzA.png" /></figure><p>We can see it has started running as process but after that, I don’t see much else.<br>I let it run for about 1 minute and then terminate it from the process hacker.<br>After that, we can start to review the logging we had setup but just to add, I didn’t see anything else visually, which is something I was expected (but at the same time I was keeping an eye for anything unusual) because we didn’t see any dll imports or exports to indicate the binary using a GUI.</p><h4>Network logs</h4><p>I like to first look through the Wireshark logging to see the calls it made:</p><p>Let’s take a quick glance at some of the calls made:<br><em>“tshark -r file.pcap -Y http.request -T fields -e http.host -e http.request.full_uri”</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*yKQ5NDGITMSeqY2QZLgw1Q.png" /></figure><p>We can see it made a number of requests to .ru websites (remember from the static analysis we had seen some of these URLs) and they are all HTTP requests (port 80).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/958/1*oClUAncahF35-cM4FXg-_w.png" /></figure><p>We can see communication occurring between the two VMs but there is no weighty traffic.</p><p>If we look through Wireshark and filter the source to be the windows VM we see the following:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6I8IKB3m9v-axRUX6OD08Q.png" /></figure><p>We can see the binary has been sending a ‘GET’ request to ‘stafftest . ru’ and has been incrementing the first parameter by one and it changed the URL (the above screenshot shows the order of different URLs it was changing on every increment). We can tell that it has gone into a loop of ‘GET’ requests.<br>If we follow the TCP stream we see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/1*-sBD_quNVkSdluUR_JRU5w.png" /></figure><p>We can tell that the binary clearly doesn’t want an INETSIM HTML response, maybe that was the reason why it was in a loop because it did not get the response to meet it’s conditions.<br>A way for us to allow the binary to make a request safely to the internet is to set up a TOR proxy. This way the receiver won’t know the true source. (Another todo :) )</p><p>Ok, so after looking through it a bit more there wasn’t anything else the network logs revealed.</p><h4>Procmon logs</h4><p>So now let’s take a look through the process monitor logs we ran through Noriben.</p><p>Straight off the bat we see:<br><em>File Activity:<br>==================<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]<br>[CreateFile] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; %LocalAppData%\Microsoft\Windows\INetCache\IE\EA51LVPB\test[1].htm [File no longer exists]</em></p><p>This is normal as in the network logs we did see the binary make a ‘GET’ request to an a ‘test’ file, so it would have cached the file, which is normal, but we could say that since the binary did not get the file it was looking for, it removed it and kept trying (the loop we saw earlier).</p><p><em>Registry Activity:<br>RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\Cache\Content\CachePrefix = <br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\Cache\Cookies\CachePrefix = Cookie:<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\Cache\History\CachePrefix = Visited:<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\ProxyBypass = 1<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\IntranetName = 1<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\UNCAsIntranet = 1<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\AutoDetect = 0<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\ProxyBypass = 1<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\IntranetName = 1<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\UNCAsIntranet = 1<br>[RegSetValue] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\AutoDetect = 0</em></p><p>Unfortunately, we can’t gather heaps of information from the RegSetValue, why it’s setting these values, I don’t know, why it did it twice (maybe it’s in a loop), I don’t know but it trying to change the values under the “ZoneMap” would indicate it is going to set the values to allow the domain of the mining service as “allow” so that it’s not blocked by the targets machine.</p><h4>Network Traffic</h4><p><em>[TCP] 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768 &gt; 192.168.1.100:80<br>[TCP] 192.168.1.100:80 &gt; 02ca4397da55b3175aaa1ad2c99981e792f66151.exe:1768</em></p><p>We’ve already analysed this TCP connection via Wireshark but it’s good to know we didn’t miss any other connections, like UDP.<br>Also, we can see that the binary has an open port on 1768. We did see in the imports section from PEstudio that uses “InternetConnectionA” (<a href="https://docs.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-internetconnecta">https://docs.microsoft.com/en-us/windows/win32/api/wininet/nf-wininet-internetconnecta</a>) which is used for an open FTP and HTTP session, and from what we saw via Wireshark we know that it’s using it for a HTTP session waiting for the correct response from it’s “GET” request.</p><p>Ok, so we gained a hint of what the binary does thanks to dynamic analysis and what other techniques we could deploy if we were to execute it again (like use a TOR proxy).<br>But now we will need to dig deeper into the binary….</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e68cc2bfe664" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Malware Analysis — Part 1]]></title>
            <link>https://sechaq.medium.com/malware-analysis-part-1-37fb68b8da8c?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/37fb68b8da8c</guid>
            <category><![CDATA[bitcoin]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[malware-analysis]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[cybercrime]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Tue, 19 Jan 2021 18:04:49 GMT</pubDate>
            <atom:updated>2021-01-21T16:36:34.714Z</atom:updated>
            <content:encoded><![CDATA[<h3>Malware Analysis of a Cryptocurrency Miner— Part 1</h3><p>Static Analysis</p><p>In this new series, I’ll be going through the process of analysing malware starting off with static analysis.</p><p>The malware I will be analysing is a Bitcoin miner which I obtained from — <a href="https://github.com/fabrimagic72/malware-samples">https://github.com/fabrimagic72/malware-samples</a><br>The reason I chose the Bitcoin miner file is because of the current upsurge of the price and news of Bitcoin.</p><p>Most of the static analysis I do is already scripted in Python which you can see here:<br><a href="https://github.com/cchaq/MalwareAnalysis-MonnappaKA/blob/main/static_analysis_ch1.py">https://github.com/cchaq/MalwareAnalysis-MonnappaKA/blob/main/static_analysis_ch1.py</a></p><p>So I won’t be running through any of the code, this will just be an analysis of the results. Also, I go through this process on a Linux box but the same steps can be taken on a Windows machine using a multitude of malware analysis tools.</p><h3>Analysis of 02ca4397da55b3175aaa1ad2c99981e792f66151</h3><p>So the name of the Bitcoin miner file is called 02ca4397da55b3175aaa1ad2c99981e792f66151, which tells us nothing and that is expected if the threat actor wants to keep it ‘dark’.</p><ol><li>Let’s determine the file type —</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/934/1*V9S9ZembsC2wXhJ_T1Y1WA.png" /></figure><p>We can see it is a PE32 (Portable Executable) executable and it’s 32-bit. It says that it’s “stipped to external PDB” which means that the compiler builds a second program database but removes certain symbols. (<a href="https://docs.microsoft.com/en-us/cpp/build/reference/pdbstripped-strip-private-symbols?view=msvc-160">https://docs.microsoft.com/en-us/cpp/build/reference/pdbstripped-strip-private-symbols?view=msvc-160</a>).</p><p>Doing ‘xxd -g 1 file | more’, we see that the first 2 bytes are “4d 5a” which translates to ‘MZ’ (Mark Zbikowski).</p><p>2. Next, we’ll generate some cryptographic hashes of the file:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*NWL1QIVUcAyof88V0lszRQ.png" /></figure><p>We can use these hashes and run them through Virus total to see if we get any hits(Spoiler alert: We do). I’ve created a function in my script to query the Virus Total API but you will need your own key to access their API.</p><p>3. Now let’s run the “strings” program against the file to display the printable characters. Now I’ve run the strings program with the default settings and that spews out a lot of strings, but there are some interesting results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/115/1*7eGGo2JEXjYULbNHw9MT4Q.png" /></figure><p>We can see that this list comprises user names and passwords (www-data is a default web user for web servers) and ‘Admin’ ‘admin’ is normally used as default credentials for web services. This indicates some type of brute force to login into a system.<br>The ‘.ru’ is the Russian domain.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bR7tx2aQYvrMIsJ9e7H2Sw.png" /></figure><p>Here what I find most notable is “mine.moneropool” and Monero is a cryptocurrency, this indicates that this is built to mine Monero.<br>We can see it makes TCP connections, so when we do a dynamic analysis on this we should see these connections. <br>I’m not familiar with how to mine for Monero, but these API calls may be public but the string starting with “42n” may indicate that is a username. This is something that will need to be researched.<br>If that is a username or an authentication string, we could use that to do tracking.</p><p>We can see a “reg add”, so we know it adds to the registry.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/499/1*qpWwa_Uep-Ps2ckAOK0vBw.jpeg" /></figure><p>And here we can see a file path. If came across this sample without any prior knowledge, we can use what we learn from Strings to make a good assumption that it is a cryptocurrency miner.<br>But we must stay open-minded because this may be the first Matryoshka doll.</p><p>4. Now we will check to see if the file is packed:</p><p>‘upx -d -o cc_miner malware_file’ —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9mVJ2vQACSqsD4iVg8V4tA.png" /></figure><p>Well…..that was easy….</p><p>5. Now for a little of an in-depth look, let&#39;s take a look through the PE header!<br>Using ‘pe’, we can extract the dll, imports, exports, and so on.<br>Here is the list of libraries the binary is using:</p><p>A. wininet.dll<br>B. kernel32.dll<br>C. msvcrt.dll<br>D.shell32.dll</p><p>If there is a library that you are unfamiliar with, then you would search that library on Microsoft&#39;s website: <a href="https://docs.microsoft.com/en-us/troubleshoot/windows-client/deployment/dynamic-link-library#dll-troubleshooting-tools">https://docs.microsoft.com/en-us/troubleshoot/windows-client/deployment/dynamic-link-library#dll-troubleshooting-tools</a><br>Once you locate the library, on the right-hand side you can see the list of it’s functions. <br>The functions used by the malware binary can be seen under the imports table:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/302/1*vmpZN97tcSy2mus5UfyoTw.png" /></figure><p>This is not the full list but merely a snippet of the imports, but you can inspect them to get a good understanding of what the malware requires in order to achieve its goal. <br>Also it’s important to note what is not being used, for example, there are no imports to build a GUI, therefore we know that there is no UI to this binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/733/1*U43GxXMXEzvTMR18geWVbA.png" /></figure><p>I did run this binary through “pestudio” (just in case I missed something or my code needs updating) and we can see that it was 73 imports.<br>We could determine if a binary was packed if we saw a low number of imports, in this case, at this moment in time, I say it is not packed.</p><p>Now let’s take a look at the exports, which we can already see from above…there are none. <br>An executable and dll can export functions that can be used by other programs. Normally, the dll exports functions which are then imported by an executable. For example, if a threat actor was able to successfully replace a dll with a malicious dll on a victim&#39;s machine, then a running process could then import a function from the malicious dll, unknowing that it’s been replaced, and execute malicious code.</p><p>5.1. Now let’s examine the PE section table and sections:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/390/1*IffJ4r2eFsSfH5Kkrjx8rw.png" /><figcaption>The date at the top is the compile date and time.</figcaption></figure><p>Not very pretty I know, but it’s on my to-do list. So I went through the section using ‘pestudio’:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XDuwHSaT_tDUok4ki0j0vA.png" /></figure><p>A lot cleaner….</p><p>The first thing which I noticed that the raw-size and the virtual-size are roughly the same. Another indication that it’s not packed<br>And….there is this in the .rsrc (resources section):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/170/1*MtTtMGQ48q_Hbg5WSTAIqw.png" /></figure><p>In the resources tab in ‘pestudio’:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/1*ltPlrdalqPNVRlciCN0RWw.png" /></figure><p>To examine this further I need to run the binary through ‘resource hacker’ —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*eLiP89ivKO6lARJBzdJGaQ.png" /></figure><p>The hidden executable size is 90% of the original binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*yKZVXYnNSJ9dbBTHtBmQsA.png" /></figure><p>Well….looks like there is an executable packed into the malware binary we have been analysing.<br>We can use resource hacker to save the binary within ‘RCData’ and then load it into ‘pestudio’.<br>And there we have another binary we need analyse, but I won’t do that in this writeup.</p><h4>Conclusion</h4><p>From the static analyse we have determined that this is a Bitcoin (Well, Monero) miner (so no surprise there) and it contains an executable within its resource section that needs analysing. <br>Using what we learned we can write up YARA rules by using the results from “strings”, ‘pefile’ and to look for the hex of ‘MZ’ within the .rsrc file (using the offset).</p><p>So next up I will do a dynamic analyse of the binary file we’ve been analysing and using what I’ve learned here I can keep a close eye on certain actions the binary performs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=37fb68b8da8c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Evaluating the Performance]]></title>
            <link>https://sechaq.medium.com/evaluating-the-performance-4663aa3c4e85?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/4663aa3c4e85</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[malware-analysis]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[data-visualization]]></category>
            <category><![CDATA[malware]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Tue, 05 Jan 2021 15:22:26 GMT</pubDate>
            <atom:updated>2021-01-05T15:30:01.595Z</atom:updated>
            <content:encoded><![CDATA[<h3>Evaluating the Performance — Part 4</h3><p>We’ll need to evaluate the performance of the detector built to ensure that we are achieving a higher true positive rate than a false positive rate. Also as we increase the types of features built and used, we’ll need to monitor their performance.</p><h3>ROC Curve</h3><p>In order to evaluate the performance of the detector, we are going to use the Receiver Operating Characteristic (ROC) curve. We plot the false-positive rates against the true positive rates at various thresholds. This will help determine how to configure our detector to get the optimal settings. Detectors are not perfect, there will be false positives but we can use this method to reduce the false positive rate and increase our true positive rate. <br>When you think about the process and the possibilities then it seems like a never-ending story but we should look at it as evolving our detector.</p><p>As we implement our function to evaluate the detector performance, we will delve further into the requirements of the ROC curve and see the results.</p><h3>Evaluate function</h3><p>We have a function called cv_evaluate (remember, as always all the code is on my GitHub page):</p><blockquote>def cv_evaluate(X,y,hasher)</blockquote><p>We’ve known that “X” is the malware data, “y” is for the benign software data, and the hasher is set to “20000”<br>We’ll need to convert the training data (X, y) to “NumPy” arrays so that we can use NumPy&#39;s enhanced array indexing. (<a href="https://numpy.org/devdocs/user/quickstart.html">https://numpy.org/devdocs/user/quickstart.html</a>)</p><p>The type of evaluation we will be doing is “cross-validation”. Cross-validation allows you to split the training examples into x fold and run a test where we pit the folds against each other by rotating each fold which is done by running multiple tests. <br>Sklearn has a class called “KFold” which has the module “cross-validation” we will use. (<a href="https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.KFold.html">https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.KFold.html</a>)</p><blockquote>for train, test in KFold(len(X),3, shuffle=True):<br> training_X, training_y =X[train], y[train]<br> test_X, test_y = X[test], y[test]</blockquote><p>We pass the number of training examples into KFold, then we tell it we want 3 folds and to shuffle the data before dividing it into the folds.<br>Whilst in the loop, we will set our training X and y, test X, and y to the corresponding elements based on the result from KFold.</p><p>With our training data X and y set, we need to run it through our classifier (remember that we use the below to determine if it can tell if the binary features we passed into it is from malware of benign software.)</p><blockquote>classifier = ensemble.RandomForestClassifier()<br> classifier.fit(training_X, training_y)</blockquote><p>Now that we’ve fitted the data in, we can now get the scores:</p><blockquote>scores = classifier.predict_proba(test_X)[:,-1]<br>fpr, tpr, thresholds = metrics.roc_curve(test_y, scores)</blockquote><p>“fpr” = false positive rate and “tpr” = true positive rates</p><p>With our scores, we use “pyplot” from “matplotlib” to build our graph to see the outcome.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*jmedx1xnL_JpMbVnDWyyZQ.jpeg" /></figure><p>Now…we just need to run like a gazillion tests…..</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4663aa3c4e85" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Applying Data Science to Malware — Part 3]]></title>
            <link>https://sechaq.medium.com/applying-data-science-to-malware-part-3-6a535cd0dc2d?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a535cd0dc2d</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Wed, 30 Dec 2020 14:03:41 GMT</pubDate>
            <atom:updated>2020-12-30T14:03:41.204Z</atom:updated>
            <content:encoded><![CDATA[<h3>Applying Data Science to Malware — Part 3</h3><p>Now we will build a machine learning detector. In order to build a machine learning detector, we need to extract a substantial amount of features from our software binary, not just malware because the point of the detector is to determine whether the software binary is malicious or benign. <br>But at this moment in time, I’m only using the strings feature, in the future I plan to add more features.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hgZWJnzomM82b1VLzx1cmg.jpeg" /></figure><h3>Strings feature</h3><blockquote>def get_string_features(path,hasher):<br> chars = r” -~”<br> min_length = 5<br> string_regexp = ‘[%s]{%d,}’ % (chars, min_length)<br> file_object = open(path)<br> data = file_object.read()<br> pattern = re.compile(string_regexp)<br> strings= pattern.findall(data)</blockquote><blockquote>string_features = {}<br> for string in strings:<br> string_features[string] = 1</blockquote><blockquote>hashed_features = hasher.transform([string_features])</blockquote><blockquote>hashed_features = hashed_features.todense()<br> hashed_features = numpy.asarray(hashed_features)<br> hashed_features = hashed_features[0]</blockquote><blockquote>print “Extracted {0} strings from {1}”.format(len(string_features),path)<br> return hashed_features</blockquote><p>We start off by defining our function that has 2 parameters, the path, and a hasher. A hasher is a feature of the sklearn library.</p><h4>Sklearn</h4><p>Sklearn is short for Scikit-learn which is a highly popular open-source machine learning package. You can learn more about it here: <a href="https://scikit-learn.org/stable/getting_started.html">https://scikit-learn.org/stable/getting_started.html</a></p><p>The hashing library allows us to compress an enormous amount of features down to a smaller chuck. This is so that your hardware can handle the amount of data being processed. 4000 compressed features vs 1 million will result in a big difference.<br>We then want to extract all the strings from the file passed through, but we only want strings that are 5+ characters long. <br>We’ll then have a for loop to go through all the strings extracted based on our above rule and for each string, we’ll store it into our “string_features” dictionary with a value of “1” to say that the feature is present in the software binary. Also to use the sklearn hasher feature, it requires a list of dictionaries</p><blockquote>hashed_features = hasher.transform([string_features])</blockquote><p>After that, we change the hashed_features structure back into a normal NumPy vector and then we return it.</p><h3>Train detector</h3><p>Now that we have built the strings feature, we can now build our function to extract the data from the software binaries we pass and train our detector.</p><blockquote>def train_detector(benign_path, malicious_path, hasher):<br> def get_training_paths(directory):<br> targets = []<br> for path in os.listdir(directory):<br> targets.append(os.path.join(directory,path))<br> return targets<br> malicious_paths = get_training_paths(malicious_path)<br> benign_paths = get_training_paths(benign_path)<br> X = [get_string_features(path,hasher) for path in malicious_paths + benign_paths]<br> y = [1 for i in range(len(malicious_paths))] + [0 for i in range(len(benign_paths))]<br># print X<br> #print y<br> return X,y</blockquote><blockquote>#classifier = tree.DecisionTreeClassifier()<br> classifier = ensemble.RandomForestClassifier(64)<br> classifier.fit(X,y)<br> pickle.dump((classifier,hasher),open(“saved_detector.pkl”,”w+”))</blockquote><p>Our function here takes 3 parameters, the first for the “non-malicious” binary, the second for Malware, and the third is our hasher (which is defined later).</p><p>Like before, we’ll need to create the absolute file path for each file within the directory we supply and they will be our targets but this will be a sub-function(helper function) of the “train_dectector” function.<br>And straight after you can see we use it to get all the absolute file paths for both the “benign_path” and “malicious_path”.</p><p>Now we can extract our feature for the supplied path to create our label <em>vector</em>.</p><h4>Vectors</h4><p>A vector in machine learning(ML) is arrays of numbers where each index corresponds to a single feature. An example of this from above is an extracted string set to “1” to say it exists. We could set it to “0” to say it does not exist in another scenario.</p><p>In our example, we have two vectors, X and y. X is the features vector (the features returning from “get_string_features” and y being the label vector, which will label each string to its corresponding binary to say whether it is a malware binary or benign.</p><p>Next, we build our decision tree. I won’t delve into how decision trees work but know that they can be used for detection. This is done by the decision tree asking a series of question, for example, does the binary contain 50%+ strings that match to our known malware strings, if yes then follow this path of questions else go this path. That is the way I look at it for now to keep it simple.</p><blockquote>#classifier = tree.DecisionTreeClassifier()<br> classifier = ensemble.RandomForestClassifier(64)</blockquote><p>Once we decided which decision tree we want use (a random forest is a collection of many decision trees), we’ll pass X and y into it and that will train it.</p><blockquote>classifier.fit(X,y)</blockquote><p>And we’ll save our detector and hasher using the Python pickle module.</p><blockquote>pickle.dump((classifier,hasher),open(“saved_detector.pkl”,”w+”))</blockquote><h3>Scan file</h3><p>Now we’ll write a function to take a binary and check it against our trained dectector to see if we can tell if it is malicous or benign.</p><blockquote>def scan_file(path):<br> if not os.path.exists(“saved_detector.pkl”):<br> print “Train a dectector before scanning files.”<br> sys.exit(1)<br> with open(“saved_detector.pkl”) as saved_detector: <br> classifier, hasher = pickle.load(saved_detector)<br> features = get_string_features(path,hasher)<br> result_proba = classifier.predict_proba([features])[:,1]</blockquote><blockquote>if result_proba &gt; 0.5:<br> print “it appears this faile is malicious!!”, `result_proba`<br> else:<br> print “it appears this file is benign”, `result_proba`</blockquote><p>Most of it is self explanatory but the main bit I want to cover is:</p><blockquote>classifier, hasher = pickle.load(saved_detector)<br> features = get_string_features(path,hasher)<br> result_proba = classifier.predict_proba(features)[1]</blockquote><p>We set our classifier and hasher to our train dectector by using the pickle.load method.<br>We’ll run our “get_string_features” against the new binary with our original hasher and set that to the local features variable.<br>We’ll pass that to our classifier (using the random forest method) to predict the probability of the binary being malware or benign.</p><h3>Put to the test</h3><p>Now let’s test the detector out, I passed through the same malware samples in my previous posts and some random benign binary that was on my machine.</p><p>That will train our detector to tell the differance between the two supplied path, and now let’s pass through a malware sample to see the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*k8fg2pv9jtMViCdcrQfFjg.png" /></figure><p>………….Hmm………</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a535cd0dc2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Applying Data Science to Malware — Part 2]]></title>
            <link>https://sechaq.medium.com/applying-data-science-to-malware-part-2-496209a90de9?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/496209a90de9</guid>
            <category><![CDATA[ransomware]]></category>
            <category><![CDATA[malware-analysis]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Tue, 08 Dec 2020 16:51:55 GMT</pubDate>
            <atom:updated>2020-12-10T13:35:30.110Z</atom:updated>
            <content:encoded><![CDATA[<h3>Applying Data Science to Malware — Part 2</h3><p>Shared code analysis</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*ZTIDWVMKu7SIKnT5kGZARA.png" /></figure><p>In the last section, I wrote about building networks and producing a visual graph that shows the connections between Malware.<br>In this section, I will go through the script where we create a system that will show the links between Malware based on shared code analysis.</p><h3>Terminology</h3><p>Before we start to build the system, we first need to understand the following:<br>1. Jaccard index<br>2. Minhashes</p><h4>Jaccard index</h4><p>The Jaccard index is quite simple, it is worked out by diving the total of shared attributes (between malware) and the total attributes.<br>For example:<br>Jaccard index = 0.5 when shared attributes (5) / total attributes (10).<br>Now, this is useful for small data sets, but when we want to compare large data sets then we turn to “minhashes”.</p><h4>Minhashes</h4><p>Now Minhashes isn’t so simple.</p><p>A minhash is a technique used to estimate the similarity of two sets. <br>Our minhash is a malware sample’s feature (in our below system the features will be the results from “strings”) and is hashed with <em>k </em>hash function and we take the minimum value of the hashes from all the features that were hashed, this is to reduce the set of malware features to a fixed size array of <em>k </em>integers, which we call minhashes. <br>With the minhashes, we can calculate our “Jaccard index” between two samples. We just check how many minhashes match and divide that by <em>k</em>. <br>Hopefully, once I show the code and talk through it, it will become more apparent what a minhash is. But one of the main reasons for using minhash is because it’s faster than using the Jaccard method when we have a large dataset.</p><p>A good video for a more in-depth look into the above: <a href="https://www.youtube.com/watch?v=aTwRpqUnQX8">https://www.youtube.com/watch?v=aTwRpqUnQX8</a></p><h3>Building the system</h3><p>The malware will be analyzed based on their strings features (In the future we will add different features to this system, but let&#39;s go one step at a time).</p><blockquote>import sys<br>import argparse<br>import os<br>import murmur<br>import shelve<br>import numpy as np<br>from similarity_graph import *</blockquote><blockquote>NUM_MINHASHES = 256<br>SKETCH_RATIO = 8</blockquote><p>The “similarity_graph” is the code from part 1, as we will reuse the same functions for checking if the file is a PE file and getting the strings.<br>The minhash and sketch ratio (sketching is used with minhash) is set based on the values from the book Malware data science.</p><p>Now like before, I won’t post all the code I used to build the system as I want to talk about the main features used to build it, but you can find it on my Github — <a href="https://github.com/cchaq/MalwareDataScience">https://github.com/cchaq/MalwareDataScience</a></p><p>First up, the minhash function:</p><blockquote>def minhash(features):<br> minhashes = []<br> sketches = []<br> for num_minhash in range(NUM_MINHASHES):<br> minhashes.append(<br> min([murmur.string_hash(‘feature’, num_minhash) for feature in features])<br> )<br> for i in xrange(0,NUM_MINHASHES,SKETCH_RATIO):<br> sketch = murmur.string_hash(‘minhashes[i:i+SKETCH_RATIO]’)<br> sketches.append(sketch)<br> return np.array(minhashes),sketches</blockquote><p>We have a “features” parameter which in this instance will be from our “strings” result (which we’ll go through later).<br>We’ll create our minhashes and sketches array because we’ll want to add these to our database.</p><p>The for loop will iterate through NUM_MINHASHES, so 256 times, and append our minhashes array with a hashed feature.<br>For hashing the murmur library is used, after reading about it, it is a good hashing library to use because it’s fast (other reasons as well but speed is the main focus here). A quick run-through of the murmur.string_hash function reveals how our “hashed” features will be stored:</p><blockquote>&gt;&gt;&gt; import murmur<br>&gt;&gt;&gt; murmur.string_hash(‘feature’,1)<br>3486256588</blockquote><p>Once we’ve gone through the number of hashes (256), we’ll take the minimum hash value and add it to our array.</p><p>Next, we iterate through the minhashes and use them to create our sketches. A sketch is a hash of multiple minhashes, in this case, we set the ratio to 8, which we use for database indexing of our malware samples. This will speed up the retrieval process of malware that are likely to be similar to one another. We have to remember that this database could grow considerably in size, which is why there is indexing.</p><h3>Building the database</h3><p>Now, let’s build the database.</p><blockquote>def store_sample(path):<br> db = get_database()<br> features = getstrings(path)<br> minhashes, sketches = minhash(features)</blockquote><blockquote>for sketch in sketches:<br> sketch = str(sketch)<br> if not sketch in db:<br> db[sketch] = set([path])<br> else:<br> obj = db[sketch]<br> obj.add(path)<br> db[sketch] = obj<br> db[path] = {‘minhashes’:minhashes,’comments’:[]}<br> db.sync()</blockquote><blockquote>print “Extracted {0} features from {1}…”.format(len(features),path)</blockquote><p>Remember, the “getstrings” function is from the “similarity_graph” script we created in the previous section. This will build our list of features (the result from “strings”) and that is passed into the minhash function which we covered above.</p><p>We iterate over our sketches and add if it does not exist in the database, we create the record and use the sample malware file path as the ID.<br>Now, if the sketch does exist then we add the sample malware file path to the sketch’s set of associated sample paths. <br>After adding the sketches to the database with the path as the ID, we set our minhashes to the path.</p><h4>Searching for similar malware samples</h4><p>The code:</p><blockquote>def search_sample(path):<br> db = get_database()<br> features = getstrings(path)<br> minhashes, sketches = minhash(features)<br> neighbours = []</blockquote><blockquote>for sketch in sketches:<br> sketch = str(sketch)</blockquote><blockquote>if not sketch in db:<br> continue</blockquote><blockquote>for neighbour_path in db[sketch]:<br> neighbour_minhashes = db[neighbour_path][‘minhashes’]<br> similarity = (neighbour_minhashes == minhashes).sum() / float(NUM_MINHASHES)<br> neighbours.append((neighbour_path, similarity))</blockquote><blockquote>neighbours = list(set(neighbours))<br> neighbours.sort(key=lambda entry:entry[1],reverse=True)<br> print “”<br> print “Sample name”.ljust(64),”Shared code estimate”<br> for neighbour, similarity in neighbours:<br> short_neighbour = neighbour.split(“/”)[-1]<br> comments = db[neighbour][‘comments’]<br> print str(“[*] “+short_neighbour).ljust(64),similarity<br> for comment in comments:<br> print “\t[comment]”,comment</blockquote><p>With this function, we can pass compare malware samples without having to load them to our database, although having to go through the process of getting the hashes for malware samples we passed through again is a redundant process, something to work on in the future.</p><p>We iterate over the malware sample sketches and for each sketch, we will look up the stored malware samples.<br>After that, we’ll work out the Jaccard index:</p><blockquote>similarity = (neighbour_minhashes == minhashes).sum() / float(NUM_MINHASHES)</blockquote><p>and append the results to our neighbour’s array.</p><h3>The results</h3><p>Ok, so I mentioned the main functions of the system except one which is to search for the samples, but I will do a separate write up for that as it’s could make this write up a bit long-winded.<br>Now you will also see some seem arguments passed in when I run the script and if you want to see how they are implemented then please check out the full code on my GitHub page — <a href="https://github.com/cchaq/MalwareDataScience">https://github.com/cchaq/MalwareDataScience</a></p><p>First, we will load our malware samples features into the database:</p><blockquote>python minhash_relation.py -l /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted<strong><br>Extracted 8388 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-d1674sc2.bin…<br>Extracted 7538 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-fvd4o59p.exe…<br>Extracted 1793 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-jfpzku0b.bin…<br>Extracted 632 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/1d4322dbad293847de14eca09bee5056eaede7ce178490e101642bf1f5875e37…<br>Extracted 9227 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-ij2n4cyd.bin…<br>Extracted 6032 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-gv5k5anv.bin…<br>Extracted 3602 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-e0y16y2p.bin…<br>Extracted 305 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-ojjfqxul.bin…<br>Extracted 44463 features from /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/32f24601153be0885f11d62e0a8a2f0280a2034fc981d8184180c5d3b1b9e8cf.bin…</strong></blockquote><p>If we open our database file via the Python command line and print it’s data, this is what we see:</p><blockquote>&gt;&gt;&gt; import shelve<br>&gt;&gt;&gt; db = shelve.open(“samples.db”)<br>&gt;&gt;&gt; dbkeys = list(db.keys())<br>&gt;&gt;&gt; for key in dbbeys:<br>… print (key,db[key])</blockquote><blockquote>(‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/146581f0b3fbe00026ee3ebe68797b0e57f39d1d8aecc99fdc3290e9cfadc4fc.bin’, {‘minhashes’: array([231<br>5179632, 3486256588, 1845446934, 574354670, 3634204494,<br> 3868613078, 3316310169, 730525171, 545429338, 4253172697,<br> 2757105328, 408003201, 217562801, 1661354022, 2763938731,<br> 1059248515, 2107807121, 1885863305, 3307288677, 1587378795,<br> 27164293, 3793397666, 400853354, 2192977244, 2594248640,<br> 2141616303, 3335467927, 1221082220, 203908147, 2346593753,</blockquote><blockquote>(‘2843819777’, set([‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/32f24601153be0885f11d62e0a8a2f0280a2034fc981d8184180c5d3b1b9e8cf.bin’, ‘/hom<br>e/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-ij2n4cyd.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-fvd4o59p.exe’, <br>‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-3kn32w1v.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-0e89k3id.e<br>xe’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/satan.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-d1674sc2.bin<br>‘, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/697158bcade7373ccc9e52ea1171d780988fc845d2b696898654e18954578920’, ‘/home/osboxes/myAnalysis/<br>malware-samples/Ransomware/extracted/smb-gv5k5anv.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-y16ftv9_.bin’, ‘/home/osboxes/myAnal<br>ysis/malware-samples/Ransomware/extracted/smb-kmnr7qja.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-e0y16y2p.bin’, ‘/home/osboxes/m<br>yAnalysis/malware-samples/Ransomware/extracted/smb-b4tq2hti.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-gab_1g0l.bin’, ‘/home/osbo<br>xes/myAnalysis/malware-samples/Ransomware/extracted/smb-tkas_857.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/mssecsvc.bin’, ‘/home/osb<br>oxes/myAnalysis/malware-samples/Ransomware/extracted/smb-82rfim2h.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-jfpzku0b.bin’, ‘/hom<br>e/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-7rwkaozq.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/027cc450ef5f8c5f653<br>329641ec1fed91f694e0d229928963b30f6b0d7d3a745’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-z7uhqxx6.exe’, ‘/home/osboxes/myAnalysis/mal<br>ware-samples/Ransomware/extracted/ed01ebfbc9eb5bbea545af4d01bf5f1071661840480439c6e5babe8e080e41aa.bin/ed01ebfbc9eb5bbea545af4d01bf5f1071661840480439c6e5babe8e<br>080e41aa.bin (1)’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/smb-lyqgstbu.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extra<br>cted/smb-oat1c4ef.exe’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/86e0eac8c5ce70c4b839ef18af5231b5f92e292b81e440193cdbdc7ed108049f.bin’, ‘<br>/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/1d4322dbad293847de14eca09bee5056eaede7ce178490e101642bf1f5875e37’, ‘/home/osboxes/myAnalysis/malw<br>are-samples/Ransomware/extracted/smb-ojjfqxul.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware/extracted/mssecsvc_1.bin’, ‘/home/osboxes/myAnalysis/m<br>alware-samples/Ransomware/extracted/146581f0b3fbe00026ee3ebe68797b0e57f39d1d8aecc99fdc3290e9cfadc4fc.bin’, ‘/home/osboxes/myAnalysis/malware-samples/Ransomware<br>/extracted/smb-vasyl9yj.bin’]))</blockquote><p>Now that we have loaded the Malware samples hashes, let’s test the results of the shared code estimate:</p><blockquote>python minhash_relation.py -s ~/myAnalysis/malware-samples/Ransomware/extracted/smb-82rfim2h.bin</blockquote><p>Oh, I forgot to mention that the samples I load are the same as my previous post.</p><p>And the result:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/620/1*JwVjj5W0U7NycxdUkAlqzA.png" /><figcaption>Shared code estimate for smb-82rfim2h.bin</figcaption></figure><p>We can see that it shares a code estimate of 1 with itself (no surprise there) and the next Malware sample it has a close relationship with is “smb-0e89k3id.exe”<br>After that, the numbers start to drop but we can still see that the Ransomeware sample does share some common “features” (that being the result from our “strings”.</p><p>Now, what would be interesting to see is if we start to grow our feature list, what kind of results would it yield and by building a database, everything we add new Malware we can see if it shares code with previously loaded Malware or if it’s from a new group.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=496209a90de9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Applying Data Science to Malware — 1]]></title>
            <link>https://sechaq.medium.com/applying-data-science-to-malware-1-fe4a230378d7?source=rss-177836e78f66------2</link>
            <guid isPermaLink="false">https://medium.com/p/fe4a230378d7</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[ransomeware]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[SecHaq]]></dc:creator>
            <pubDate>Mon, 30 Nov 2020 13:56:55 GMT</pubDate>
            <atom:updated>2021-01-07T17:21:19.612Z</atom:updated>
            <content:encoded><![CDATA[<h3>Applying Data Science to Malware —Part 1</h3><p>With Malware exploding in numbers, I decided to learn and apply Data Science to Malware.</p><p>So first I need a number of Malware samples, which I obtained from <a href="https://github.com/fabrimagic72/malware-samples">https://github.com/fabrimagic72/malware-samples</a></p><p>Now the following techniques can work on any set of Malware, maybe if your a business/organization who is being targeted or you’ve been following a certain group of Malware authors and you want to see how the Malware is connected, if they use the same resources, hosts, code, etc then that would yield some interesting data and start to paint a picture.</p><p>Unfortunately, I don’t have access to those sets of Malware but that doesn’t say we can’t apply the techniques to Malware collected from honeypots.</p><h3>Ransomeware samples</h3><p>From the Malware samples, the Ransomware folder looks to have a number of samples we could apply the techniques on.</p><blockquote>Step one: unzip all the Malware within that dir:<br>find . -name “*.zip” | while read filename; do 7z x $filename -pinfected -aou; done;</blockquote><p>Step two: start building the script</p><p>Now I won’t post the whole script on here, I’ll add a link at the bottom it once I put it up on Github.<br>So let’s take a look at the interesting stuff:</p><blockquote>for root,dirs,files in os.walk(args.target_path):<br> for path in files:<br> #try opening the file with pe to see if it’s really a pe file<br> try:<br> pe = pefile.PE(os.path.join(root,path))<br> except pefile.PEFormatError:<br> continue<br> fullpath = os.path.join(root,path)<br> #extract printable strings from the target sample<br> strings = os.popen(“strings ‘{0}’”.format(fullpath)).read()<br>#use the search_doc function in the included reg mod, to find hostnames<br> hostnames = find_hostname(strings)<br> if len(hostnames):<br> #add the nodes and edges for the bipartite network<br> network.add_node(path,label=path[:32],color=’black’,penwidth=5,bipartite=0)</blockquote><blockquote>for hostname in hostnames:<br> network.add_node(hostname,label=hostname,color=’blue’,penwidth=10,bipartite=1)<br> network.add_edge(hostname,path,penwidth=2) #NOTE WE HAVE EXTRACTED ALL MALWARE INTO ONE FOLDER</blockquote><blockquote>if hostnames:<br> print “extracted hostnames from:”,path<br> pprint.pprint(hostnames)</blockquote><p>What this does, is looks through each file in the given directory, check if it has a PE header if so we run the program “strings” on it, then get the list of strings from the file and run it through a function called “find_hostname” (which I’ve not posted here, but it goes through a regex process to strip the input and run the list through a list of domain suffixes to say if it string matches a list within domain suffixes, then it is accepted as a domain)</p><p>Then we create our network.</p><p>If we have a positive list of hostname, we’ll create a node for that malware.</p><blockquote>network.add_node(path,label=path[:32],color=’black’,penwidth=5,bipartite=0)</blockquote><p>Now we’ll start to create nodes and edges for each hostname we find that is connected to that malware.</p><blockquote>network.add_node(hostname,label=hostname,color=’blue’,penwidth=10,bipartite=1)<br> network.add_edge(hostname,path,penwidth=2)</blockquote><p>And then print the hostname to the screen.<br>And the results are, to me anyway, interesting. We can see the following hostnames:</p><blockquote><em>extracted hostnames from smb-b4tq2hti.bin<br>[‘mnses7xf743znk7.onion’,<br> ‘r5x6sdidz4q7f6q.onion’,<br> ‘sw7xmbs2ivmt5og.onion’,<br>Note — I have removed some characters from the hostname, safety first :)</em></blockquote><p>Now let’s save everything to a “.dot” file so that we can convert the network into a visual graph.</p><blockquote>#write the dot file to disk<br>write_dot(network,args.output_file)<br>malware = set(n for n, d in network.nodes(data=True) if d[‘bipartite’]==0)<br>hostname = set(network)-malware</blockquote><blockquote>#use networkX’s bipartite network projection function to produce the malware and hostname projections</blockquote><blockquote>malware_network = bipartite.projected_graph(network, malware)<br>hostname_network = bipartite.projected_graph(network, hostname)</blockquote><blockquote>#write the projected networks to disk as specified by the user<br>write_dot(malware_network, args.malware_projection)<br>write_dot(hostname_network,args.hostname_projection)</blockquote><p>So we will have 3 files, the first being the whole network, the second to show the connection between the Malware, and the third to show the connections between the hostnames.</p><blockquote>python ransomewareMalwareNetwork.py /home/osboxes/myAnalysis/malware-samples/Ransomware/extracted ./orginal.dot ./malwareProjection.dot ./hostname_projection.dot</blockquote><p>We then use fdp (one of many choices but it is suited for a small network) to convert the “.dot” files into images:</p><blockquote>fdp orginal.dot -T png -o orignal_ransomeware_image.png</blockquote><p>And now let’s view the results:</p><p>Original network, showing the connection between the Malware and hostnames:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*QjnfeOfwU4vR0MdwOgf7PA.png" /><figcaption>A network of Ransomeware and hostnames</figcaption></figure><p>The blue circle represents hostnames and the black circle represents the Malware. Now, granted it’s quite hard to actually see the connection via Medium but this was something I was hoping to see.<br>The cluster on the left is from the “Wannacry” folder and we would expect to see those files and hostnames linked.</p><p>Next, let’s view just the Malware connections:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*D44yqLKlXRynD1sguXZ_nQ.png" /><figcaption>Malware projection</figcaption></figure><p>And the hostname projection:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gBcVhGWvXzFboXL_Cnr2pg.png" /><figcaption>Hostname projection</figcaption></figure><p>Now, visually the hostname doesn’t tell us much, so that’s going to take me some time to adjust the network for the hostname to get it to be more visually pleasing and useful.</p><p>I won’t do a review of my findings as this is just to apply what I learn to some real-world Malware. And I’m quite happy with the findings and itching to see how else we can use the data we learn from Malware via Malware analysis and add it to these methods.</p><p>Now, I did try to build a graph based on image relationship for the ransomware malware, which is done by extracting the images from the malware but the results were far less “exciting” but that could be because the malware doesn’t use images or it is obfuscated.<br>Either way, we can use the same methods on different samples to see what they yeild.</p><p>Everything I learned and applied in the above is from the book “Malware Data Science”. I highly recommend it.</p><p><a href="https://github.com/cchaq/MalwareDataScience">https://github.com/cchaq/MalwareDataScience</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fe4a230378d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>