<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Shell Parameter Expansion on 1nitramfs' Blog</title><link>https://mehloul-mohamed.github.io/tags/shell-parameter-expansion/</link><description>Recent content in Shell Parameter Expansion on 1nitramfs' Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Mon, 08 Apr 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://mehloul-mohamed.github.io/tags/shell-parameter-expansion/index.xml" rel="self" type="application/rss+xml"/><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>