<h1 id="Hack-The-Box">Hack The Box - Reversing (Easy Part 2)</h1>

Ghi chú:
<ul>
<li>Easy: 8-14 challenge</li>
<ul>
<li><a href="#You Cant C Me ">8. You Cant C Me </a></li>
<li><a href="#Baby Crypt ">9. Baby Crypt </a></li>
<li><a href="#Anti Flag ">10. Anti Flag </a></li>
<li><a href="#IRCWare ">11. IRCWare </a></li>
<li><a href="#Sekure Decrypt ">12. Sekure Decrypt </a></li>
<li><a href="#Hissss ">13. Hissss </a></li>
<li><a href="#Rebuilding ">14. Rebuilding </a></li>
</ul>
</li>
</ul>
<h2>Difficulty: Easy </h2>
<div id="You Cant C Me"></div>
<h3>Challenge 8: You Cant C Me</h3>
Can you see me?
<a href="https://drive.google.com/file/d/1XZKfEwOAiZxSKdf8nFMnqXRuLIWjt6b0/view?usp=sharing">Download Challenge Here</a>
<h3>Solution</h3>
Chúng ta có thể sử dụng lệnh 'file' để cung cấp cái nhìn tổng quan về loại tệp.

Mở chương trình trong IDA.

Bây giờ là lúc để khai thác nó.
:::info
```python
arr = [0x6D, 0x5E, 0x26, 0x26, 0x66, 0x69, 0x17, 0x55, 0x6F, 0x26, 0x6B, 0x55, 0x5A, 0x27, 0x5A, 0x55, 0x59, 0x55, 0x63, 0x29]
result = ''.join(chr(x + 10) for x in arr)
print(result)
```
:::
:::success
```
Flag: HTB{wh00ps!_y0u_d1d_c_m3}
```
:::
<div id="Baby Crypt"></div>
<h3>Challenge 9: Baby Crypt</h3>
Give me the key and take what's yours.
<a href="https://drive.google.com/file/d/1Ksxm3_2HDmX4i8RQtuiVgnQcqNMB13g9/view?usp=sharing">Download Challenge Here</a>
<h3>Solution</h3>
Chúng ta có thể sử dụng lệnh 'file' để cung cấp cái nhìn tổng quan về loại tệp.

Mở chương trình trong IDA.

Bây giờ là lúc để khai thác nó.
:::info
```python
arr = [0x3F, 0x64, 0x35, 0x0C, 0x48, 0x47, 0x05, 0x6F, 0x46, 0x04, 0x6F, 0x02, 0x04, 0x03, 0x13, 0x28, 0x52, 0x0E, 0x28, 0x58, 0x43, 0x0F, 0x00, 0x05, 0x56, 0x4D]
key = "w0w"
result = ''.join(chr(arr[i] ^ ord(key[i%3])) for i in range(26))
print(result)
```
:::
:::success
```
HTB{x0r_1s_us3d_by_h4x0r!}
```
:::
<div id="Anti Flag"></div>
<h3>Challenge 10: Anti Flag</h3>
Flag? What's a flag?
<a href="https://drive.google.com/file/d/1E8991qX-bKwoRgAtL0L4WAWN-2yRClyu/view?usp=sharing">Download Challenge Here</a>
<h3>Solution</h3>
Chúng ta có thể sử dụng lệnh 'file' để cung cấp cái nhìn tổng quan về loại tệp.

Mở chương trình trong IDA.

* Việc thực thi tệp trả về <b>No flag for you :(</b>
* Nếu trình gỡ lỗi phát hiện của nó trả về <b>Well done!!</b>.
* Không có ích gì với những thứ này. Lựa chọn cuối cùng là đi sâu vào <b>sub_13FF()</b>.


Đây là mã hóa RC4 với `ciphertext = D0C0354F0CD9232A484B090B543CF8C0E5DFD7790F3DDB35C400` và `key = 2asdf-012=14`.

Bạn cũng có thể vá tệp bằng đoạn mã IDAPython.
:::info
```python
import ida_bytes
ida_bytes.patch_byte(0x14F4,0XEB)
ida_bytes.patch_byte(0x1510,0XEB)
```
:::
:::success
```
Flag: HTB{y0u_trac3_m3_g00d!!!}
```
:::
<div id="IRCWare"></div>
<h3>Challenge 11: IRCWare</h3>
During a routine check on our servers we found this suspicious binary, although when analyzing it we couldn't get it to do anything. We assume it's dead malware, but maybe something interesting can still be extracted from it?
<a href="https://drive.google.com/file/d/1QYDqsWok0eGL1cPKklx67CVks20SZrv5/view?usp=sharing">Download Challenge Here</a>
<h3>Solution</h3>

Mở chương trình trong IDA.

IDA là một công cụ disassembler rất tốt, nó đã cho ta biết được một số lệnh `system call`. Ta cũng có thể dùng <a href="https://blog.rchapman.org/posts/Linux_System_Call_Table_for_x86_64/">LINUX SYSTEM CALL TABLE </a> để biết cách syscall() thực hiện.


Hệ thống tạo một số ngẫu nhiên (4 byte) và thêm vào <b>Nickware</b> as Nick


`Hàm sub_40028F()`, chương trình giao tiếp với địa chỉ IP đã biết. Tuy nhiên `push 100007Fh (IP)` và `push 401Fh (port)` chưa mang nhiều ý nghĩa lắm. Ta dùng <a href="https://gchq.github.io/CyberChef/">CyberChef</a> và nhận được IP=127.0.0.1 (localhost) and port=8000.


Chúng ta có thể tham gia vào giao thức IRC bằng cách sử dụng kênh “#secret”. Chúng tôi sẽ gửi tin nhắn trên máy khách để cung cấp cho chúng tôi một lá cờ. Chúng ta có thể thực hiện 3 hành động.
* PRIVMSG #secret :@pass
* PRIVMSG #secret :@flag
* PRIVMSG #secret :@exec
Nó yêu cầu mật khẩu để có được cờ. Chúng tôi tìm thấy nó giải mã mật mã và so sánh mật khẩu.

Đó là ROT13 với số vòng quay = 9

Bây giờ chúng ta có thể gửi tin nhắn trên máy khách để cung cấp cho chúng ta một lá cờ.

:::success
```
Flag: HTB{m1N1m411st1C_fL4g_pR0v1d3r_b0T}
```
:::
<div id="Sekure Decrypt"></div>
<h3>Challenge 12: Sekure Decrypt</h3>
Timmy created a secure decryption program
<a href="https://drive.google.com/file/d/1_ZkTPgtgap9rnVsGBufE5_8cI9gZk5GC/view?usp=sharing">Download Challenge Here</a>
<h3>Solution</h3>
Chúng ta có thể sử dụng lệnh 'file' để cung cấp cái nhìn tổng quan về loại tệp.

Chúng ta được cho một tệp <a href="https://en.wikipedia.org/wiki/Core_dump">core dump</a>, một `decrypt file` và một `source code file`.
:::info
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
int encrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len) {
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if( buffer_len % blocksize != 0 ) {
return 1;
}
mcrypt_generic_init(td, key, key_len, IV);
mcrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit (td);
mcrypt_module_close(td);
return 0;
}
int decrypt(void* buffer, int buffer_len, char* IV, char* key, int key_len) {
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if( buffer_len % blocksize != 0 ){
return 1;
}
mcrypt_generic_init(td, key, key_len, IV);
mdecrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit (td);
mcrypt_module_close(td);
return 0;
}
void* read_file(char* filename, int len) {
FILE *fp = fopen(filename, "rb");
void* data = malloc(len);
fread(data, 1, len, fp);
fclose(&fp);
return data;
}
int main(int argc, char* argv[]) // gcc src.c -o dec -lmcrypt -ggdb
{
char* IV = "AAAAAAAAAAAAAAAA";
char *key = getenv("KEY");
int keysize = 16;
char* buffer;
int buffer_len = 16;
void *ciphertext = read_file("flag.enc", buffer_len);
decrypt(ciphertext, buffer_len, IV, key, keysize);
printf("Decrypted contents: %s\n", ciphertext);
return 0;
}
```
:::
1. Raw memory dump là bản sao hoàn chỉnh của nội dung bộ nhớ của máy tính tại một thời điểm cụ thể. Nó bao gồm tất cả các dữ liệu đang được sử dụng bởi hệ điều hành, chương trình đang chạy và dữ liệu khác được lưu trữ trong RAM. Nó không có header, metadata hay magic number.
1. Segment fault: Đây là một loại lỗi bộ nhớ xảy ra khi một chương trình cố gắng truy cập một vùng nhớ mà nó không được phép truy cập. Điều này thường xảy ra do lỗi lập trình hoặc lỗi dữ liệu.
1. Dump: Khi xảy ra lỗi, hệ thống có thể tạo ra một bản sao của nội dung bộ nhớ vào một tệp gọi là core dump. Core dump này chứa thông tin chi tiết về trạng thái của chương trình tại thời điểm xảy ra lỗi, giúp cho việc phân tích và khắc phục lỗi dễ dàng hơn.
Chương trình có `IV = 16*"A"` and `key = lấy khóa từ biến môi trường`. Đọc bộ đệm của `flag.enc` dưới dạng bản mã và giải mã để lấy nội dung giải mã. Hàm `encrypt() và decrypt()` là mã hóa AES
Chương trình đã gặp lỗi phân đoạn và để giúp việc phân tích lỗi dễ dàng hơn, hệ thống đã tạo ra một core dump chứa thông tin chi tiết về lỗi. `Key` đã được load vào trong chương trình cũng như một vài `ciphertext` được load cho tới khi crash.
Về cơ bản, ta có thể tìm `key` trong `dump`. Ta tìm được `key = VXISlqY>Ve6D<{#F`

Mở chương trình trong IDA, chúng ta có thể thấy trạng thái được ghi lại của bộ nhớ làm việc. Ta sẽ có dữ liệu dưới dạng bản mã và ta cần xác định phần bù để giải mã. Với `src.c`, chúng ta có thể thấy rằng mỗi lần lấy 16 byte làm bộ đệm để mã hóa và `buffer_encrypted` cũng có 16 byte.
Ý tưởng của tôi là đọc từng byte 16 để giải mã, bruceforce cho đến khi chúng tôi tìm thấy mẫu cờ.
:::info
```cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcrypt.h>
#define BLOCK_SIZE 16
#define KEY_SIZE 16
void processFile(FILE *fp, char *IV, char *key);
int decrypt(void *buffer, int buffer_len, char *IV, char *key, int key_len) {
MCRYPT td = mcrypt_module_open("rijndael-128", NULL, "cbc", NULL);
int blocksize = mcrypt_enc_get_block_size(td);
if (buffer_len % blocksize != 0) {
return 1;
}
mcrypt_generic_init(td, key, key_len, IV);
mdecrypt_generic(td, buffer, buffer_len);
mcrypt_generic_deinit(td);
mcrypt_module_close(td);
return 0;
}
int main(int argc, char *argv[]) {
char IV[] = "AAAAAAAAAAAAAAAA";
char key[] = "VXISlqY>Ve6D<{#F";
FILE *fp = fopen("core", "rb");
if (fp == NULL) {
perror("Error opening file");
return 1;
}
processFile(fp, IV, key);
fclose(fp);
return 0;
}
void processFile(FILE *fp, char *IV, char *key) {
fseek(fp, 0, SEEK_END);
int fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
for (int x = 0; x < fileSize; x += BLOCK_SIZE) {
void *ciphertext = malloc(BLOCK_SIZE);
fread(ciphertext, 1, BLOCK_SIZE, fp);
decrypt(ciphertext, BLOCK_SIZE, IV, key, KEY_SIZE);
if (strncmp(ciphertext, "HTB{", 4) == 0) {
printf("Decrypted contents (%d): %s\n", x, (char *)ciphertext);
free(ciphertext);
return;
}
free(ciphertext);
}
}
```
:::

:::success
```
Flag: HTB{t1m_l3arn_C}
```
:::
<div id="Hissss"></div>
<h3>Challenge 13: Hissss</h3>
Spying time. Check what all users have been up to with this Challenge recently.
<a href="https://drive.google.com/file/d/1ucAARUKumgEhA4IkD8JuO9LL33T_W4O5/view?usp=sharing">Download Challenge Here</a>
<h3>Solution</h3>
Chúng ta có thể sử dụng lệnh 'file' để cung cấp cái nhìn tổng quan về loại tệp.

Mở chương trình trong IDA. Chuỗi tìm kiếm, chúng tôi nhận được `%s.pkg` , `%s%s%s.exe` , `%s.py`
`.pkg` là phần mở rộng tên tệp được sử dụng cho một số định dạng tệp chứa các gói phần mềm và các tệp khác để được cài đặt trên một thiết bị, hệ điều hành hoặc hệ thống tập tin nhất định. Tôi đoán chương trình này chứa một số tệp có phần mở rộng tên tệp nối tiếp, đặc biệt là “auth.exe” , “ auth.py “ .
Tôi thực sự tin rằng nó chứa <i>auth.py</i> khi ta quan sát hàm `sub_3F30()`. Hơn nữa, chương trình còn đề cập đến `_MEIPASS` , một điều khá mới mẻ đối với tôi. Tìm kiếm nhanh cho tôi một số thông tin về <a href="https://stackoverflow.com/questions/22472124/what-is-sys-meipass-in-python">_MEIPASS</a>. Vì vậy trước hết chúng ta cần giải nén và dịch ngược mã python bằng <a href="https://github.com/extremecoders-re/pyinstxtractor">PyInstaller Extractor</a> và <a href="https://github.com/zrax/pycdc">Decompyle++</a> hoặc <a href="https://github.com/rocky/python-uncompyle6/">Uncompyle6</a>.

Ta đã dùng <a href="https://www.toolnb.com/tools-lang-en/pyc.html">https://www.toolnb.com/tools-lang-en/pyc.html</a>. Và đoạn code trở nên rất đẹp như sau:
:::info
```python
import sys
password = input('Enter password> ')
if len(password) != 12:
print("Sorry! You've entered the wrong password.")
sys.exit(0)
if ord(password[0]) != 48 or password[11] != '!' or ord(password[7]) != ord(password[5]) or 143 - ord(password[0]) != ord(password[4]) or ord(password[1]) ^ ord(password[3]) != 30 or ord(password[2]) * ord(password[3]) != 5610 or password[1] != 'p' or ord(password[6]) - ord(password[8]) != -46 or ord(password[6]) ^ ord(password[7]) != 64 or ord(password[10]) + ord(password[5]) != 166 or ord('n') - ord(password[9]) != 1 or password[10] != str(3):
print('Sorry, the password is incorrect.')
else:
print(f"Well Done! HTB{{{password}}}")
```
:::
Giải nó và ta sẽ có được mật khẩu là `0p3n_s3sam3!`
:::success
```
Flag: HTB{0p3n_s3sam3!}
```
:::
<div id="Rebuilding"></div>
<h3>Challenge 14: Rebuilding</h3>
You arrive on a barren planet, searching for the hideout of a scientist involved in the Longhir resistance movement. You touch down at the mouth of a vast cavern, your sensors picking up strange noises far below. All around you, ancient machinery whirrs and spins as strange sigils appear and change on the walls. You can tell that this machine has been running since long before you arrived, and will continue long after you're gone. Can you hope to understand its workings?
<a href="https://drive.google.com/file/d/1JJbc9qoAKHLH1Yx7U0g7iYVDo5ZcaMQh/view?usp=sharing">Download Challenge Here</a>
<h3>Solution</h3>
Chúng ta có thể sử dụng lệnh 'file' để cung cấp cái nhìn tổng quan về loại tệp.

Mở chương trình trong IDA.

Chúng ta có thể nhận ra rằng `input_value[i] = encrypted[i] ^ key[i%len(key)]`. Lúc đầu, `key = humans` nhưng giá trị trả về lại sai. Tôi tìm thấy xrefs cho `sub_84A()` được gọi bởi gọi bởi <b>_libc_csu_init()</b>.
Hàm _libc_csu_init (C Runtime Support Initialization) là một thủ tục được trình biên dịch C thêm vào chương trình để thực hiện các tác vụ khởi tạo liên quan đến thư viện C và môi trường thực thi trước khi hàm main() được gọi. Vì vậy khi chạy chương trình, `key = aliens`. <a href="https://security.stackexchange.com/questions/196096/why-does-my-stack-contain-the-return-address-to-libc-csu-init-after-main-is-in">Đọc thêm tại đây</a>

:::success
```
Flag: HTB{h1d1ng_c0d3s_1n_c0nstruct0r5}
```
:::