<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Bash on 1nitramfs' Blog</title><link>https://mehloul-mohamed.github.io/tags/bash/</link><description>Recent content in Bash on 1nitramfs' Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Tue, 09 Apr 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://mehloul-mohamed.github.io/tags/bash/index.xml" rel="self" type="application/rss+xml"/><item><title>[PicoCTF 2024] dont-you-love-banners writeup</title><link>https://mehloul-mohamed.github.io/p/picoctf-2024-dont-you-love-banners-writeup/</link><pubDate>Tue, 09 Apr 2024 00:00:00 +0000</pubDate><guid>https://mehloul-mohamed.github.io/p/picoctf-2024-dont-you-love-banners-writeup/</guid><description>&lt;p&gt;&lt;strong&gt;In this post, I aim to explain my solution for the “dont-you-love-banners” challenge from PicoCTF 2024’s “General Skills” category.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Can you abuse the banner?The server has been leaking some crucial information on tethys.picoctf.net 51954. Use the leaked information to get to the server.To connect to the running application use nc tethys.picoctf.net 57614. From the above information abuse the machine and find the flag in the /root directory.&lt;/p&gt;
&lt;p&gt;We have an address and a Netcat command. Upon connecting to the address with Netcat, we are greeted with some very interesting output:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/address.png"
loading="lazy"
alt="Address Screenshot"
&gt;&lt;br&gt;
Ok, we see something about OpenSSH and additionally, we can see a very clear password.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Password for what?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;We do not know yet. Let’s explore more! Let’s use the Netcat command provided:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/pass.png"
loading="lazy"
alt="nc screenshot"
&gt;&lt;br&gt;
Very interesting! Looks like we found a use for that password. Let’s put it in:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/access.png"
loading="lazy"
alt="Access Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;“What is the top cyber security conference in the world?” seems like a subjective question. After researching some of the most well-known cybersecurity conferences and testing them out, it appears that DEF CON is the answer.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Not very surprising.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/answer.png"
loading="lazy"
alt="Answer screenshot"
&gt;&lt;br&gt;
Now we have a new question, just a quick Google search yields us the answer “John Draper”:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/answer2.png"
loading="lazy"
alt="Answer 2 screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We have a shell!!!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Let’s explore! Running &lt;code&gt;ls&lt;/code&gt; gives us:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/ls.png"
loading="lazy"
alt="ls Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;Ok, we have two files.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Thanks for stating the obvious&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Let’s look at their content.&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/content.png"
loading="lazy"
alt="Content Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Not very helpful&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;We can’t be sure for now. Let’s look back at the challenge text.&lt;br&gt;
find the flag in the /root directory.&lt;br&gt;
I guess it’s about time we looked at &lt;code&gt;/root&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/root.png"
loading="lazy"
alt="Root Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We’ve found the flag!!!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;I Hate to say it, but we’re not done yet:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/attempt.png"
loading="lazy"
alt="Attempt Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We’re stuck&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;I guess the most logical step is to check what other things we have on &lt;code&gt;/root&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/explore.png"
loading="lazy"
alt="Explore Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Intresting… &lt;code&gt;script.py&lt;/code&gt;, Can you &lt;code&gt;cat&lt;/code&gt; that for me.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Sure! Here’s the content of &lt;code&gt;script.py&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/script.png"
loading="lazy"
alt="Script Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;That seems like the script for the prompts we got earlier. The ones with the questions.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Indeed.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;But, it seems useless. What can we do with it?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Read the script more thoroughly, what is it doing?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It seems to be printing out the content of the file &lt;code&gt;banner&lt;/code&gt; from earlier then asking the user questions and checking the answers.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;And what do we want to do?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;We want to print out the content of /root/flag.txt&lt;/p&gt;&lt;/blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;Wait… how about we modify the script!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Sorry, we can’t. It’s owned by root and we are not root.&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/permissions.png"
loading="lazy"
alt="Permissions Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;So what now&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Let’s look at the hint.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Cheaterrrrr!!!!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Do you know about symlinks?&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What are symlinks?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;A symlink, or a symbolic link, is a file that points to another file or directory on your system. Think of it almost like a shortcut.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I see, but how would that be useful here?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Well, I thought the same. And then it hit me! Why don’t we remove that &lt;code&gt;banner&lt;/code&gt; file and make a new symlink to the &lt;code&gt;flag.txt&lt;/code&gt; file in its place? That could work!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How do I make a symlink&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;We will use the &lt;code&gt;ln&lt;/code&gt; command with the option &lt;code&gt;-s&lt;/code&gt; to make a symbolic link.&lt;/p&gt;
&lt;p&gt;But first, let’s remove &lt;code&gt;banner&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/remove.png"
loading="lazy"
alt="rm Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;Now let’s make the symlink:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/symlink.png"
loading="lazy"
alt="Symlink Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Woah! Slow down there!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Let me explain. I used the &lt;code&gt;ln&lt;/code&gt; command which is what we use to make links.&lt;/p&gt;
&lt;p&gt;I gave it the option &lt;code&gt;-s&lt;/code&gt; to specify that I want a soft link(symlink).&lt;/p&gt;
&lt;p&gt;And lastly, I gave it the file I want to make a symlink to &lt;code&gt;/root/flag.txt&lt;/code&gt; and the path I want the link to be on (&lt;code&gt;./banner&lt;/code&gt; you could also just write &lt;code&gt;banner&lt;/code&gt;)&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Ok, now we run the script with &lt;code&gt;python3 /root/script.py&lt;/code&gt;?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Let’s try!&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/run.png"
loading="lazy"
alt="Run Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It doesn’t work. You clearly don’t know what you’re doing.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Very harsh comments, I must say. Anyway, I’ll explain. When you ran the script, you ran it with your user (“player”), who doesn’t have access to &lt;code&gt;flag.txt&lt;/code&gt;. So when the script tried to open it (through the soft link we made), it couldn&amp;rsquo;t. Therefore, it displayed the default banner message.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;So we need to get &lt;code&gt;root&lt;/code&gt; to run the script.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Exactly!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Well the only other time we’ve seen the script being run is when we connected to the address with Netcat, maybe it was run as &lt;code&gt;root&lt;/code&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Intresting…&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Let’s exit with &lt;code&gt;CTRL-D&lt;/code&gt; and try to connect again:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/flag.png"
loading="lazy"
alt="Flag Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;Here’s our flag.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Wonderful! but one question: That port looks different from the one we used the first time, why?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Oh, that has nothing to do with the challenge, I just wrote the first and last parts of the writeup at different times and used different instances on picoCTF, that’s why the ports are different.&lt;/p&gt;
&lt;p&gt;Thank you for reading,&lt;br&gt;
Hope you enjoyed this ride and learned something new!&lt;/p&gt;</description></item><item><title>[PicoCTF 2024] SansAlpha writeup</title><link>https://mehloul-mohamed.github.io/p/picoctf-2024-sansalpha-writeup/</link><pubDate>Mon, 08 Apr 2024 00:00:00 +0000</pubDate><guid>https://mehloul-mohamed.github.io/p/picoctf-2024-sansalpha-writeup/</guid><description>&lt;p&gt;&lt;strong&gt;In this post, I aim to explain my solution for the “SansAlpha” challenge from PicoCTF 2024’s “General Skills” category.&lt;/strong&gt;&lt;br&gt;
We are given a shell where no alphabetical characters seem to be working. Running a command that contains a letter gives you an error message:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/shell.png"
loading="lazy"
alt="Shell Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;Our goal is simple: capture the flag. But how? You may ask. Fear not, you’re in the right place. Buckle up, it’s going to be a thrilling ride.&lt;/p&gt;
&lt;h1 id="investigation"&gt;Investigation
&lt;/h1&gt;&lt;p&gt;Let’s get to know our environment a little better. Entering &lt;code&gt;*&lt;/code&gt; into our shell, we get:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/first-test.png"
loading="lazy"
alt="First Test Screenshot"
&gt;&lt;br&gt;
Pretty interesting!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Why did you run &lt;code&gt;*&lt;/code&gt;? and why did it produce that error?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Well, simply put, it is just bash globbing! when I entered * the shell interpreted it as ‘every file and directory in the current directory’. It attempted to execute the first file or directory it found and passed all the other files and directories in the current directory as arguments, this YouTube video from LiveOverflow about another somewhat similar challenge explains the idea pretty well:&lt;/p&gt;
&lt;p&gt;{{ youtube 6D1LnMj0Yt0 }}&lt;/p&gt;
&lt;p&gt;Now that we know what the &lt;code&gt;*&lt;/code&gt; means here, we can understand what the error means. &lt;code&gt;bash: blargh: command not found&lt;/code&gt; As you might have guessed, our shell tried to execute the first file or directory it found, which happened to be named “blargh”.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Cool! Now what?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Let’s see if we can find more files!&lt;br&gt;
There’s this special bash variable $_that we can use to look at the last argument given to the previous command.&lt;br&gt;
When input it into our shell we get:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/second-test.png"
loading="lazy"
alt="Second Test Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;We’ve found another file! but it doesn’t seem to be our flag.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Still useless&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Don’t be hasty my friend, Let’s dig a little deeper. How about we see if we can find any other files or directories near us? Let’s look for files and directories inside subdirectories!&lt;/p&gt;
&lt;p&gt;running &lt;code&gt;./*/*&lt;/code&gt; gives us:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/subdirs.png"
loading="lazy"
alt="Subdirs Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;Alright! We found something interesting here! This appears to be the flag file!&lt;/p&gt;
&lt;h1 id="now-lets-rewind"&gt;Now let’s rewind
&lt;/h1&gt;&lt;p&gt;&lt;blockquote&gt;
&lt;p&gt;What did you do?&lt;/p&gt;&lt;/blockquote&gt;&lt;br&gt;
I ran &lt;code&gt;./*/*&lt;/code&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;But why?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;well, the &lt;code&gt;./&lt;/code&gt; means “look in the current directory”, the current directory being the directory we are in right now. Note: The &lt;code&gt;./&lt;/code&gt; is not necessary for this situation since even if we omit it here (i.e. we write only &lt;code&gt;*/*&lt;/code&gt; ) we would also be looking in the current directory.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;What about the &lt;code&gt;*/*&lt;/code&gt;?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Simply put, this looks for files and directories stored inside subdirectories of our current directory. It tries to execute the first one it found and passes all the other files and directories it found as arguments.&lt;/p&gt;
&lt;h2 id="what-did-it-find-first"&gt;What did it find first?
&lt;/h2&gt;&lt;p&gt;&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;flag.txt&lt;/code&gt; which is inside the &lt;code&gt;blargh&lt;/code&gt; folder.&lt;/p&gt;&lt;/blockquote&gt;&lt;br&gt;
Well done!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;But how are we going to get the content of flag.txt without using letters?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Remember that &lt;code&gt;on-calastran.txt&lt;/code&gt; file? Conveniently, its name has all the letters of the command &lt;code&gt;cat&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;blockquote&gt;
&lt;p&gt;If only we could just get the letters from it.&lt;/p&gt;&lt;/blockquote&gt;&lt;br&gt;
Wait! We actually can!!&lt;/p&gt;
&lt;h1 id="enter-shell-parameter-expansion"&gt;Enter Shell Parameter Expansion
&lt;/h1&gt;&lt;p&gt;While I was researching, trying to find I way to solve this challenge, I stumbled upon this post:&lt;br&gt;
[https://www.commandlinefu.com/commands/view/11704/launch-bash-without-using-any-letters]&lt;/p&gt;
&lt;p&gt;It showed a neat trick the author used to run bash without using any letters. I found that interesting since that’s very similar to what I needed. I needed to somehow launch a program without using letters, so I researched more about the syntax he was employing and found out that he was using something called “Shell Parameter Expansion”. Fancy name I must say! Anyway, I looked for documentation and found this:&lt;/p&gt;
&lt;p&gt;[https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html]&lt;/p&gt;
&lt;p&gt;After skimming it, one thing caught my eye:&lt;br&gt;
&lt;code&gt;${parameter:offset:length}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;This is referred to as Substring Expansion. It expands to up to length characters of the value of parameter starting at the character specified by offset. If parameter is ‘@’ or ‘&lt;em&gt;’, an indexed array subscripted by ‘@’ or ‘&lt;/em&gt;’, or an associative array name, the results differ as described below. If length is omitted, it expands to the substring of the value of parameter starting at the character specified by offset and extending to the end of the value. length and offset are arithmetic expressions (see Shell Arithmetic).&lt;/p&gt;
&lt;p&gt;If offset evaluates to a number less than zero, the value is used as an offset in characters from the end of the value of parameter. If length evaluates to a number less than zero, it is interpreted as an offset in characters from the end of the value of parameter rather than a number of characters, and the expansion is the characters between offset and that result. Note that a negative offset must be separated from the colon by at least one space to avoid being confused with the ‘:-’ expansion.&lt;/p&gt;
&lt;p&gt;This allows us to take a string and get whatever part we want from it, which is awesome! We can now take all the letters we need from the string &lt;code&gt;on-calastran.txt&lt;/code&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;But how do we use it?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;So remember our &lt;code&gt;$_&lt;/code&gt; variable? when we looked at its value after running &lt;code&gt;*&lt;/code&gt; we found that it had the string “on-calastran.txt” so, we can just use our new trick to get letters from it, like this: &lt;code&gt;${_:3:1}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/expantion.png"
loading="lazy"
alt="Param Expantion Demo Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;We’ve got a “c”!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Whoa there! What’s that?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Don’t panic, I’ll explain. So we took the value of the &lt;code&gt;$_&lt;/code&gt; variable (hence &lt;code&gt;${_&lt;/code&gt; ) and we got the letter at the index 3 (that’s why we wrote &lt;code&gt;:3&lt;/code&gt;) (&lt;strong&gt;Note:&lt;/strong&gt; indices start at 0). As for the &lt;code&gt;:1&lt;/code&gt;, it means that I want to take only 1 letter. Lastly, we close our expression with &lt;code&gt;}&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Now for the “a”:&lt;/strong&gt; We have “a” at the 5th position, but since indices start at 0 we write 4:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;${_:4:1}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/expantion2.png"
loading="lazy"
alt="Expantion Demo 2 Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;And we can get a “t” with:&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;${_:8:1}&lt;/code&gt;&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/expantion3.png"
loading="lazy"
alt="Expantion Demo 3 Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;Now we know how we can write &lt;code&gt;cat&lt;/code&gt;: &lt;code&gt;${_:3:1}${_:4:1}${_:8:1}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; for us to get the right letters, we have to run &lt;code&gt;*&lt;/code&gt; first so that the value of &lt;code&gt;$_&lt;/code&gt; would be “on-calastran.txt”.&lt;/p&gt;
&lt;p&gt;Now that we have &lt;code&gt;cat&lt;/code&gt;, Let’s use it:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/final.png"
loading="lazy"
alt="Final Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;So what did you do here?&lt;/p&gt;&lt;/blockquote&gt;
&lt;ul&gt;
&lt;li&gt;First, I ran &lt;code&gt;*&lt;/code&gt; to get the &lt;code&gt;on-calastran.txt&lt;/code&gt; value on &lt;code&gt;$_&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Then, I instructed &lt;code&gt;cat&lt;/code&gt; to &lt;code&gt;cat&lt;/code&gt; every file inside every subdirectory in our current directory (hence the &lt;code&gt;./*/*&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;And now we have our flag!&lt;br&gt;
Hope you enjoyed this ride and learned something new!&lt;/p&gt;</description></item></channel></rss>