<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pyjail on 1nitramfs' Blog</title><link>https://mehloul-mohamed.github.io/tags/pyjail/</link><description>Recent content in Pyjail on 1nitramfs' Blog</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Wed, 12 Jun 2024 00:00:00 +0000</lastBuildDate><atom:link href="https://mehloul-mohamed.github.io/tags/pyjail/index.xml" rel="self" type="application/rss+xml"/><item><title>[Akasec CTF 2024] pyJail WriteUp</title><link>https://mehloul-mohamed.github.io/p/akasec-ctf-2024-pyjail-writeup/</link><pubDate>Wed, 12 Jun 2024 00:00:00 +0000</pubDate><guid>https://mehloul-mohamed.github.io/p/akasec-ctf-2024-pyjail-writeup/</guid><description>&lt;p&gt;Sigh.. another python jail to escape.&lt;/p&gt;
&lt;p&gt;We’re given a Python script that restricts input to only 16 characters of printable ASCII. Here’s the script:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/sourcecode.png"
loading="lazy"
alt="SourceCode Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;p&gt;Well, a simple&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;exec(input())
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;should do the trick right?&lt;/p&gt;
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Unfortunately, no. &lt;code&gt;stdin&lt;/code&gt; has been set to &lt;code&gt;None&lt;/code&gt;; running &lt;code&gt;input()&lt;/code&gt; would give us this error:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/error.png"
loading="lazy"
alt="Error Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;p&gt;This is impossible! We can’t run&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;print(open(‘flag.txt’).read())
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;since it’s too long&lt;/p&gt;
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Yes, it’s too long, but we can shorten it.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;If you look closely at how it’s calculating the length:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/calc.png"
loading="lazy"
alt="Length Calculation Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;It’s only counting printable ASCII characters.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Well, duh! It literally says that at the top of the script.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;We can use that to our advantage. One of Python3’s features is that it normalizes fonts; Python will interpret&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/example1.png"
loading="lazy"
alt="Example Screenshot 1"
&gt;&lt;/p&gt;
&lt;p&gt;as&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/example2.png"
loading="lazy"
alt="Example Screenshot 2"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I assume those italic characters will not be counted in the length, because the script is only checking for ASCII.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Correct!&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;p&gt;I ran&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;𝘱𝘳𝘪𝘯𝘵(𝘰𝘱𝘦𝘯(‘flag.txt’).𝘳𝘦𝘢𝘥())
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;it still says “Too long”&lt;/p&gt;
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;It’s important to note that the method we’ve been using does not work with parentheses and dots (as far as I know). Also, the string “flag.txt” must be written in ASCII, so the file we are trying to open is “flag.txt” and not something like “𝘧𝘭𝘢𝘨.𝘵𝘹𝘵”.&lt;br&gt;
With that in mind, if you count the number of printable ASCII characters in our input, it is 17.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;So what’s the plan?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;I haven’t given up on the &lt;code&gt;input()&lt;/code&gt; idea. If somehow we can set &lt;code&gt;stdin&lt;/code&gt; back to its original value, we can run:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;exec(input())
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;And we’ll have no restrictions.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;But how?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;At the beginning of the script, the &lt;code&gt;sys&lt;/code&gt; module is imported.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;How is that useful?&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;A quick look at the &lt;code&gt;sys&lt;/code&gt; module’s documentation gives us this:&lt;/p&gt;
&lt;p&gt;sys.&lt;strong&gt;stdin&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;sys.&lt;strong&gt;stdout&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;sys.&lt;strong&gt;stderr&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;These objects contain the original values of stdin, stderr and stdout at the start of the program. They are used during finalization, and could be useful to print to the actual standard stream no matter if the sys.std* object has been redirected.&lt;br&gt;
We can use &lt;code&gt;sys.__stdin__&lt;/code&gt; to get &lt;code&gt;stdin&lt;/code&gt; back to its original value and call &lt;code&gt;exec(input())&lt;/code&gt;.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;I doubt this will be within our character limit.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;If we use our font trick we can get it to the right size.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Oh, right!&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Something like this should do it:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/exp-part1.png"
loading="lazy"
alt="Exploit Part 1 Screenshot"
&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Why did you wrap everything in exec()&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;It’s because the &lt;code&gt;eval()&lt;/code&gt; function doesn’t allow us to assign values to variables since it only evaluates expression, therefore I used &lt;code&gt;exec()&lt;/code&gt; to do the job.&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;After running it, it seems to be stuck, I don’t see anything.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;It’s not stuck. It’s waiting for you to give it input.&lt;br&gt;
we can use a simple:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://mehloul-mohamed.github.io/exp-part2.png"
loading="lazy"
alt="Exploit Part 2 Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;Let’s put everything together:&lt;br&gt;
&lt;img src="https://mehloul-mohamed.github.io/final.png"
loading="lazy"
alt="Final Exploit Screenshot"
&gt;&lt;/p&gt;
&lt;p&gt;And we have our flag!&lt;/p&gt;
&lt;p&gt;&lt;blockquote&gt;
&lt;p&gt;Nice. That was awesome!&lt;/p&gt;&lt;/blockquote&gt;
I hope you enjoyed this ride and learned something new!&lt;/p&gt;</description></item></channel></rss>