summaryrefslogtreecommitdiffstats
path: root/meta
diff options
context:
space:
mode:
Diffstat (limited to 'meta')
-rw-r--r--meta/.gitignore1
-rw-r--r--meta/Makefile11
-rw-r--r--meta/encrypt.py10
-rw-r--r--meta/flag.bmpbin0 -> 2074762 bytes
-rw-r--r--meta/flag.bmp.encbin0 -> 2074768 bytes
-rw-r--r--meta/recover.c150
-rw-r--r--meta/recover.py55
7 files changed, 227 insertions, 0 deletions
diff --git a/meta/.gitignore b/meta/.gitignore
new file mode 100644
index 0000000..f3880fa
--- /dev/null
+++ b/meta/.gitignore
@@ -0,0 +1 @@
+recover
diff --git a/meta/Makefile b/meta/Makefile
new file mode 100644
index 0000000..c3b984e
--- /dev/null
+++ b/meta/Makefile
@@ -0,0 +1,11 @@
+CFLAGS = -g
+LDLIBS = -lraylib
+
+all: recover
+
+clean:
+ rm -f recover
+
+recover: recover.c
+
+.PHONY: all clean
diff --git a/meta/encrypt.py b/meta/encrypt.py
new file mode 100644
index 0000000..5451f4a
--- /dev/null
+++ b/meta/encrypt.py
@@ -0,0 +1,10 @@
+#!/usr/bin/env python3
+
+from Crypto.Cipher import AES
+import os, sys
+
+img_in = open(sys.argv[1], "rb").read()
+img_in += b'\00' * (16 - (len(img_in) % 16))
+cipher = AES.new(os.urandom(16), AES.MODE_ECB)
+img_out = cipher.encrypt(img_in)
+open(sys.argv[1] + ".enc", "wb+").write(img_out)
diff --git a/meta/flag.bmp b/meta/flag.bmp
new file mode 100644
index 0000000..091c585
--- /dev/null
+++ b/meta/flag.bmp
Binary files differ
diff --git a/meta/flag.bmp.enc b/meta/flag.bmp.enc
new file mode 100644
index 0000000..4f7ecfe
--- /dev/null
+++ b/meta/flag.bmp.enc
Binary files differ
diff --git a/meta/recover.c b/meta/recover.c
new file mode 100644
index 0000000..54574b6
--- /dev/null
+++ b/meta/recover.c
@@ -0,0 +1,150 @@
+#include "raylib.h"
+
+#include <err.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+
+#define BLOCKSIZE 16
+#define BESTN 4
+
+struct block {
+ uint8_t data[BLOCKSIZE];
+ uint32_t cnt;
+};
+
+uint8_t *
+read_file(const char *filename, size_t *size)
+{
+ uint8_t *buf;
+ FILE *f;
+
+ f = fopen(filename, "r");
+ if (!f) err(1, "fopen");
+
+ fseek(f, 0, SEEK_END);
+ *size = ftell(f);
+ fseek(f, 0, SEEK_SET);
+
+ buf = malloc(*size);
+ if (!buf) err(1, "malloc");
+
+ if (!fread(buf, *size, 1, f))
+ err(1, "fread");
+
+ fclose(f);
+
+ return buf;
+}
+
+void
+update(struct block *blocks, struct block *bestblocks,
+ size_t *blockslen, uint8_t *img, size_t imglen)
+{
+ struct block cmp, tmp;
+ size_t tmpcnt;
+ size_t i, k;
+
+ memset(bestblocks, 0, BESTN * sizeof(struct block));
+ *blockslen = 0;
+ for (i = 0; i < imglen - BLOCKSIZE + 1; i += BLOCKSIZE) {
+ for (k = 0; k < *blockslen; k++) {
+ if (!memcmp(blocks[k].data, &img[i], BLOCKSIZE)) {
+ blocks[k].cnt += 1;
+ break;
+ }
+ }
+ if (k == *blockslen) {
+ memcpy(blocks[k].data, &img[i], BLOCKSIZE);
+ blocks[k].cnt = 1;
+ *blockslen += 1;
+ }
+
+ memcpy(&cmp, &blocks[k], sizeof(struct block));
+ for (k = 0; k < BESTN; k++) {
+ if (cmp.cnt > bestblocks[k].cnt || !bestblocks[k].cnt) {
+ memcpy(&tmp, &bestblocks[k], sizeof(struct block));
+ memcpy(&bestblocks[k], &cmp, sizeof(struct block));
+ memcpy(&cmp, &tmp, sizeof(struct block));
+ }
+ }
+ }
+}
+
+
+int
+main(int argc, const char **argv)
+{
+ const int wwidth = 1920;
+ const int wheight = 1080;
+ struct block bestblocks[BESTN];
+ struct block *blocks;
+ char textbuf[256];
+ size_t blockslen;
+ size_t imgoff, imglen;
+ size_t width, height;
+ size_t x, y, pos, i;
+ uint8_t *img;
+
+ if (argc < 2) return 1;
+
+ img = read_file(argv[1], &imglen);
+
+ blocks = malloc(imglen / BLOCKSIZE * sizeof(struct block));
+ if (!blocks) err(1, "malloc");
+
+ InitWindow(wwidth, wheight, "bmpass");
+
+ update(blocks, bestblocks, &blockslen, img, imglen);
+
+ width = wwidth;
+ imgoff = 0;
+ while (!WindowShouldClose()) {
+ if (IsKeyDown(KEY_LEFT) && width > 0)
+ width -= 1;
+ else if (IsKeyDown(KEY_RIGHT))
+ width += 1;
+ else if (IsKeyDown(KEY_DOWN) && imgoff > 0)
+ imgoff -= BLOCKSIZE;
+ else if (IsKeyDown(KEY_UP))
+ imgoff += BLOCKSIZE;
+ else if (IsKeyDown(KEY_ESCAPE))
+ break;
+
+ BeginDrawing();
+
+ ClearBackground(GRAY);
+
+ height = (imglen - imgoff) / width;
+ for (y = 0; y < height; y++) {
+ if (y >= wheight) continue;
+ for (x = 0; x < width; x++) {
+ if (x >= wwidth) continue;
+ pos = (imgoff + y * width + x)
+ / BLOCKSIZE * BLOCKSIZE;
+ if (pos + BLOCKSIZE >= imglen) continue;
+ for (i = 0; i < BESTN; i++) {
+ if (!memcmp(bestblocks[i].data,
+ img + pos, BLOCKSIZE))
+ break;
+ }
+ if (i != BESTN)
+ DrawPixel(x, wheight - 1 - y, BLACK);
+ else
+ DrawPixel(x, wheight - 1 - y, WHITE);
+ }
+ }
+
+ snprintf(textbuf, sizeof(textbuf), "imgoff %lu", imgoff);
+ DrawText(textbuf, 10, wheight - 40, 20, BLACK);
+
+ snprintf(textbuf, sizeof(textbuf), "width %lu", width);
+ DrawText(textbuf, 10, wheight - 20, 20, BLACK);
+
+ EndDrawing();
+ }
+
+ CloseWindow();
+}
diff --git a/meta/recover.py b/meta/recover.py
new file mode 100644
index 0000000..72b3a6c
--- /dev/null
+++ b/meta/recover.py
@@ -0,0 +1,55 @@
+#!/usr/bin/env python3
+
+from pyray import *
+import struct
+import sys
+
+img_enc = open(sys.argv[1], "rb").read()
+
+
+blocksize = 16
+
+pix = img_enc[0x480:] # offset needs to be blocksize aligned
+img_off = 0x0a
+
+print(len(pix) - img_off, 1920 * 1080)
+
+blocks = {}
+for i in range(0, len(pix)-blocksize+1, blocksize):
+ block = pix[i:i+blocksize]
+ if block not in blocks:
+ blocks[block] = []
+ blocks[block].append(i)
+blocks_items = [(k,len(v)) for k,v in blocks.items()]
+blocks_sorted = sorted(blocks_items, key=lambda x: x[1], reverse=True)[8:]
+blocks_top = {k:v for k,v in blocks_sorted}
+
+imgbuf = []
+for i in range(0, len(pix)-blocksize+1, blocksize):
+ block = pix[i:i+blocksize]
+ if block not in blocks_top:
+ imgbuf += [Color(255, 255, 255, 255)] * blocksize
+ else:
+ imgbuf += [Color(0, 0, 0, 255)] * blocksize
+
+width = 1920
+init_window(1920, 1080, "bmpass")
+while not window_should_close():
+ while key := get_key_pressed():
+ if key == KEY_LEFT:
+ width -= 1
+ elif key == KEY_RIGHT:
+ width += 1
+ elif key == KEY_UP and img_off > 0:
+ img_off -= 1
+ elif key == KEY_DOWN:
+ img_off += 1
+ begin_drawing()
+ clear_background(GRAY)
+ height = len(imgbuf) // width
+ for y in range(height):
+ for x in range(width):
+ draw_pixel(x, height - 1 - y, imgbuf[img_off + y * width + x])
+ end_drawing()
+close_window()
+