# Writeup RE + MISC
## BabyRE
Sử dụng DiE (Detect-It-Easy) để phân tích file --> **PE32**
Check main

>tự hiểu
sub_401020 = printf
sub_401050 = scanf

Dựa vào ảnh, ta có thể thấy giá trị của eax là `var_28`, nhấn vào thì thấy `var_28` là một xmmword, có khả năng `var_28` là **xmmword_41B6E0** (giá trị gán cho v8)



Vậy là khẳng định được giá trị của `var_28` chính là **xmmword_41B6E0**
--> Việc cần làm là tìm giá trị trên vùng **xmmword_41B6E0**
Mặt khác, trở lại phần nhập v12

Ta có hhd là 1 byte (giá trị từ -127 đến 128) --> nhập 5 số có giá trị từ -127 đến 128 trong file `.exe`
Tiếp đến đọc đến phần input đầu vào

- Dòng 23 + 24 cho thấy:
```
v5[0] = v8[j] ^ v12[j%5]
```
- Dòng 25 + 26 cho thấy:
```
v5[1] = v12[j -5(&v8 + j+1-&v8)/5+1] ^ v8[j+1] = v12[j - 5(j+1)/5 +1] ^ v8[j+1] = v12[(j+1)%5] ^ v8[j+1]
```
Tóm lại, code lại đơn giản như sau
```c
for(int j = 0; j<26; j++)
v8[j] ^= v12[j%5];
```
trở lại với IDA, ta thấy 
`Guest your flag. The flag will be of the form flag{[1-zA-Z0-9]+}`
Dựa vào form dễ thấy 5 char đầu của flag lag `flag{` trong khi v12 cũng có 5 giá trị
Viết code dịch ngược:
```python=1
find = open("input.txt", "r", encoding='utf-8')
fout = open("output.txt", "w", encoding='utf-8')
data = find.readlines()
s = 'flag{'
input = []
for i in range(len(s)):
input.append(chr(ord(s[i])^(int(data[i], 16))))
for i in range(len(input)):
fout.write(str(ord(input[i])) +'\n')
find.close()
fout.close()
```
với file input.txt chứa các byte (dạng hex) của data
Sau khi chạy thu được:
```python=
4
8
15
23
42
```
### flag{easy_baby_challenge}
## HiddenTreasure
File zip cho ta 2 file bao gồm 1 file .bmp và 1 file .exe
Dựa vào việc .bmp ta có thể đoán được đây là 1 file ảnh được hiển thị bằng các bit lưu trữ (bitmap)
Ném file HiddenTreasure.exe vào IDAPro và để ý 1 chút:

Dễ thấy argv[2] dùng cho CreateFileW dùng để tạo (hoặc mở file) --> khả năng argv[2] ~ (ảnh gì đó quên tên rồi).bmp nên ta dự đoán argv[1] liên quan đến flag
Dựa vào code đọc được, ta có thể thấy code ảnh hưởng trực tiếp đến file ảnh là `v26[2* v21+ v21]=v22` (v26 ~ v17 ~ IpBaseAddress[v14] <-- v13)
Dễ thấy v21 là biến không đổi (v19 không đổi, i_Width, i_v9 là số vòng lặp phụ thuộc vào flag. Mà flag không đổi nên i_v9 không đổi suy ra v21 không đổi)
Ngoài ra v22 cũng không đổi (cứ lằng nhằng họ hàng nhà chúng nó ra đến cái đầu là hiểu)
--> Tìm tất cả mảng v22 =))
Điều cần làm là
```c=1
cout<<int((int8_t)BitmapData[2 * v21 + v21])<<endl;
```
(in ra giá trị đúng của v26[2* v21+v21])
Code:
```c=1
#include <bits/stdc++.h>
#include <windows.h>
#include <windef.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define el '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MaxN = 1e6 + 1e5;
const int mod = 1e9 + 7;
int TEST = 1;
#define MAXSIZE 100
char PointerAddress[1000000];
char ssss[100000];
char ggg[] = "C:\\Users\\DELL\\Desktop\\reverse\\inside-the-mind-of-a-hacker-memory.bmp";
void solve()
{
cin >> ssss;
unsigned int Destination_length;
HANDLE File_Write_Handle;
void *v5;
HANDLE FileMappingW_Handle;
void *v8;
unsigned int i_Destination;
unsigned int i_v9;
char v12;
const void *v13;
DWORD DataOffset;
unsigned int v15;
int ImageWidth;
char *v17;
int i_Height;
int v19;
int i_Width;
int v21;
char v22;
HANDLE v23;
HANDLE hObject;
int v25;
char *BitmapData;
int ImageHeight;
char *PointerAddress;
byte *input_byte;
DWORD picture_SIZE;
int v31;
unsigned int v32;
int v33;
char Destination[256];
{
memset(Destination, 0, sizeof(Destination));
wcstombs(Destination, (const wchar_t *)ssss, 0x100u);
Destination_length = 256;
if (Destination_length)
{
File_Write_Handle = CreateFileW(L"C:\\Users\\DELL\\Desktop\\reverse\\inside-the-mind-of-a-hacker-memory.bmp", 0xC0000000, 1u, 0, 3u, 0x80u, 0);
v5 = File_Write_Handle;
v23 = File_Write_Handle;
if (File_Write_Handle != (HANDLE)-1)
{
picture_SIZE = GetFileSize(File_Write_Handle, 0);
if (picture_SIZE == -1 || (FileMappingW_Handle = CreateFileMappingW(v5, 0, 4u, 0, 0, 0),
v8 = FileMappingW_Handle,
(hObject = FileMappingW_Handle) == 0))
{
CloseHandle(v5);
return;
}
PointerAddress = (char *)MapViewOfFile(FileMappingW_Handle, 6u, 0, 0, 0);
if (PointerAddress)
{
byte *v9 = (byte *)malloc(8 * Destination_length);
i_Destination = 0;
input_byte = v9;
i_v9 = 0;
v32 = 0;
do
{
v12 = Destination[i_Destination];
v9[i_v9] = v12 & 1;
v9[i_v9 + 1] = (v12 >> 1) & 1;
v9[i_v9 + 2] = (v12 >> 2) & 1;
v9[i_v9 + 3] = (v12 >> 3) & 1;
v9[i_v9 + 4] = (v12 >> 4) & 1;
v9[i_v9 + 5] = (v12 >> 5) & 1;
v9[i_v9 + 6] = (v12 >> 6) & 1;
i_Destination = v32 + 1;
v9[i_v9 + 7] = (v12 >> 7) & 1;
i_v9 += 8;
v32 = i_Destination;
} while (i_Destination < Destination_length);
v13 = PointerAddress;
{
DataOffset = *(int *)(PointerAddress + 10);
if (DataOffset < picture_SIZE && i_v9 < *(int *)(PointerAddress + 34))
{
v15 = 0;
ImageWidth = *(int *)(PointerAddress + 18);
ImageHeight = *(int *)(PointerAddress + 22);
v17 = &PointerAddress[DataOffset];
i_Height = 0;
v25 = ImageWidth;
BitmapData = v17;
*((int16_t *)PointerAddress + 3) = i_v9;
v33 = 0;
if (i_v9)
{
v19 = 0;
v31 = 0;
do
{
if (i_Height >= ImageHeight)
break;
i_Width = 0;
if (v15 < i_v9)
{
do
{
if (i_Width >= ImageWidth)
break;
v21 = v19 + i_Width++;
v22 = input_byte[v15++];
cout << int((int8_t)BitmapData[2 * v21 + v21]) << endl;
v19 = v31;
ImageWidth = v25;
} while (v15 < i_v9);
i_Height = v33;
}
++i_Height;
v19 += 3 * ImageWidth;
v33 = i_Height;
v31 = v19;
} while (v15 < i_v9);
v13 = PointerAddress;
}
v9 = input_byte;
}
}
free(v9);
UnmapViewOfFile(v13);
CloseHandle(hObject);
CloseHandle(v23);
}
else
{
CloseHandle(v8);
CloseHandle(v5);
}
}
}
}
return;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
freopen("C:\\Users\\DELL\\Desktop\\reverse\\inside-the-mind-of-a-hacker-memory.bmp", "rb", stdin);
freopen("output.txt", "w", stdout);
while (TEST--)
solve();
}
```
Sau khi chạy chương trình, ta check file `output.txt` và để ý đề bài có liên quan đến bitmap --> chỉ chọn những số 0 và 1, sau đó ghép lại thành 1 byte --> chuyển qua ascii
Hoặc có thể chuyển hết những ký tự 0 và 1 trong file cho vào file `decode.txt` và chạy file python sau:
```python=1
fout = open("flag.txt", "w")
finp = open("decode.txt","r")
i = 0
ans = []
c = ''
while True:
line = str(finp.readline())
if (line == ''):
break
i = i+1
if i==8:
i = 0
c = c + line[:-1]
ans.append(c)
c = ''
else:
c = c + line[:-1]
for s in ans:
fout.write(str(chr(int(s[::-1],2))))
finp.close()
fout.close()
```
Và flag sẽ nằm trong `flag.txt`
### flag{d0nt_f0rget-th3_treasur3}