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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
| from pwn import * import base64 from PIL import Image from qreader import QReader import cv2 import pyzipper import pandas.io.clipboard as cb import os
import time
import string
print('import done') context.log_level = 'debug'
SUBMITTED_ROUNDS = 0
def pretty_display(data: bytes): print(f'len: {len(data)}') if len(data) >= 128: print('first 64 bytes:') l = data[:64] print(l) for i in range(0, len(l), 16): print(' '.join([f'{l[i+j]:02x}' for j in range(16)])) print('last 64 bytes:') r = data[-64:] print(r) for i in range(0, len(r), 16): print(' '.join([f'{r[i+j]:02x}' for j in range(16)])) else: print(data) for i in range(0, len(data), 16): try: print(' '.join([f'{data[i+j]:02x}' for j in range(16)])) except: print(' '.join([f'{data[i+j]:02x}' for j in range(len(data)%16)]))
def ask_next(data: bytes): global s, SUBMITTED_ROUNDS pretty_display(data) print(''' 1. b64 decode 2. b85 decode 3. b32 decode 4. hex decode 5. [::-1] 6. bits[::-1] foreach byte 7. lstrip <len> 8. rstrip b"\\x00" 9. clear junk bytes <to_remove> <to_add_0x40> ''')
file_name = f'./tmp/{time.time()}.txt' print(f'save to {file_name}') with open(file_name, 'wb') as f: f.write(data) try: if len(set(list(data))) == 2: if b'0' not in data: data = data.replace(b'\x8C', b'1').replace(b'\x0C', b'0') print('auto bin') ask_next(handle_res(data.decode(), f'./tmp/{time.time()}.qrtext.txt', 2))
printable_ratio = calculate_printable_ratio(data.rstrip(b'\x00')[4:]) if printable_ratio > 0.95 and calculate_printable_ratio(data) != 1: if any(chr(data[_]) not in string.printable for _ in range(4)): print('auto remove unprintable: lstrip 4') new_data = data[4:] return ask_next(new_data) if data[-1] == 0: print('auto remove unprintable: rstrip b"\\x00"') new_data = data.rstrip(b'\x00') return ask_next(new_data) if data[-1] == b'=': try: new_data = auto_base(data.decode()) return ask_next(new_data) except Exception as e: pass try: lbase = auto_base(data.decode()) except: lbase = None try: rbase = auto_base(data.decode()[::-1]) except: rbase = None if lbase is not None and rbase is not None: print('lbase will produce: ') if len(lbase) > 64: print(lbase[:64]) print(lbase[-64:]) else: print(lbase) print('rbase will produce: ') if len(rbase) > 64: print(rbase[:64]) print(rbase[-64:]) else: print(rbase) elif lbase is not None: return ask_next(lbase) elif rbase is not None: return ask_next(rbase) if data[4:8] == b'\x89PNG' or data[4:6] == b'PK' or data[4:12] == b'504B0304' or data[4:12] == b'89504E47': print('auto lstrip 4') new_data = data[4:] return ask_next(new_data)
if data[:4] == b'\x89PNG': print('auto png') new_data = png_judge_type(file_name) return ask_next(new_data) if data[:2] == b'PK': print('auto zip') new_data = brute_zip_pswd(file_name) return ask_next(new_data) if data[:8] == b'504B0304' or data[:8] == b'89504E47' \ or data[:8] == b'04034B50' or data[:8] == b'474E5089' \ or data[-8:] == b'504B0304' or data[-8:] == b'89504E47' \ or data[-8:] == b'04034B50' or data[-8:] == b'474E5089': print('auto hex') new_data = int(data.decode(), 16).to_bytes(len(data)//2, 'big') return ask_next(new_data) if data[:8] == b'4030B405' or data[:8] == b'74E40598' \ or data[:8] == b'05B43040' or data[:8] == b'9805E474' \ or data[-8:] == b'4030B405' or data[-8:] == b'74E40598' \ or data[-8:] == b'05B43040' or data[-8:] == b'9805E474': data = data[::-1] print('auto rhex') new_data = int(data.decode(), 16).to_bytes(len(data)//2, 'big') return ask_next(new_data) if len(set(list(data))) == 16: try: print('lhex will produce: ') if len(data) > 128: print(int(data[:128].decode(), 16).to_bytes(64, 'big')) print(int(data[-128:].decode(), 16).to_bytes(64, 'big')) else: print(int(data.decode(), 16).to_bytes(len(data)//2, 'big')) print('rhex will produce: ') if len(data) > 128: print(int(data[::-1][:128].decode(), 16).to_bytes(64, 'big')) print(int(data[::-1][-128:].decode(), 16).to_bytes(64, 'big')) else: print(int(data[::-1].decode(), 16).to_bytes(len(data)//2, 'big')) except: pass
if data[-4:] == b'GNP\x89' or data[-2:] == b'KP' or b"i4Mu1S" in data \ or data[-8:-4] == b'GNP\x89' or data[-6:-4] == b'KP' or b'FQY=)' in data: print('auto [::-1]') new_data = data[::-1] return ask_next(new_data)
if b'\x91\x0a\x72\xe2' in data[:10] or b'\xe2\x72\x0a\x91' in data[-10:] \ or b'\x46\x4E\xAE\x2E\xA6\xFA\x0E\x36' in data \ or b'\x36\x0e\xfa\xa6\x2e\xae\x4e\x46' in data: print('auto bits[::-1] foreach byte') new_data = bytes([int(bin(i)[2:].zfill(8)[::-1], 2) for i in list(data)]) return ask_next(new_data)
if b'S1uM4i' in data: print('auto submit flag') try: s.sendline(regex_flag(data)) SUBMITTED_ROUNDS += 1 if SUBMITTED_ROUNDS != 25: return get_chall() s.interactive() except Exception as e: print(e) print('failed to submit flag') pass
cmd = input('>> ').strip().split() if cmd[0] == '1': new_data = base64.b64decode(data.decode()) return ask_next(new_data) elif cmd[0] == '2': new_data = base64.b85decode(data.decode()) return ask_next(new_data) elif cmd[0] == '3': new_data = base64.b32decode(data.decode()) return ask_next(new_data) elif cmd[0] == '4': new_data = int(data.decode(), 16).to_bytes(len(data)//2, 'big') return ask_next(new_data) elif cmd[0] == '5': new_data = data[::-1] return ask_next(new_data) elif cmd[0] == '6': new_data = bytes([int(bin(i)[2:].zfill(8)[::-1], 2) for i in list(data)]) return ask_next(new_data) elif cmd[0] == '7': new_data = data[int(cmd[1]):] return ask_next(new_data) elif cmd[0] == '8': new_data = data.rstrip(b'\x00') return ask_next(new_data) elif cmd[0] == '9': new_data = clear_junk_bytes(data, int(cmd[1], 16), int(cmd[2], 16)) return ask_next(new_data) else: print('invalid command') return ask_next(data) except Exception as e: print(e) return ask_next(data) def regex_flag(_data): import re t_flag = re.findall(b"S1uM4i.*}", _data) if len(t_flag) >= 0: return t_flag[0] else: raise Exception('not found')
def auto_base(data: str) -> bytes: if "S1uM4i" in data or "i4Mu1S" in data: raise Exception('is flag') check_base85_str = "!#$%&()*-;<>?@^_`{|}~" check_base64_str = "abcdefghijklmnopqrstuvwxyz" for _ in range(len(check_base85_str)): if check_base85_str[_] in data: print("auto decode base85") return base64.b85decode(data) for _ in range(len(check_base64_str)): if check_base64_str[_] in data: print("auto decode base64") return base64.b64decode(data) print("auto decode base32") return base64.b32decode(data)
def calculate_printable_ratio(data): printable_chars = string.printable total_chars = len(data) printable_chars_count = sum(1 for char in data if chr(char) in printable_chars) printable_ratio = printable_chars_count / total_chars return printable_ratio
def get_chall() -> str: global s s.recvuntil(b'-----BEGIN MATRYOSHKA MESSAGE-----\n') context.log_level = 'info' msg = s.recvuntil(b'-----END MATRYOSHKA MESSAGE-----\n') context.log_level = 'debug' msg = msg.decode().split()[:-3] msg = ''.join(msg) msg = base64.b64decode(msg) ask_next(msg)
def png_judge_type(file_name: str) -> bytes: img = Image.open(file_name) if img.width != 640 or img.height != 640: print('pixel') img.close() return pixel_to_data(file_name) if img.getpixel((270, 90))[0] >> 7 == 1: print('LSB') cnt = 0 for x in range(640): if img.getpixel((x, 0))[1] & 1 == 1: cnt += 1 img.close() if cnt > 100: print('4panel lsb') return cartoon_lsb_4(file_name) else: print('2panel lsb') return cartoon_lsb_2(file_name) else: print('MSB') cnt = 0 for x in range(640): if img.getpixel((x, 0))[1] >> 7 == 1: cnt += 1 img.close() if cnt > 100: print('4panel msb') return cartoon_msb_4(file_name) else: print('2panel msb') return cartoon_msb_2(file_name)
def cartoon_msb_2(file_name: str) -> bytes: img = Image.open(file_name) res = '' for y in range(img.height): for x in range(img.width): r, g, b, a = img.getpixel((x, y)) if not b >> 7: res += str(a >> 7) img.close() return handle_res(res, file_name, 2)
def cartoon_lsb_2(file_name: str) -> bytes: img = Image.open(file_name) res = '' for y in range(img.height): for x in range(img.width): r, g, b, a = img.getpixel((x, y)) if not b & 1: res += str(a & 1) img.close() return handle_res(res, file_name, 2) def cartoon_lsb_4(file_name: str) -> bytes: img = Image.open(file_name) res = '' for y in range(img.height): for x in range(img.width): r, g, b, a = img.getpixel((x, y)) res += str(r & 1) res += str(g & 1) res += str(b & 1) res += str(a & 1) img.close() return handle_res(res, file_name, 4)
def cartoon_msb_4(file_name: str) -> bytes: img = Image.open(file_name) res = '' for y in range(img.height): for x in range(img.width): r, g, b, a = img.getpixel((x, y)) res += str(r >> 7) res += str(g >> 7) res += str(b >> 7) res += str(a >> 7) img.close() return handle_res(res, file_name, 4)
def handle_res(res: str, file_name: str, type: int) -> bytes: res_backup = res res = res.rstrip('0').rstrip('1')[20:].lstrip('1') if res[-7:] == '0000000' and res[:7] != '0000000': res = res[::-1] bits_per_row = 21 while bits_per_row < 200 \ and (res[bits_per_row*6 : bits_per_row*6+7] != '0000000' \ or not all([res[bits_per_row * _] == '0' for _ in range(7)]) \ or not -6 < len(res) // bits_per_row - bits_per_row < 6): bits_per_row += 1 if type == 4: return handle_as_file(res_backup) return handle_as_qr(res, bits_per_row, file_name) def handle_as_qr(res: str, bits_per_row: int, file_name: str) -> bytes: res += '1' * bits_per_row img = Image.new('1', (bits_per_row, len(res) // bits_per_row)) for y in range(len(res) // bits_per_row): for x in range(bits_per_row): img.putpixel((x, y), (int(res[y * bits_per_row + x]))) img.save(file_name + '.qr.png') qreader = QReader() image = cv2.cvtColor(cv2.imread(file_name + '.qr.png'), cv2.COLOR_BGR2RGB) decoded_text = qreader.detect_and_decode(image=image) decoded_text = decoded_text[0].encode() return decoded_text
def handle_as_file(res: str) -> bytes: res = res.rstrip('0') res += '0' * (8 - len(res) % 8) return int(res, 2).to_bytes(len(res) // 8, 'big')
def reorder_dword(data: bytes, order: str) -> bytes: data = list(data) if len(data) % 4 != 0: data += [0] * (4 - len(data) % 4) for _ in range(0, len(data), 4): data[_], data[_+1], data[_+2], data[_+3] = data[_+int(order[0])-1], data[_+int(order[1])-1], data[_+int(order[2])-1], data[_+int(order[3])-1] return bytes(data)
def clear_junk_bytes(data: bytes, to_remove: int, to_add_0x40: int) -> bytes: data = data.replace(bytes([to_remove]), b'') data = list(data) i = 0 while i < len(data): if data[i] == to_add_0x40: data[i+1] = data[i+1] + 0x40 data[i:] = data[i+1:] i += 1 print(data) print(max(data)) return bytes(data)
def brute_zip_pswd(file_name: str) -> bytes: with open(file_name, 'ab') as f: f.write(b'\x00' * 16) abspth = os.path.abspath(file_name) print(abspth) cb.copy(abspth) pswd = input('password: ').strip().encode() zfile = pyzipper.AESZipFile(file_name, 'r') os.makedirs(file_name+'.ext', exist_ok=True) zfile.extractall(file_name+'.ext', pwd=pswd) zfile.close() files = os.listdir(file_name+'.ext') for f in files: fhnd = open(file_name+'.ext/'+f, 'rb') data = fhnd.read() fhnd.close() return data
def pixel_to_data(file_name: str) -> bytes: img = Image.open(file_name) res = [] for y in range(img.height): for x in range(img.width): r, g, b, a = img.getpixel((x, y)) res += [r, g, b, a] return bytes(res).rstrip(b'\x00') + b'\x00'*16
if __name__ == '__main__': s = remote('1.95.11.7', 30721) s.recvuntil(b'flag\n') s.sendline(b'S1uM4i') get_chall()
|