<?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 Nir Chako on Medium]]></title>
        <description><![CDATA[Stories by Nir Chako on Medium]]></description>
        <link>https://medium.com/@C_h4ck_0?source=rss-33fd60f064de------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*0v2shJhmo5-3rLwz.</url>
            <title>Stories by Nir Chako on Medium</title>
            <link>https://medium.com/@C_h4ck_0?source=rss-33fd60f064de------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 00:48:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@C_h4ck_0/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[Your network through the eyes of a hacker]]></title>
            <link>https://medium.com/cyberark-engineering/your-network-through-the-eyes-of-a-hacker-7231f421d97a?source=rss-33fd60f064de------2</link>
            <guid isPermaLink="false">https://medium.com/p/7231f421d97a</guid>
            <category><![CDATA[cyber]]></category>
            <category><![CDATA[network]]></category>
            <category><![CDATA[dns]]></category>
            <category><![CDATA[hacker]]></category>
            <category><![CDATA[idp]]></category>
            <dc:creator><![CDATA[Nir Chako]]></dc:creator>
            <pubDate>Thu, 27 Aug 2020 07:23:10 GMT</pubDate>
            <atom:updated>2020-08-27T11:30:05.338Z</atom:updated>
            <content:encoded><![CDATA[<h3><strong>Your Network Through The Eyes of a Hacker</strong></h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v_QeF6neU2RGhg0QyWukmw.jpeg" /></figure><p>I’m sure your network’s security is top-notch.</p><p>You must have already taken care of micro-segmentations, strict firewall policies, and have some kind of EDR solution on the different endpoints. And of course, you’re only allowing secure connections that rely on encrypted protocols.</p><p>If you are one of those who take security seriously, you might even run a regular security update routine. And if that’s not enough, you probably also have a qualified SOC team that monitors all of those systems 24/7. Your SOC team submits a report each week that shows several security events that appeared in the different systems but were all examined and characterized as false-positives.</p><p>Having said all that — do you feel secure?</p><p>In this post, you’re going to learn three stealthy attack techniques you should anticipate and work to avoid. More than that, it’s important that you learn how to expand this knowledge in a way that allows you to protect your network. In order to do that, we will first dive into the mind of a hacker and understand their strategy.</p><h3><strong>Hacker’s perspective</strong></h3><p>As a white-hat hacker and former red teamer, I can tell you that one of my main goals in an operation is to stay secretive. Staying secretive is an important ingredient of an APT (Advanced Persistent Threat) attack, which features a stealthy threat actor.</p><h4><strong>The Art of Cyber Warfare</strong></h4><p>If I had to highlight one key feature of a skilled attacker, it would be — patience.</p><blockquote><em>“If it is to your advantage, make a forward move. If not, stay where you are.” — Sun Tzu, The Art of War</em></blockquote><p>An attacker who succeeded in passing your first line of defense and gained initial access will strive to evade existing defense systems. They will not make a move until they are certain about it. And more than that, when they do make a move — they will make sure that you are unaware of it.</p><blockquote><em>“Make your way by unexpected routes and attack unguarded spots.” — Sun Tzu, The Art of War</em></blockquote><h4><strong>Strolling in the Dark</strong></h4><p>It all depends on the attacker’s intelligence data.</p><p>The easy scenario is when the attacker has enough information to make a decision about their moves and plans ahead.</p><p>The more problematic scenario is when there’s not enough intelligence data. In this situation, the best way for an attacker to remain under-the-radar is to utilize the “must-have” features of an organizational network, such as shared folders, ping messages, and DNS queries.</p><p>Why would an attacker use those? Because these are protocols that are very frequently used, so it doesn’t require pre-operation intelligence. They shed light on unknown paths.</p><p>Even if one of your defense systems catches something unusual, chances are your SOC team will have a hard time investigating exactly what happened. Searching for an unusual pattern in the network traffic of these protocols is like looking for a needle in a haystack.</p><p>In the upcoming section, I’ll explain 3 examples of common protocols used by an attacker to their advantage:</p><p>1. <strong>SMB protocol</strong> — enables the shared network folder</p><p>2. <strong>DNS protocol</strong> — enables the translation of domain names to IP addresses</p><p>3. <strong>ICMP protocol</strong> — enables, inter alia, sending ‘ping’ messages</p><h3><strong>Let’s talk business</strong></h3><h4><strong>SMB and shared network folders</strong></h4><p>It’s a common feature of most networks to have some kind of a shared drive, to allow users to share files with each other and to have a backup of a locally saved file.</p><p>This service will be open, most of the time, to a wide range of users and systems. It can, therefore, be used as a tool for breaking down your micro-segmentation and firewall rules.</p><p>The way a hacker would use a shared drive is by creating a file in a shared folder that will act as a “pipeline” between a Command &amp; Control (C&amp;C) server and an agent. Even if the agent is running on a machine that isn’t allowed to speak with the C&amp;C, it’s more than likely that the two of them can find a shared folder where they can share such a “pipeline” file with each other.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*mCtvVvwpBJD2MLGkoAFsqg.png" /><figcaption><strong><em>Figure 1.</em> </strong><em>Breaking network segmentation using shared network folders</em></figcaption></figure><h4><strong>DNS beacons</strong></h4><p><a href="https://www.cobaltstrike.com/help-dns-beacon">DNS beacons</a> are a well-known technique used to hide the payloads of a session between a C&amp;C server and its agents.</p><p>It uses the DNS protocol to bypass firewall rules and relies on the massive amount of DNS queries to “blend into the crowd”.</p><p>How does it work?<br> <br> The agent sends a DNS query to the local DNS server. The query looks like a machine asking for a domain name translation, when in fact, the name server that answers those queries is controlled by the attacker.</p><p>In this way, the attacker can respond with any desired payload, in the DNS TXT type record for example, and that will reach the agent.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8Lqyx0ONXyA6zXVLPjrOuQ.png" /><figcaption><strong><em>Figure 2.</em> </strong><em>Bypassing firewall rules using DNS queries</em></figcaption></figure><p>Just imagine your crown jewel DB server, leaking the most valuable information, drop by drop, one DNS query after another.</p><h4><strong>ICMP tunneling</strong></h4><p>Last but not least is a <a href="https://medium.com/bugbountywriteup/ping-power-icmp-tunnel-31e2abb2aaea">simple technique</a> in which a hacker uses the payload section of the ICMP ping packet in order to pass whatever payload they would like.</p><p>Regular Linux ping echo message -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*sHszA-Vy3s4ASexGLA0grA.png" /><figcaption><strong>Figure 3. </strong>Ping Echo message</figcaption></figure><p>Manipulated ping echo message -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*3JPYo9CWKw-pPk43H6BXvQ.png" /><figcaption><strong>Figure 4. </strong>Manipulated Ping Echo message</figcaption></figure><p>Ping packets are mostly allowed to pass between different endpoints on the network. It’s an everyday tool that IT teams use to troubleshoot network faults — that’s exactly why it’s a great way to bypass network policies.</p><p>Similar to SMB and DNS, the amount of ICMP packets sent on the network is huge, making this method hard to detect.</p><h3><strong>Now what?</strong></h3><p>So, what can you do against these types of attacks?</p><p><strong>Keep your enemies close, but your friends closer</strong> — using a firewall to filter the types of traffic that you don’t use is the easy part. But what are you going to do with the traffic you allowed to pass?</p><p>Two main methods worth pointing out that can help address these challenges are:</p><p>1. DPI — Deep Packet Inspection as a security feature in your network can go a long way to detecting the malicious use of packets to transfer payloads, such as DNS beacons and ICMP tunneling</p><p>2. Behavioral-based security — an automated proactive approach, in which you can detect behavioral anomalies in your network, such as 2 machines going back and forth from the same file on a shared folder (sound familiar?)</p><p>The most important issue here is that you need to embrace the perspective of an attacker on your network.<br> <br> Look for the properties on the network that are “obvious”, the trivial types of traffic, and those that are difficult to filter out the unusual patterns.</p><p><strong>Good luck!</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7231f421d97a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/cyberark-engineering/your-network-through-the-eyes-of-a-hacker-7231f421d97a">Your network through the eyes of a hacker</a> was originally published in <a href="https://medium.com/cyberark-engineering">CyberArk Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ping Power — ICMP Tunnel]]></title>
            <link>https://infosecwriteups.com/ping-power-icmp-tunnel-31e2abb2aaea?source=rss-33fd60f064de------2</link>
            <guid isPermaLink="false">https://medium.com/p/31e2abb2aaea</guid>
            <category><![CDATA[red-team]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[networking]]></category>
            <category><![CDATA[scapy]]></category>
            <category><![CDATA[wireshark]]></category>
            <dc:creator><![CDATA[Nir Chako]]></dc:creator>
            <pubDate>Mon, 17 Dec 2018 19:18:44 GMT</pubDate>
            <atom:updated>2019-03-02T12:11:28.179Z</atom:updated>
            <content:encoded><![CDATA[<h3>Ping Power — ICMP Tunnel</h3><p>An attacker is often required to face a number of challenges in his activities.<br>Two of these challenges include –</p><ol><li>Overcome network barriers (network policies, segmentation, etc.).</li><li>Perform different operations in “stealth mode” so he won’t get caught.</li></ol><p>One good way to deal with these challenges is by using <strong>ICMP Tunnel</strong> when trying to create a stealth connection that can cross the different barriers in the network.</p><p>In computer networks, <strong>tunneling</strong> is generally the encapsulation of one network protocol as the payload of another network protocol. <a href="https://en.m.wikipedia.org/wiki/Tunneling_protocol">Read more</a></p><p><strong>ICMP</strong> (Internet Control Message Protocol) is a supporting protocol in the Internet protocol suite. It is used by network devices to send error messages and operational information. The most known and probably the most commonly used ICMP message is the <a href="https://en.wikipedia.org/wiki/Ping_(networking_utility)">Ping</a> message.</p><p><strong>Ping</strong> is a control message that is a part of the ICMP (Internet Control Message Protocol).</p><p>Ping is sent from one node in a network to another. It’s built from a layer 2 and layer 3 headers (MAC and IP headers defined by the OSI module) and a special ICMP packet. The sending node will set the destination parameters and if the message is received by the destination it will return it right back –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/471/1*r3Vun5KPHe7ktYPpvVaPrg.png" /></figure><p>This is an IP datagram of a ping packet –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DJZez6oQhbC-pOYDgVfRTg.png" /></figure><p><strong>ICMP Tunneling</strong> can be done by changing the Payload Data so it will contain the data we want to send.</p><p>Usually, it contains a default Payload Data such as this ASCII string — “abcdefghijklmnopqrstuvwabcdefghi”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*DhqCSKkCHl8Lu_RwGWWP8A.png" /><figcaption>Wireshark — ICMP packet, Payload Data</figcaption></figure><p>If we encapsulate an HTTP packet inside the Payload Data, we will get the most common way of this method- sneak out of a Pay-for-WiFi.</p><p>This can be achieved by using a proxy server that waits for ping messages and sends them as needed (for example — as HTTP).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/634/1*yNNoRhB3taQLoGroWGSelw.png" /></figure><ol><li>Using the right tools (such as <a href="http://www.mit.edu/afs.new/sipb/user/golem/tmp/ptunnel-0.61.orig/web/">ptunnel</a>), encapsulate the HTTP packet that you would like to send to Google into the Ping packet (inside Payload Data). Then send it to the proxy server IP address as the destination.</li></ol><ul><li>Note — This IP is not the destination of the HTTP packet (the IP destination of the HTTP packet will be the IP of <a href="http://www.google.com/">www.google.com</a> )</li></ul><ol><li>Because airports routers usually allow ICMP traffic out of the network, the router will deliver the Ping message to the proxy server.</li><li>The proxy server receives the Ping packet, breaks it into 2 parts –</li></ol><ul><li>The ICMP headers.</li><li>The payload which contains the original HTTP message.</li><li>Note — The source IP of the HTTP packet that the proxy sends to Google, should be the IP of the proxy server itself and not the IP of your laptop (or the airport’s router…) because Google should reply back to the proxy and not to you.</li></ul><p>This might be the most common use of ICMP Tunnel, but <strong>as a Red Teamer, I do find it pretty useful as an “under-the-radar” method to evade firewalls and other network policies.</strong></p><p>All of this is possible because ping messages are allowed to cross the router out of the “Pay-for-WiFi” LAN to the Internet.<br>Why would someone allow this situation to happen?</p><p>As a former network engineer, I can tell you that <strong>ping has a lot of power</strong> when trying to understand and solve even the most complicated problems.<br>Most troubleshooting processes begin with testing whether information passes from one point to another. Asking — is this information route possible at all? are the network components active and are able to respond?</p><p>Ping messages can answer these questions and many others in the simplest way.</p><p>These troubleshooting processes occur on a daily basis. It means that the network’s configuration must allow transmission of ping messages on the network from one node to another. Every firewall policies, router policies and Switch ACL (Access List) must allow the flow of ICMP messages from almost any network component to any other.<br>That’s why <strong>ping messages will most likely be less affected by network segmentation and policies</strong>.</p><p>Knowing this, it seems to me that in order to create a connection in a network when you need to pass obstacles such as segmentation and network policies — it would be a good idea that agents will connect with the C&amp;C server using ICMP Tunneling.</p><p>I wrote a simple POC (Proof Of Concept) in python to demonstrate how it works.</p><blockquote><em>Note that:</em></blockquote><blockquote><em>– This POC requires you to install </em><a href="https://scapy.net/download/"><em>Scapy</em></a><em> (which is a great tool to </em><a href="https://scapy.readthedocs.io/en/latest/index.html"><em>learn about</em></a><em> anyway)</em></blockquote><blockquote><em>– This POC will not involve handling with fragmentation. Fragmentation will occur, for example, if the answer from the agent will be bigger then the allowed Payload Data size.</em></blockquote><p>This POC will involve a C&amp;C server and an agent. Where the C2 server will send the agent commands through an ICMP Tunnel and the agent will return the results, also, through the ICMP Tunnel.</p><h3>C2.py</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95a73cbd69e3590c0548a53306b90a44/href">https://medium.com/media/95a73cbd69e3590c0548a53306b90a44/href</a></iframe><h3>Agent.py</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a92183e250fcba69d16abc63b090da9e/href">https://medium.com/media/a92183e250fcba69d16abc63b090da9e/href</a></iframe><p>Using this will look something like this –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/650/1*sBukRk2VLDmXKY5pGhxsWQ.png" /></figure><p>And it’s always a good idea to see what’s exactly going on using Wireshark –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wyUjrKZkWxM99xjwx2V2dw.png" /><figcaption>C2- pwd command</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YJMFLIW-_PigphvVpuwmAQ.png" /><figcaption>Agent — pwd result</figcaption></figure><p>As you can see — there are 2 ICMP messages, one with the command and one with the result.</p><h3>D.P.O.V (Defense Point of View)</h3><p>It’s important to take a look from the defense point of view and think about what we should take into account when we build this kind of tools.</p><p>The most important thing to remember is that cybersecurity tools do not start and end with firewall whitelists policies. Most of today’s defense tools will include some kind of anomaly detection functionality.</p><p>It’s a good way to map out some interesting anomalies by first describing the common behavior of the relevant subject.</p><p>Talking about ping messages in a regular network, we can assume the following features –</p><ol><li>Most of the ping messages will be sent in the default manner — 4 pings at a time.</li><li>The ping message is from type 8 (echo ping request) and Ping answer is from type 0 (echo ping reply)</li><li>There are some fields that are sent with each ping packet (using Windows 8.1) –</li></ol><ul><li><em>id </em>= 0x0001</li><li><em>seq </em>of the replay will be equal to the <em>seq</em> of the request</li><li>The <em>Payload Data</em> will remain with its default size (32 Bytes) and content — “abcdefghijklmnopqrstuvwabcdefghi”</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/935/1*dXu-hBtB1dnIQrZlUSCDTg.png" /></figure><p>Knowing all the above, you then must consider –</p><ol><li>Build the C&amp;C and Agent in a way that there will be no more than 4 ping messages in one batch for every 1 minute (for example). If the data you need to transfer would take 15 ping messages — it would take it 3 minutes to pass. It may be slow, but if someone or something is looking at this anomaly — it will be worth it.</li><li><strong>Make sure that the</strong> <strong>ping request and reply are logically correct</strong>. If every ping message you send will be, for example, of type 0 — it would be weird to see a lot of ping replies when there are no ping requests.</li><li>Try to be as similar as you can be to your surroundings. Do your research. You can, of course, keep this fields configurable and change them as you go.</li><li>Notice that the <strong>Payload Data size</strong> will affect the first section (number of ping messages per data size) and<strong> it’s a metadata information</strong>.</li><li><strong>Payload Data content</strong> — let’s talk about DPI…</li></ol><p><strong>DPI — Deep Packet Inspection</strong></p><p>Whereas <strong>the conventional packet inspections read the metadata of the packet</strong> (mainly headers), <strong>Deep packet inspection read the contents of a packet</strong> that is going through it in real time. Basically, it takes a look at the payload and tries to understand if it looks right.</p><p>In our case — a DPI tool using his protocol anomaly capability can discover the ICMP Tunnel by looking at the payload and see that it’s different than what it should be.</p><p>In this case, it won’t help us to change all of the other parameters.</p><p>So — why should we even bother with ICMP Tunneling?</p><p>Because –</p><ol><li>DPI is not a trivial capability and you may come across many networks without this capability.</li><li>Most DPI tools rely on a signatures database — if there is no signature relevant for ICMP messages, it won’t detect the ICMP Tunnel.</li><li>Even if there is a relevant signature at the database, the operator should first configure it to be in an active mode.</li></ol><ul><li>Why he might not activate it?</li><li>Each signature check takes processing resources (mainly CPU and time) so<strong> it may slow down the network</strong>.</li><li>Network checks can include different types of ping messages with different payload sizes (ex. <strong>ping -l 1234 8.8.8.8</strong>, will send ping message with payload data of size 1234[Byte]) to troubleshoot problems involving MTU features. <strong>Activating this kind of signature will rise up a lot of false-positive alerts and thus annoy the monitoring team and reduce the reliability of the signature.</strong></li></ul><h3>sum += 1337</h3><p>ICMP Tunneling is an excellent tool to communicate “under the radar”. While there are times when it will be less effective, depending on the existing defenses on the network, many times you’ll find that it is a simple and convenient way to overcome certain limitations.</p><p>Most of all, it’s a concept worth knowing. If you get the trick, you can develop as many different solutions as you need, such as — DNS Tunnel, SSH Tunnel and so on…</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=31e2abb2aaea" width="1" height="1" alt=""><hr><p><a href="https://infosecwriteups.com/ping-power-icmp-tunnel-31e2abb2aaea">Ping Power — ICMP Tunnel</a> was originally published in <a href="https://infosecwriteups.com/">InfoSec Write-ups</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[shellcoding 101 — RPISEC/MBE/lab3C]]></title>
            <link>https://medium.com/@C_h4ck_0/shellcoding-101-rpisec-mbe-lab3c-76af01950039?source=rss-33fd60f064de------2</link>
            <guid isPermaLink="false">https://medium.com/p/76af01950039</guid>
            <category><![CDATA[buffer-overflow]]></category>
            <category><![CDATA[exploitation]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[shellcode]]></category>
            <category><![CDATA[aids]]></category>
            <dc:creator><![CDATA[Nir Chako]]></dc:creator>
            <pubDate>Mon, 17 Dec 2018 19:04:06 GMT</pubDate>
            <atom:updated>2019-03-02T12:09:16.122Z</atom:updated>
            <content:encoded><![CDATA[<h3>shellcoding 101 — RPISEC/MBE/lab3C</h3><p>From Wikipedia –</p><p>In <a href="https://en.wikipedia.org/wiki/Hacker_(computer_security)">hacking</a>, a <strong>shellcode</strong> is a small piece of code used as the <a href="https://en.wikipedia.org/wiki/Payload_(computing)">payload</a> in the <a href="https://en.wikipedia.org/wiki/Exploit_(computer_security)">exploitation</a> of a software <a href="https://en.wikipedia.org/wiki/Vulnerability_(computing)">vulnerability</a>.</p><p>I think that the best way to learn something is simply by playing with it.</p><p>Shellcoding is no different. One way to learn this subject from scratch is by solving LAB3 from the <a href="https://github.com/RPISEC/MBE">RPISEC/MBE</a> course. This post will cover LAB3C (the first exercise in LAB03).</p><p>lab3C contains 2 files –</p><ol><li>lab3C — ELF file</li><li>lab3C.c — c code from which the ELF file was generated</li></ol><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a19c573dd1299b3fa932e13e0833c327/href">https://medium.com/media/a19c573dd1299b3fa932e13e0833c327/href</a></iframe><p>In order to solve this exercise you should get an interactive shell.</p><p>And so, to achieve this target, we’re going to inject and run a shellcode through the lab3C program.</p><p>Going through the code we can identify a potential buffer overflow. fgets function receives a length of 0x64 which is 100 chars in a decimal base (and I’m pretty sure that 100 &gt; 64)</p><blockquote>char a_user_pass[64]<br>fgets(a_user_pass, 0x64, stdin);</blockquote><p>Another thing we should notice is that <strong><em>a_user_name</em></strong> is an uninitialized global variable.</p><p>That means that the compiler will store it in the <strong>.bss</strong> section. <strong><em>a_user_pass</em></strong><em> </em>will be stored in the <strong>data </strong>section.</p><p>This program opens up a nice vector of attack. The memory segment that holds the .bss is not marked as a non-executable (/* gcc -z execstack -fno-stack-protector -o lab3C lab3C.c */), so you can actually execute code from it.</p><p>In order to get to this point, we should perform these steps –</p><ol><li>Insert the shellcode into <strong><em>a_user_name</em></strong>.</li><li>Create a buffer overflow in <strong><em>a_user_pass</em></strong><em> </em>in order to ride over the RET address to be the address of <strong><em>a_user_name</em></strong>.</li></ol><p>At this point, we can assume that we can initiate a memory leak so that <strong><em>a_user_pass</em></strong><em> </em>will ride over the return address from the main function and point it to the <strong>.bss</strong> section, where <strong><em>a_user_name </em></strong>is saved.</p><p>In order to bring this attack vector to life, we should first answer these questions –</p><ol><li>How can we pass the logical barriers of the program in order to inject the shellcode into <strong><em>a_user_name</em></strong>?</li><li>What is the address of <strong><em>a_user_name</em></strong>?</li><li>How can we ride over the return address with <strong><em>a_user_name</em></strong>‘s address?</li><li>What the shellcode should be?</li></ol><p>Let’s answer these questions –</p><ol><li>The first 6 chars in <strong><em>a_user_name</em></strong> should be “rpisec” in order to get to the point where we can insert <strong><em>a_user_pass</em></strong>. Otherwise, we won’t get the chance to create the buffer overflow that rides over the return address.</li></ol><ul><li>we know that the first 6 chars should be “rpisec” from <strong><em>verify_user_name </em></strong>function –</li></ul><pre>int verify_user_name()<br>{<br>    puts(&quot;verifying username....\n&quot;);<br>    return strncmp(a_user_name, &quot;rpisec&quot;, 6);<br>}</pre><p>2. We can see by debugging with IDA that <strong><em>a_user_name</em></strong> is at 0x08049C40.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*Kw3cD5kt4pKP3Z0QH3-4MA.png" /></figure><p>3. Debugging with IDA, we can also see that in order to ride over the return address we should first insert 80 chars and only then insert 0x08049C40.</p><p>Here we can see the stack filled with 76 ‘a’ chars and one ‘\x0A’ (ascii for line feed — or ‘ENTER’) –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/523/1*7zfvaJzclPW5N9Tk4aTiuw.png" /></figure><p>At 0xFFA4CF8C we can see the return address from the main function that will be called in any of these cases –</p><pre>x = verify_user_pass(a_user_pass);<br>if (x == 0 || x != 0){<br>    puts(&quot;nope, incorrect password...\n&quot;);<br>    <strong>return EXIT_FAILURE;</strong><br>}<br><strong>return EXIT_SUCCESS;</strong></pre><p>4. First of all, in order to avoid inaccuracy with the address of the shellcode and to avoid the “rpisec” string (user name), we will insert some nop commands (<a href="https://en.wikipedia.org/wiki/NOP_slide">nop sled</a>) and inject 0x08049C4A, instead of 0x08049C40, for it to be the new return address.</p><p>Secondly, we are going to use this shellcode –</p><p><a href="http://shell-storm.org/shellcode/files/shellcode-811.php">http://shell-storm.org/shellcode/files/shellcode-811.php</a></p><p>Which implements an execve(“/bin/sh”) command for Linux x86 (32bit OS). This will allow us to get an interactive Linux shell.</p><p>Using python with pwntools, the full solution is as follows-</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ff80fc26f7b2f5277fe49c4ba894342/href">https://medium.com/media/5ff80fc26f7b2f5277fe49c4ba894342/href</a></iframe><p>And so we get –</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*h7tvpKefWmnNyrgh-3BybQ.png" /></figure><h3>Shellcode explanation</h3><p>As I said at the beginning of this post — the best way to learn something is to play with it.</p><p>Now, after solving this exercise, I do want to go deeper with you to understand what exactly happened.</p><p>The shellcode we injected was a hexadecimal data that represent a small piece of code. What does this mean? It means that, for example, when the computer’s CPU reads this — 0x68 he knows that the next 4 bytes should be pushed on to the stack. The push command is called an <em>opcode</em> and the data that will be pushed in is called an <em>operand.</em></p><p>When we change the return address so that it will point to the start of the shellcode, we actually tell the CPU to continue and run the next piece of code from there. The EIP register (Extended Instruction Pointer) tells the computer where to go next to execute the next command and controls the flow of a program. After a <em>return</em> command, EIP points to the return address. In this case, after riding over the original return address, EIP will point to the first opcode of the shellcode.</p><h3>The shellcode</h3><blockquote><strong><em>31</em></strong><em> c0 xor %eax,%eax</em><strong><em> // set eax to 0</em></strong><em><br></em><strong><em>50</em></strong><em> push %eax</em><strong><em> // push eax to the stack</em></strong></blockquote><p><strong>Push the path of “/bin//sh” to the stack</strong></p><blockquote><strong><em>68</em></strong><em> 2f 2f 73 68 push $0x68732f2f </em><strong><em>//</em></strong><em> </em><strong><em>push chars in ascii — “hs//”</em></strong><em><br></em><strong><em>68</em></strong><em> 2f 62 69 6e push $0x6e69622f </em><strong><em>// push chars in ascii — “nib/”</em></strong></blockquote><p><strong>initialize arguments</strong></p><blockquote><strong><em>89</em></strong><em> e3 mov %esp,%ebx </em><strong><em>// set ebx to 0</em></strong><em><br></em><strong><em>89</em></strong><em> c1 mov %eax,%ecx </em><strong><em>// set ecx to 0</em></strong><em><br></em><strong><em>89</em></strong><em> c2 mov %eax,%edx</em><strong><em> // set edx to 0</em></strong></blockquote><p><strong>Call the execve function using system call</strong></p><blockquote><strong><em>b0</em></strong><em> 0b mov $0xb,%al</em><strong><em> // set the least significant byte of eax to 11 (eax was 0 before so now it’s 11)</em></strong></blockquote><blockquote><strong><em>cd</em></strong><em> 80 int $0x80 </em><strong><em>// system call. the value of eax is 11 so the system call will be execve. read more about the different sys calls and their IDs </em></strong><a href="https://syscalls.kernelgrok.com/"><em>— https//syscalls.kernelgrok.com</em></a></blockquote><p><strong>execve receive 3 arguments</strong></p><pre><strong>int execve(const char *</strong><em>filename</em><strong>, char *const</strong> <em>argv</em><strong>[], char *const</strong> <em>envp</em><strong>[]);</strong></pre><p><strong>ebx</strong> (<em>filename) </em>— pointer to a string naming the file to execute</p><p><strong>ecx</strong> <em>(argv) </em>— pointer to an array of pointers to strings, which are the program arguments, the first one being a copy of the program name, to be used by the invoked program itself</p><p><strong>edx</strong> (envp) — pointer to an array of pointers to strings, which are the environment variables; Linux tolerates that value to be NULL, for an empty environment.</p><p>The full command will be — execve(“/bin//sh”, arr, 0) where arr points to an array of two pointers, first one being a pointer to “/bin//sh”, while the other is NULL.</p><p><strong>Exit call</strong></p><blockquote><strong><em>31</em></strong><em> c0 xor %eax,%eax </em><strong><em>// set eax to 0</em></strong><em><br></em><strong><em>40</em></strong><em> inc %eax</em><strong><em> // set eax to 1</em></strong><em><br></em><strong><em>cd</em></strong><em> 80 int $0x80 </em><strong><em>// system call when eax=1 is a sys_exit call</em></strong></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76af01950039" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>