| 1 | + | use printf_compat::{format as formatter, output}; |
| 2 | + | use std::alloc::{alloc, dealloc, Layout}; |
| 3 | + | use std::borrow::{Borrow, BorrowMut}; |
| 4 | + | use std::collections::HashMap; |
| 5 | + | use std::ffi::CStr; |
| 6 | + | use std::ffi::{c_char, c_int, c_short}; |
| 7 | + | use std::{ptr, slice}; |
| 8 | + | use windows::Win32::Foundation::HANDLE; |
| 9 | + | use windows::Win32::System::Threading::{PROCESS_INFORMATION, STARTUPINFOA}; |
| 10 | + | |
| 11 | + | #[repr(C)] |
| 12 | + | #[derive(Debug, Copy, Clone)] |
| 13 | + | pub struct DataP { |
| 14 | + | pub original: *mut c_char, |
| 15 | + | pub buffer: *mut c_char, |
| 16 | + | pub length: c_int, |
| 17 | + | pub size: c_int, |
| 18 | + | } |
| 19 | + | |
| 20 | + | #[repr(C)] |
| 21 | + | #[derive(Debug, Copy, Clone)] |
| 22 | + | pub struct FormatP { |
| 23 | + | pub original: *mut c_char, |
| 24 | + | pub buffer: *mut c_char, |
| 25 | + | pub length: c_int, |
| 26 | + | pub size: c_int, |
| 27 | + | } |
| 28 | + | |
| 29 | + | #[repr(C)] |
| 30 | + | #[derive(Debug, Clone)] |
| 31 | + | pub struct Carrier { |
| 32 | + | pub output: Vec<c_char>, |
| 33 | + | pub offset: usize, |
| 34 | + | } |
| 35 | + | |
| 36 | + | impl Carrier { |
| 37 | + | pub const fn new() -> Carrier { |
| 38 | + | Carrier { |
| 39 | + | output: Vec::new(), |
| 40 | + | offset: 0, |
| 41 | + | } |
| 42 | + | } |
| 43 | + | |
| 44 | + | pub fn append_char_array(&mut self, s: *mut c_char, len: c_int) { |
| 45 | + | let holder = unsafe { slice::from_raw_parts(s, len as usize) }; |
| 46 | + | |
| 47 | + | self.output.extend_from_slice(holder); |
| 48 | + | self.offset = self.output.len() - holder.len(); |
| 49 | + | } |
| 50 | + | |
| 51 | + | pub fn append_string(&mut self, s: String) { |
| 52 | + | let mut mapped = s.bytes().map(|c| c as i8).collect::<Vec<c_char>>(); |
| 53 | + | |
| 54 | + | self.output.append(&mut mapped); |
| 55 | + | self.offset = self.output.len() - s.len() as usize; |
| 56 | + | } |
| 57 | + | |
| 58 | + | pub fn flush(&mut self) -> String { |
| 59 | + | let mut result = String::new(); |
| 60 | + | |
| 61 | + | for c in self.output.iter() { |
| 62 | + | if (*c as u8) == 0 { |
| 63 | + | result.push(0x0a as char); |
| 64 | + | } else { |
| 65 | + | result.push(*c as u8 as char); |
| 66 | + | } |
| 67 | + | } |
| 68 | + | |
| 69 | + | result |
| 70 | + | } |
| 71 | + | |
| 72 | + | #[allow(dead_code)] |
| 73 | + | pub fn get_from_offset(&self, offset: usize) -> &[c_char] { |
| 74 | + | if offset >= self.output.len() { |
| 75 | + | return &[]; |
| 76 | + | } |
| 77 | + | |
| 78 | + | let (_, tail) = self.output.split_at(offset); |
| 79 | + | |
| 80 | + | return tail; |
| 81 | + | } |
| 82 | + | |
| 83 | + | #[allow(dead_code)] |
| 84 | + | pub fn get_from_current_offset(&self) -> &[c_char] { |
| 85 | + | let (_, tail) = self.output.split_at(self.offset); |
| 86 | + | |
| 87 | + | return tail; |
| 88 | + | } |
| 89 | + | |
| 90 | + | #[allow(dead_code)] |
| 91 | + | pub fn len(&self) -> usize { |
| 92 | + | return self.output.len(); |
| 93 | + | } |
| 94 | + | } |
| 95 | + | |
| 96 | + | static mut OUTPUT: Carrier = Carrier::new(); |
| 97 | + | |
| 98 | + | pub fn resolve_internal_functions(name: &str) -> Result<isize, String> { |
| 99 | + | match name { |
| 100 | + | "BeaconDataParse" => Ok((BeaconDataParse as *const ()) as isize), |
| 101 | + | "BeaconDataInt" => Ok((BeaconDataInt as *const ()) as isize), |
| 102 | + | "BeaconDataShort" => Ok((BeaconDataShort as *const ()) as isize), |
| 103 | + | "BeaconDataLength" => Ok((BeaconDataLength as *const ()) as isize), |
| 104 | + | "BeaconDataExtract" => Ok((BeaconDataExtract as *const ()) as isize), |
| 105 | + | "BeaconFormatAlloc" => Ok((BeaconFormatAlloc as *const ()) as isize), |
| 106 | + | "BeaconFormatReset" => Ok((BeaconFormatReset as *const ()) as isize), |
| 107 | + | "BeaconFormatFree" => Ok((BeaconFormatFree as *const ()) as isize), |
| 108 | + | "BeaconFormatAppend" => Ok((BeaconFormatAppend as *const ()) as isize), |
| 109 | + | "BeaconFormatPrintf" => Ok((BeaconFormatPrintf as *const ()) as isize), |
| 110 | + | "BeaconFormatToString" => Ok((BeaconFormatToString as *const ()) as isize), |
| 111 | + | "BeaconFormatInt" => Ok((BeaconFormatInt as *const ()) as isize), |
| 112 | + | "BeaconPrintf" => Ok((BeaconPrintf as *const ()) as isize), |
| 113 | + | "BeaconOutput" => Ok((BeaconOutput as *const ()) as isize), |
| 114 | + | "BeaconUseToken" => Ok((BeaconUseToken as *const ()) as isize), |
| 115 | + | "BeaconRevertToken" => Ok((BeaconRevertToken as *const ()) as isize), |
| 116 | + | "BeaconIsAdmin" => Ok((BeaconIsAdmin as *const ()) as isize), |
| 117 | + | "BeaconGetSpawnTo" => Ok((BeaconGetSpawnTo as *const ()) as isize), |
| 118 | + | "BeaconSpawnTemporaryProcess" => Ok((BeaconSpawnTemporaryProcess as *const ()) as isize), |
| 119 | + | "BeaconInjectProcess" => Ok((BeaconInjectProcess as *const ()) as isize), |
| 120 | + | "BeaconInjectTemporaryProcess" => Ok((BeaconInjectTemporaryProcess as *const ()) as isize), |
| 121 | + | "BeaconCleanupProcess" => Ok((BeaconCleanupProcess as *const ()) as isize), |
| 122 | + | "toWideChar" => Ok((to_wide_char as *const ()) as isize), |
| 123 | + | &_ => Err("[!] Couldn't find internal function.".into()), |
| 124 | + | } |
| 125 | + | } |
| 126 | + | |
| 127 | + | #[no_mangle] |
| 128 | + | pub extern "C" fn BeaconDataParse(parser: *mut DataP, buffer: *mut c_char, size: c_int) { |
| 129 | + | if parser.is_null() { |
| 130 | + | return; |
| 131 | + | } |
| 132 | + | |
| 133 | + | let mut pp: DataP = unsafe { *parser }; |
| 134 | + | |
| 135 | + | pp.original = buffer; |
| 136 | + | pp.buffer = buffer; |
| 137 | + | pp.length = size - 4; |
| 138 | + | pp.size = size - 4; |
| 139 | + | |
| 140 | + | unsafe { |
| 141 | + | pp.buffer = pp.buffer.add(4); |
| 142 | + | } |
| 143 | + | |
| 144 | + | unsafe { |
| 145 | + | *parser = pp; |
| 146 | + | } |
| 147 | + | |
| 148 | + | return; |
| 149 | + | } |
| 150 | + | |
| 151 | + | #[no_mangle] |
| 152 | + | pub extern "C" fn BeaconDataInt(parser: *mut DataP) -> c_int { |
| 153 | + | if parser.is_null() { |
| 154 | + | return 0; |
| 155 | + | } |
| 156 | + | |
| 157 | + | let mut pp: DataP = unsafe { *parser }; |
| 158 | + | |
| 159 | + | if pp.length < 4 { |
| 160 | + | return 0; |
| 161 | + | } |
| 162 | + | |
| 163 | + | let result: &[u8] = unsafe { slice::from_raw_parts(pp.buffer as *const u8, 4) }; |
| 164 | + | |
| 165 | + | let mut dst = [0u8; 4]; |
| 166 | + | dst.clone_from_slice(&result[0..4]); |
| 167 | + | |
| 168 | + | pp.buffer = unsafe { pp.buffer.add(4) }; |
| 169 | + | pp.length = pp.length - 4; |
| 170 | + | |
| 171 | + | unsafe { |
| 172 | + | *parser = pp; |
| 173 | + | } |
| 174 | + | |
| 175 | + | return i32::from_ne_bytes(dst) as c_int; |
| 176 | + | } |
| 177 | + | |
| 178 | + | #[no_mangle] |
| 179 | + | pub extern "C" fn BeaconDataShort(parser: *mut DataP) -> c_short { |
| 180 | + | if parser.is_null() { |
| 181 | + | return 0; |
| 182 | + | } |
| 183 | + | |
| 184 | + | let mut pp: DataP = unsafe { *parser }; |
| 185 | + | |
| 186 | + | if pp.length < 2 { |
| 187 | + | return 0; |
| 188 | + | } |
| 189 | + | |
| 190 | + | let result: &[u8] = unsafe { slice::from_raw_parts(pp.buffer as *const u8, 4) }; |
| 191 | + | |
| 192 | + | let mut dst = [0u8; 2]; |
| 193 | + | dst.clone_from_slice(&result[0..2]); |
| 194 | + | |
| 195 | + | pp.buffer = unsafe { pp.buffer.add(2) }; |
| 196 | + | pp.length = pp.length - 2; |
| 197 | + | |
| 198 | + | unsafe { |
| 199 | + | *parser = pp; |
| 200 | + | } |
| 201 | + | |
| 202 | + | return i16::from_ne_bytes(dst); |
| 203 | + | } |
| 204 | + | |
| 205 | + | #[no_mangle] |
| 206 | + | pub extern "C" fn BeaconDataLength(parser: *mut DataP) -> c_int { |
| 207 | + | if parser.is_null() { |
| 208 | + | return 0; |
| 209 | + | } |
| 210 | + | |
| 211 | + | let pp: DataP = unsafe { *parser }; |
| 212 | + | |
| 213 | + | return pp.length; |
| 214 | + | } |
| 215 | + | |
| 216 | + | #[no_mangle] |
| 217 | + | pub extern "C" fn BeaconDataExtract(parser: *mut DataP, size: *mut c_int) -> *mut c_char { |
| 218 | + | if parser.is_null() { |
| 219 | + | return ptr::null_mut(); |
| 220 | + | } |
| 221 | + | |
| 222 | + | let mut pp: DataP = unsafe { *parser }; |
| 223 | + | |
| 224 | + | if pp.length < 4 { |
| 225 | + | return ptr::null_mut(); |
| 226 | + | } |
| 227 | + | |
| 228 | + | let length_parts: &[u8] = unsafe { slice::from_raw_parts(pp.buffer as *const u8, 4) }; |
| 229 | + | |
| 230 | + | let mut length_holder = [0u8; 4]; |
| 231 | + | length_holder.clone_from_slice(&length_parts[0..4]); |
| 232 | + | |
| 233 | + | let length: u32 = u32::from_ne_bytes(length_holder); |
| 234 | + | |
| 235 | + | pp.buffer = unsafe { pp.buffer.add(4) }; |
| 236 | + | |
| 237 | + | let result = pp.buffer; |
| 238 | + | |
| 239 | + | if result.is_null() { |
| 240 | + | return ptr::null_mut(); |
| 241 | + | } |
| 242 | + | |
| 243 | + | pp.length = pp.length - 4; |
| 244 | + | |
| 245 | + | pp.length = pp.length - length as i32; |
| 246 | + | |
| 247 | + | pp.buffer = unsafe { pp.buffer.add(length as usize) }; |
| 248 | + | |
| 249 | + | if !size.is_null() && !result.is_null() { |
| 250 | + | unsafe { |
| 251 | + | *size = length as c_int; |
| 252 | + | } |
| 253 | + | } |
| 254 | + | |
| 255 | + | unsafe { |
| 256 | + | *parser = pp; |
| 257 | + | } |
| 258 | + | |
| 259 | + | return result; |
| 260 | + | } |
| 261 | + | |
| 262 | + | #[no_mangle] |
| 263 | + | pub extern "C" fn BeaconFormatAlloc(format: *mut FormatP, max_size: c_int) { |
| 264 | + | if format.is_null() { |
| 265 | + | return; |
| 266 | + | } |
| 267 | + | |
| 268 | + | if max_size == 0 { |
| 269 | + | return; |
| 270 | + | } |
| 271 | + | |
| 272 | + | let mut fp: FormatP = unsafe { *format }; |
| 273 | + | |
| 274 | + | let mut align: usize = 1; |
| 275 | + | |
| 276 | + | while align < max_size as usize { |
| 277 | + | align = align * 2; |
| 278 | + | } |
| 279 | + | |
| 280 | + | let layout = Layout::from_size_align(max_size as usize, align).unwrap(); |
| 281 | + | let ptr = unsafe { alloc(layout) }; |
| 282 | + | |
| 283 | + | fp.original = ptr as *mut i8; |
| 284 | + | fp.buffer = fp.original; |
| 285 | + | fp.length = 0; |
| 286 | + | fp.size = max_size; |
| 287 | + | |
| 288 | + | unsafe { |
| 289 | + | *format = fp; |
| 290 | + | } |
| 291 | + | |
| 292 | + | return; |
| 293 | + | } |
| 294 | + | |
| 295 | + | #[no_mangle] |
| 296 | + | pub extern "C" fn BeaconFormatReset(format: *mut FormatP) { |
| 297 | + | if format.is_null() { |
| 298 | + | return; |
| 299 | + | } |
| 300 | + | |
| 301 | + | let mut fp: FormatP = unsafe { *format }; |
| 302 | + | |
| 303 | + | let size = fp.size; |
| 304 | + | |
| 305 | + | // Free format |
| 306 | + | BeaconFormatFree(&mut fp); |
| 307 | + | |
| 308 | + | // Alloc format |
| 309 | + | BeaconFormatAlloc(&mut fp, size); |
| 310 | + | |
| 311 | + | unsafe { |
| 312 | + | *format = fp; |
| 313 | + | } |
| 314 | + | |
| 315 | + | return; |
| 316 | + | } |
| 317 | + | |
| 318 | + | #[no_mangle] |
| 319 | + | pub extern "C" fn BeaconFormatFree(format: *mut FormatP) { |
| 320 | + | if format.is_null() { |
| 321 | + | return; |
| 322 | + | } |
| 323 | + | |
| 324 | + | let mut fp: FormatP = unsafe { *format }; |
| 325 | + | |
| 326 | + | if !fp.original.is_null() { |
| 327 | + | let mut align: usize = 1; |
| 328 | + | |
| 329 | + | while align < fp.size as usize { |
| 330 | + | align = align * 2; |
| 331 | + | } |
| 332 | + | |
| 333 | + | let layout = Layout::from_size_align(fp.size as usize, align).unwrap(); |
| 334 | + | |
| 335 | + | unsafe { dealloc(fp.original as *mut u8, layout) }; |
| 336 | + | } |
| 337 | + | |
| 338 | + | fp.original = ptr::null_mut(); |
| 339 | + | fp.buffer = ptr::null_mut(); |
| 340 | + | fp.length = 0; |
| 341 | + | fp.size = 0; |
| 342 | + | |
| 343 | + | unsafe { |
| 344 | + | *format = fp; |
| 345 | + | } |
| 346 | + | |
| 347 | + | return; |
| 348 | + | } |
| 349 | + | |
| 350 | + | #[no_mangle] |
| 351 | + | pub extern "C" fn BeaconFormatAppend(format: *mut FormatP, text: *mut c_char, len: c_int) { |
| 352 | + | if format.is_null() { |
| 353 | + | return; |
| 354 | + | } |
| 355 | + | |
| 356 | + | let mut fp: FormatP = unsafe { *format }; |
| 357 | + | |
| 358 | + | if fp.length + len > fp.size { |
| 359 | + | return; |
| 360 | + | } |
| 361 | + | |
| 362 | + | unsafe { |
| 363 | + | ptr::copy_nonoverlapping(text, fp.original, len as usize); |
| 364 | + | } |
| 365 | + | |
| 366 | + | fp.buffer = unsafe { fp.buffer.add(len as usize) }; |
| 367 | + | fp.length = fp.length + len; |
| 368 | + | |
| 369 | + | unsafe { |
| 370 | + | *format = fp; |
| 371 | + | } |
| 372 | + | |
| 373 | + | return; |
| 374 | + | } |
| 375 | + | |
| 376 | + | #[no_mangle] |
| 377 | + | pub unsafe extern "C" fn BeaconFormatPrintf(format: *mut FormatP, fmt: *mut c_char, mut tail: ...) { |
| 378 | + | if format.is_null() { |
| 379 | + | return; |
| 380 | + | } |
| 381 | + | |
| 382 | + | let mut fp: FormatP = *format; |
| 383 | + | |
| 384 | + | let mut s = String::new(); |
| 385 | + | let bytes_written = formatter(fmt, tail.as_va_list(), output::fmt_write(&mut s)); |
| 386 | + | |
| 387 | + | if fp.length + bytes_written + 1 > fp.size { |
| 388 | + | return; |
| 389 | + | } |
| 390 | + | |
| 391 | + | s.push('\0'); |
| 392 | + | |
| 393 | + | ptr::copy_nonoverlapping(s.as_ptr(), fp.buffer as *mut u8, s.len()); |
| 394 | + | |
| 395 | + | fp.length = fp.length + s.len() as i32; |
| 396 | + | |
| 397 | + | *format = fp; |
| 398 | + | |
| 399 | + | return; |
| 400 | + | } |
| 401 | + | |
| 402 | + | #[no_mangle] |
| 403 | + | pub extern "C" fn BeaconFormatToString(format: *mut FormatP, size: *mut c_int) -> *mut c_char { |
| 404 | + | if format.is_null() { |
| 405 | + | return ptr::null_mut(); |
| 406 | + | } |
| 407 | + | |
| 408 | + | let fp: FormatP = unsafe { *format }; |
| 409 | + | |
| 410 | + | if fp.length == 0 { |
| 411 | + | return ptr::null_mut(); |
| 412 | + | } |
| 413 | + | |
| 414 | + | unsafe { |
| 415 | + | *size = fp.length; |
| 416 | + | } |
| 417 | + | |
| 418 | + | return fp.original; |
| 419 | + | } |
| 420 | + | |
| 421 | + | #[no_mangle] |
| 422 | + | pub extern "C" fn BeaconFormatInt(format: *mut FormatP, value: c_int) { |
| 423 | + | if format.is_null() { |
| 424 | + | return; |
| 425 | + | } |
| 426 | + | |
| 427 | + | let mut fp: FormatP = unsafe { *format }; |
| 428 | + | |
| 429 | + | if fp.length + 4 > fp.size { |
| 430 | + | return; |
| 431 | + | } |
| 432 | + | |
| 433 | + | let swapped = swap_endianness(value as u32); |
| 434 | + | let mut result = swapped.to_be_bytes(); |
| 435 | + | |
| 436 | + | unsafe { |
| 437 | + | ptr::copy_nonoverlapping(result.as_mut_ptr(), fp.original as *mut u8, 4); |
| 438 | + | } |
| 439 | + | |
| 440 | + | fp.buffer = unsafe { fp.buffer.add(4) }; |
| 441 | + | fp.length = fp.length + 4; |
| 442 | + | |
| 443 | + | unsafe { |
| 444 | + | *format = fp; |
| 445 | + | } |
| 446 | + | |
| 447 | + | return; |
| 448 | + | } |
| 449 | + | |
| 450 | + | #[no_mangle] |
| 451 | + | pub unsafe extern "C" fn BeaconPrintf(_: c_int, fmt: *mut c_char, mut tail: ...) { |
| 452 | + | let mut s = String::new(); |
| 453 | + | |
| 454 | + | formatter(fmt, tail.as_va_list(), output::fmt_write(&mut s)); |
| 455 | + | |
| 456 | + | s.push('\0'); |
| 457 | + | |
| 458 | + | OUTPUT.append_string(s); |
| 459 | + | |
| 460 | + | return; |
| 461 | + | } |
| 462 | + | |
| 463 | + | #[no_mangle] |
| 464 | + | pub extern "C" fn BeaconOutput(_: c_int, data: *mut c_char, len: c_int) { |
| 465 | + | unsafe { OUTPUT.append_char_array(data, len) } |
| 466 | + | } |
| 467 | + | |
| 468 | + | #[no_mangle] |
| 469 | + | fn get_output() -> Carrier { |
| 470 | + | return unsafe { OUTPUT.clone() }; |
| 471 | + | } |
| 472 | + | |
| 473 | + | #[no_mangle] |
| 474 | + | pub extern "C" fn swap_endianness(src: u32) -> u32 { |
| 475 | + | let test: u32 = 0x000000ff; |
| 476 | + | |
| 477 | + | // if test is 0xff00, then we are little endian, otherwise big endian |
| 478 | + | if (((test >> 24) & 0xff) as u8) == 0xff { |
| 479 | + | return src.swap_bytes(); |
| 480 | + | } |
| 481 | + | |
| 482 | + | return src; |
| 483 | + | } |
| 484 | + | |
| 485 | + | #[no_mangle] |
| 486 | + | /// Converts the src string to a UTF16-LE wide-character string, using the target's default encoding. |
| 487 | + | /// |
| 488 | + | /// # Arguments |
| 489 | + | /// |
| 490 | + | /// * `src` - The source string to convert. |
| 491 | + | /// * `dst` - The destination string. |
| 492 | + | /// * `max` - The size (in bytes!) of the destination buffer |
| 493 | + | /// |
| 494 | + | /// # Safety |
| 495 | + | /// This function is unsafe because it dereferences the src pointer. |
| 496 | + | pub extern "C" fn to_wide_char(src: *mut c_char, dst: *mut u16, max: c_int) -> i32 { |
| 497 | + | if src.is_null() { |
| 498 | + | return 0; |
| 499 | + | } |
| 500 | + | |
| 501 | + | let c_str: &CStr = unsafe { CStr::from_ptr(src) }; |
| 502 | + | |
| 503 | + | let str_slice: &str = match c_str.to_str() { |
| 504 | + | Ok(s) => s, |
| 505 | + | Err(_) => return 0, |
| 506 | + | }; |
| 507 | + | |
| 508 | + | let mut size = str_slice.len(); |
| 509 | + | |
| 510 | + | if size > max as usize { |
| 511 | + | size = max as usize - 1; |
| 512 | + | } |
| 513 | + | |
| 514 | + | let mut v: Vec<u16> = str_slice |
| 515 | + | .encode_utf16() |
| 516 | + | .take(size) |
| 517 | + | .map(|x| x as u16) |
| 518 | + | .collect(); |
| 519 | + | v.push(0); |
| 520 | + | |
| 521 | + | unsafe { ptr::copy(v.as_ptr(), dst, size) }; |
| 522 | + | |
| 523 | + | return 1; |
| 524 | + | } |
| 525 | + | |
| 526 | + | #[no_mangle] |
| 527 | + | pub extern "C" fn BeaconUseToken(token: HANDLE) -> i32 { |
| 528 | + | todo!() |
| 529 | + | } |
| 530 | + | |
| 531 | + | #[no_mangle] |
| 532 | + | pub extern "C" fn BeaconRevertToken() { |
| 533 | + | todo!() |
| 534 | + | } |
| 535 | + | |
| 536 | + | #[no_mangle] |
| 537 | + | pub extern "C" fn BeaconIsAdmin() -> i32 { |
| 538 | + | todo!() |
| 539 | + | } |
| 540 | + | |
| 541 | + | #[no_mangle] |
| 542 | + | pub extern "C" fn BeaconGetSpawnTo(x86: i32, buffer: *mut c_char, length: c_int) { |
| 543 | + | todo!() |
| 544 | + | } |
| 545 | + | |
| 546 | + | #[no_mangle] |
| 547 | + | pub extern "C" fn BeaconSpawnTemporaryProcess( |
| 548 | + | x86: i32, |
| 549 | + | ignoreToken: i32, |
| 550 | + | sInfo: *mut STARTUPINFOA, |
| 551 | + | pInfo: *mut PROCESS_INFORMATION, |
| 552 | + | ) -> i32 { |
| 553 | + | todo!() |
| 554 | + | } |
| 555 | + | |
| 556 | + | #[no_mangle] |
| 557 | + | pub extern "C" fn BeaconInjectProcess( |
| 558 | + | hProc: HANDLE, |
| 559 | + | pid: c_int, |
| 560 | + | payload: *mut c_char, |
| 561 | + | p_len: c_int, |
| 562 | + | p_offset: c_int, |
| 563 | + | arg: *mut c_char, |
| 564 | + | a_len: c_int, |
| 565 | + | ) { |
| 566 | + | todo!() |
| 567 | + | } |
| 568 | + | |
| 569 | + | #[no_mangle] |
| 570 | + | pub extern "C" fn BeaconInjectTemporaryProcess( |
| 571 | + | pInfo: *mut PROCESS_INFORMATION, |
| 572 | + | payload: *mut c_char, |
| 573 | + | p_len: c_int, |
| 574 | + | p_offset: c_int, |
| 575 | + | arg: *mut c_char, |
| 576 | + | a_len: c_int, |
| 577 | + | ) { |
| 578 | + | todo!() |
| 579 | + | } |
| 580 | + | |
| 581 | + | #[no_mangle] |
| 582 | + | pub extern "C" fn BeaconCleanupProcess(pInfo: *mut PROCESS_INFORMATION) { |
| 583 | + | todo!() |
| 584 | + | } |
| 585 | + | |
| 586 | + | #[cfg(test)] |
| 587 | + | mod tests { |
| 588 | + | use core::ffi::CStr; |
| 589 | + | use std::ffi::CString; |
| 590 | + | use std::{ptr, slice}; |
| 591 | + | |
| 592 | + | use crate::functions::*; |
| 593 | + | #[cfg(target_os = "windows")] |
| 594 | + | use winapi::{shared::minwindef::DWORD, shared::ntdef::c_char}; |
| 595 | + | |
| 596 | + | #[cfg(not(target_os = "windows"))] |
| 597 | + | use super::super::test_types::{c_char, DWORD}; |
| 598 | + | |
| 599 | + | unsafe fn reset_output() { |
| 600 | + | OUTPUT = Carrier::new(); |
| 601 | + | } |
| 602 | + | |
| 603 | + | #[test] |
| 604 | + | fn can_parse_beacon_data() { |
| 605 | + | let mut buffer: [c_char; 0xff] = [0i8; 0xff]; |
| 606 | + | |
| 607 | + | let mut parser = super::DataP { |
| 608 | + | original: ptr::null_mut(), |
| 609 | + | buffer: ptr::null_mut(), |
| 610 | + | length: 0, |
| 611 | + | size: 0, |
| 612 | + | }; |
| 613 | + | |
| 614 | + | assert_eq!(parser.length, 0); |
| 615 | + | assert_eq!(parser.size, 0); |
| 616 | + | assert_eq!(parser.buffer, ptr::null_mut()); |
| 617 | + | |
| 618 | + | BeaconDataParse(&mut parser, buffer.as_mut_ptr(), 0xff); |
| 619 | + | |
| 620 | + | assert_ne!(parser.original, ptr::null_mut()); |
| 621 | + | assert_ne!(parser.buffer, ptr::null_mut()); |
| 622 | + | assert_eq!(parser.length, 0xff - 4); |
| 623 | + | assert_eq!(parser.size, 0xff - 4); |
| 624 | + | |
| 625 | + | unsafe { |
| 626 | + | assert_eq!(parser.buffer, buffer.as_mut_ptr().add(4)); |
| 627 | + | } |
| 628 | + | } |
| 629 | + | |
| 630 | + | #[test] |
| 631 | + | fn can_extract_int_from_parser() { |
| 632 | + | let mut buffer: [c_char; 0xff] = [0i8; 0xff]; |
| 633 | + | |
| 634 | + | buffer[4] = 0x1; |
| 635 | + | buffer[5] = 0x4; |
| 636 | + | |
| 637 | + | let mut parser = super::DataP { |
| 638 | + | original: buffer.as_mut_ptr(), |
| 639 | + | buffer: buffer.as_mut_ptr(), |
| 640 | + | length: 0, |
| 641 | + | size: 0, |
| 642 | + | }; |
| 643 | + | |
| 644 | + | BeaconDataParse(&mut parser, buffer.as_mut_ptr(), 0xff); |
| 645 | + | |
| 646 | + | let result = BeaconDataInt(&mut parser); |
| 647 | + | |
| 648 | + | assert_eq!(1025, result); |
| 649 | + | } |
| 650 | + | |
| 651 | + | #[test] |
| 652 | + | fn can_extract_short_from_parser() { |
| 653 | + | let mut buffer: [c_char; 0xff] = [0i8; 0xff]; |
| 654 | + | |
| 655 | + | buffer[4] = 0x1; |
| 656 | + | buffer[5] = 0x4; |
| 657 | + | |
| 658 | + | let mut parser = super::DataP { |
| 659 | + | original: buffer.as_mut_ptr(), |
| 660 | + | buffer: buffer.as_mut_ptr(), |
| 661 | + | length: 0, |
| 662 | + | size: 0, |
| 663 | + | }; |
| 664 | + | |
| 665 | + | BeaconDataParse(&mut parser, buffer.as_mut_ptr(), 0xff); |
| 666 | + | |
| 667 | + | let result = BeaconDataShort(&mut parser); |
| 668 | + | |
| 669 | + | assert_eq!(1025, result); |
| 670 | + | } |
| 671 | + | |
| 672 | + | #[test] |
| 673 | + | fn can_extract_data_from_parser() { |
| 674 | + | let mut buffer: [c_char; 0xff] = [0i8; 0xff]; |
| 675 | + | |
| 676 | + | // set our data size |
| 677 | + | buffer[4] = 0x5; |
| 678 | + | |
| 679 | + | // set our data |
| 680 | + | buffer[8] = 104; |
| 681 | + | buffer[9] = 101; |
| 682 | + | buffer[10] = 108; |
| 683 | + | buffer[11] = 108; |
| 684 | + | buffer[12] = 111; |
| 685 | + | |
| 686 | + | let mut parser = super::DataP { |
| 687 | + | original: buffer.as_mut_ptr(), |
| 688 | + | buffer: buffer.as_mut_ptr(), |
| 689 | + | length: 0, |
| 690 | + | size: 0, |
| 691 | + | }; |
| 692 | + | |
| 693 | + | BeaconDataParse(&mut parser, buffer.as_mut_ptr(), 0xff); |
| 694 | + | |
| 695 | + | let mut size = 0; |
| 696 | + | |
| 697 | + | let result = BeaconDataExtract(&mut parser, &mut size); |
| 698 | + | |
| 699 | + | let string: &[u8] = unsafe { slice::from_raw_parts(result as *const u8, size as usize) }; |
| 700 | + | |
| 701 | + | let result_string = String::from_utf8_lossy(string); |
| 702 | + | |
| 703 | + | assert_eq!("hello", result_string); |
| 704 | + | assert_eq!(5, size); |
| 705 | + | } |
| 706 | + | |
| 707 | + | #[test] |
| 708 | + | fn can_return_data_length() { |
| 709 | + | let mut buffer: [c_char; 0xff] = [0i8; 0xff]; |
| 710 | + | let expected_length = 0xff - 4; |
| 711 | + | |
| 712 | + | let mut parser = super::DataP { |
| 713 | + | original: buffer.as_mut_ptr(), |
| 714 | + | buffer: buffer.as_mut_ptr(), |
| 715 | + | length: 0, |
| 716 | + | size: 0, |
| 717 | + | }; |
| 718 | + | |
| 719 | + | BeaconDataParse(&mut parser, buffer.as_mut_ptr(), 0xff); |
| 720 | + | |
| 721 | + | let length = BeaconDataLength(&mut parser); |
| 722 | + | |
| 723 | + | assert_eq!(length, expected_length); |
| 724 | + | } |
| 725 | + | |
| 726 | + | #[test] |
| 727 | + | fn can_allocate_format_p() { |
| 728 | + | let mut format = super::FormatP { |
| 729 | + | original: ptr::null_mut(), |
| 730 | + | buffer: ptr::null_mut(), |
| 731 | + | length: 0, |
| 732 | + | size: 0, |
| 733 | + | }; |
| 734 | + | |
| 735 | + | BeaconFormatAlloc(&mut format, 0xff); |
| 736 | + | |
| 737 | + | assert_ne!(format.original, ptr::null_mut()); |
| 738 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 739 | + | assert_eq!(format.length, 0); |
| 740 | + | assert_eq!(format.size, 0xff); |
| 741 | + | } |
| 742 | + | |
| 743 | + | #[test] |
| 744 | + | fn can_reset_format_p() { |
| 745 | + | let mut format = super::FormatP { |
| 746 | + | original: ptr::null_mut(), |
| 747 | + | buffer: ptr::null_mut(), |
| 748 | + | length: 0, |
| 749 | + | size: 0, |
| 750 | + | }; |
| 751 | + | |
| 752 | + | BeaconFormatAlloc(&mut format, 0xff); |
| 753 | + | |
| 754 | + | assert_ne!(format.original, ptr::null_mut()); |
| 755 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 756 | + | assert_eq!(format.length, 0); |
| 757 | + | assert_eq!(format.size, 0xff); |
| 758 | + | |
| 759 | + | BeaconFormatReset(&mut format); |
| 760 | + | |
| 761 | + | assert_ne!(format.original, ptr::null_mut()); |
| 762 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 763 | + | assert_eq!(format.length, 0); |
| 764 | + | assert_eq!(format.size, 0xff); |
| 765 | + | } |
| 766 | + | |
| 767 | + | #[test] |
| 768 | + | fn can_deallocate_format_p() { |
| 769 | + | let mut format = super::FormatP { |
| 770 | + | original: ptr::null_mut(), |
| 771 | + | buffer: ptr::null_mut(), |
| 772 | + | length: 0, |
| 773 | + | size: 0, |
| 774 | + | }; |
| 775 | + | |
| 776 | + | BeaconFormatAlloc(&mut format, 0xff); |
| 777 | + | |
| 778 | + | assert_ne!(format.original, ptr::null_mut()); |
| 779 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 780 | + | assert_eq!(format.length, 0); |
| 781 | + | assert_eq!(format.size, 0xff); |
| 782 | + | |
| 783 | + | BeaconFormatFree(&mut format); |
| 784 | + | |
| 785 | + | assert_eq!(format.original, ptr::null_mut()); |
| 786 | + | assert_eq!(format.buffer, ptr::null_mut()); |
| 787 | + | assert_eq!(format.length, 0); |
| 788 | + | assert_eq!(format.size, 0); |
| 789 | + | } |
| 790 | + | |
| 791 | + | #[test] |
| 792 | + | fn can_append_text_to_format_p() { |
| 793 | + | let mut format = super::FormatP { |
| 794 | + | original: ptr::null_mut(), |
| 795 | + | buffer: ptr::null_mut(), |
| 796 | + | length: 0, |
| 797 | + | size: 0, |
| 798 | + | }; |
| 799 | + | |
| 800 | + | BeaconFormatAlloc(&mut format, 0xff); |
| 801 | + | |
| 802 | + | assert_ne!(format.original, ptr::null_mut()); |
| 803 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 804 | + | assert_eq!(format.length, 0); |
| 805 | + | assert_eq!(format.size, 0xff); |
| 806 | + | |
| 807 | + | let mut buffer: [c_char; 5] = [104, 101, 108, 108, 111]; |
| 808 | + | |
| 809 | + | BeaconFormatAppend(&mut format, buffer.as_mut_ptr(), 5); |
| 810 | + | |
| 811 | + | assert_eq!(format.length, 5); |
| 812 | + | |
| 813 | + | let string: &[u8] = |
| 814 | + | unsafe { slice::from_raw_parts(format.original as *const u8, format.length as usize) }; |
| 815 | + | |
| 816 | + | let result_string = String::from_utf8_lossy(string); |
| 817 | + | assert_eq!("hello", result_string); |
| 818 | + | } |
| 819 | + | |
| 820 | + | #[test] |
| 821 | + | fn can_printf_to_format_p() { |
| 822 | + | let mut format = super::FormatP { |
| 823 | + | original: ptr::null_mut(), |
| 824 | + | buffer: ptr::null_mut(), |
| 825 | + | length: 0, |
| 826 | + | size: 0, |
| 827 | + | }; |
| 828 | + | |
| 829 | + | BeaconFormatAlloc(&mut format, 0xff); |
| 830 | + | |
| 831 | + | assert_ne!(format.original, ptr::null_mut()); |
| 832 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 833 | + | assert_eq!(format.length, 0); |
| 834 | + | assert_eq!(format.size, 0xff); |
| 835 | + | |
| 836 | + | let fmt = CString::new("%.*f %.3s").unwrap(); |
| 837 | + | let fmt_slice = fmt.to_bytes_with_nul(); |
| 838 | + | |
| 839 | + | unsafe { |
| 840 | + | BeaconFormatPrintf( |
| 841 | + | &mut format, |
| 842 | + | fmt_slice.as_ptr() as *mut c_char, |
| 843 | + | 2, |
| 844 | + | 0.3333, |
| 845 | + | "foobar", |
| 846 | + | ) |
| 847 | + | }; |
| 848 | + | |
| 849 | + | assert_eq!(format.length, 9); |
| 850 | + | |
| 851 | + | let string: &[c_char] = |
| 852 | + | unsafe { slice::from_raw_parts(format.original, format.length as usize) }; |
| 853 | + | |
| 854 | + | let result_string = unsafe { CStr::from_ptr(string.as_ptr()) }; |
| 855 | + | |
| 856 | + | assert_eq!("0.33 foo", result_string.to_str().unwrap()); |
| 857 | + | } |
| 858 | + | |
| 859 | + | #[test] |
| 860 | + | fn can_turn_format_p_into_string() { |
| 861 | + | let mut format = super::FormatP { |
| 862 | + | original: ptr::null_mut(), |
| 863 | + | buffer: ptr::null_mut(), |
| 864 | + | length: 0, |
| 865 | + | size: 0, |
| 866 | + | }; |
| 867 | + | |
| 868 | + | BeaconFormatAlloc(&mut format, 0xff); |
| 869 | + | |
| 870 | + | assert_ne!(format.original, ptr::null_mut()); |
| 871 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 872 | + | assert_eq!(format.length, 0); |
| 873 | + | assert_eq!(format.size, 0xff); |
| 874 | + | |
| 875 | + | let mut buffer: [c_char; 5] = [104, 101, 108, 108, 111]; |
| 876 | + | |
| 877 | + | BeaconFormatAppend(&mut format, buffer.as_mut_ptr(), 5); |
| 878 | + | |
| 879 | + | assert_eq!(format.length, 5); |
| 880 | + | |
| 881 | + | let mut length = 0; |
| 882 | + | |
| 883 | + | let string_parts = BeaconFormatToString(&mut format, &mut length); |
| 884 | + | |
| 885 | + | let string: &[u8] = |
| 886 | + | unsafe { slice::from_raw_parts(string_parts as *const u8, length as usize) }; |
| 887 | + | |
| 888 | + | let result_string = String::from_utf8_lossy(string); |
| 889 | + | |
| 890 | + | assert_eq!("hello", result_string); |
| 891 | + | } |
| 892 | + | |
| 893 | + | #[test] |
| 894 | + | fn can_append_int_to_format_p() { |
| 895 | + | let mut format = super::FormatP { |
| 896 | + | original: ptr::null_mut(), |
| 897 | + | buffer: ptr::null_mut(), |
| 898 | + | length: 0, |
| 899 | + | size: 0, |
| 900 | + | }; |
| 901 | + | |
| 902 | + | BeaconFormatAlloc(&mut format, 0xff); |
| 903 | + | |
| 904 | + | assert_ne!(format.original, ptr::null_mut()); |
| 905 | + | assert_ne!(format.buffer, ptr::null_mut()); |
| 906 | + | assert_eq!(format.length, 0); |
| 907 | + | assert_eq!(format.size, 0xff); |
| 908 | + | |
| 909 | + | BeaconFormatInt(&mut format, 5); |
| 910 | + | |
| 911 | + | assert_eq!(format.length, 4); |
| 912 | + | |
| 913 | + | let result: &[u8] = |
| 914 | + | unsafe { slice::from_raw_parts(format.original as *const u8, format.length as usize) }; |
| 915 | + | |
| 916 | + | assert_eq!(result, &[0, 0, 0, 5]); |
| 917 | + | } |
| 918 | + | |
| 919 | + | #[test] |
| 920 | + | fn can_printf_to_beacon() { |
| 921 | + | let fmt = CString::new("%.*f %.3s").unwrap(); |
| 922 | + | let fmt_slice = fmt.to_bytes_with_nul(); |
| 923 | + | |
| 924 | + | unsafe { BeaconPrintf(0, fmt_slice.as_ptr() as *mut c_char, 2, 0.3333, "foobar") }; |
| 925 | + | |
| 926 | + | let result = unsafe { OUTPUT.get_from_current_offset() }; |
| 927 | + | |
| 928 | + | let result_string = unsafe { CStr::from_ptr(result.as_ptr()) }; |
| 929 | + | |
| 930 | + | assert_eq!(unsafe { OUTPUT.len() }, 9); |
| 931 | + | assert_eq!(&[48, 46, 51, 51, 32, 102, 111, 111, 0], result); |
| 932 | + | assert_eq!("0.33 foo", result_string.to_str().unwrap()); |
| 933 | + | |
| 934 | + | unsafe { reset_output() }; |
| 935 | + | } |
| 936 | + | |
| 937 | + | #[test] |
| 938 | + | fn can_append_beacon_output() { |
| 939 | + | let mut buffer: [c_char; 6] = [104, 101, 108, 108, 111, 0]; |
| 940 | + | |
| 941 | + | BeaconOutput(0, buffer.as_mut_ptr(), 6); |
| 942 | + | |
| 943 | + | let result = unsafe { OUTPUT.get_from_current_offset() }; |
| 944 | + | |
| 945 | + | let result_string = unsafe { CStr::from_ptr(result.as_ptr()) }; |
| 946 | + | |
| 947 | + | assert_eq!(unsafe { OUTPUT.len() }, 6); |
| 948 | + | assert_eq!(&[104, 101, 108, 108, 111, 0], result); |
| 949 | + | assert_eq!("hello", result_string.to_str().unwrap()); |
| 950 | + | |
| 951 | + | unsafe { reset_output() }; |
| 952 | + | } |
| 953 | + | |
| 954 | + | #[test] |
| 955 | + | fn can_return_beacon_output() { |
| 956 | + | let mut buffer: [c_char; 6] = [104, 101, 108, 108, 111, 0]; |
| 957 | + | BeaconOutput(0, buffer.as_mut_ptr(), 6); |
| 958 | + | |
| 959 | + | let fmt = CString::new("%.*f %.3s").unwrap(); |
| 960 | + | let fmt_slice = fmt.to_bytes_with_nul(); |
| 961 | + | |
| 962 | + | unsafe { BeaconPrintf(0, fmt_slice.as_ptr() as *mut c_char, 2, 0.3333, "foobar") }; |
| 963 | + | |
| 964 | + | let mut data = get_output(); |
| 965 | + | |
| 966 | + | assert_eq!("hello\n0.33 foo\n", data.flush()); |
| 967 | + | assert_eq!(15, data.len()); |
| 968 | + | } |
| 969 | + | |
| 970 | + | #[test] |
| 971 | + | fn can_swap_endianness() { |
| 972 | + | let src = 1025_u32.to_le(); |
| 973 | + | |
| 974 | + | // This test won't work on big endian machines. |
| 975 | + | // So I'll just test the code that actually matters. |
| 976 | + | assert_eq!(17039360, src.swap_bytes()); |
| 977 | + | } |
| 978 | + | |
| 979 | + | #[test] |
| 980 | + | fn can_convert_string_to_wide_string() { |
| 981 | + | let before = CString::new("hello world! ããã¼ã¯ã¼ã«ãï¼ã?!").unwrap(); |
| 982 | + | |
| 983 | + | let c_ptr: *mut c_char = before.into_raw(); |
| 984 | + | |
| 985 | + | let mut buffer = [0; 0xff]; |
| 986 | + | let buffer_length: DWORD = buffer.len() as DWORD; |
| 987 | + | |
| 988 | + | let success = crate::beacon::to_wide_char(c_ptr, buffer.as_mut_ptr(), buffer_length as i32); |
| 989 | + | |
| 990 | + | let buffer_slice: [u16; 0xff] = buffer.map(|b| b as u16); |
| 991 | + | let len = buffer.iter().take_while(|&&c| c != 0).count(); |
| 992 | + | |
| 993 | + | let result = String::from_utf16_lossy(&buffer_slice[..len]); |
| 994 | + | |
| 995 | + | assert_eq!("hello world! ããã¼ã¯ã¼ã«ãï¼ã?!", format!("{}", result)); |
| 996 | + | assert_eq!(success, 1); |
| 997 | + | } |
| 998 | + | |
| 999 | + | #[test] |
| 1000 | + | fn can_limit_characters_when_converting() { |
| 1001 | + | let before = CString::new("hello world! ããã¼ã¯ã¼ã«ãï¼ã?!").unwrap(); |
| 1002 | + | |
| 1003 | + | let c_ptr: *mut c_char = before.into_raw(); |
| 1004 | + | |
| 1005 | + | let mut buffer = [0; 0xff]; |
| 1006 | + | |
| 1007 | + | let success = crate::beacon::to_wide_char(c_ptr, buffer.as_mut_ptr(), 5 as i32); |
| 1008 | + | |
| 1009 | + | let buffer_slice: [u16; 0xff] = buffer.map(|b| b as u16); |
| 1010 | + | let len = buffer.iter().take_while(|&&c| c != 0).count(); |
| 1011 | + | |
| 1012 | + | let result = String::from_utf16_lossy(&buffer_slice[..len]); |
| 1013 | + | |
| 1014 | + | assert_eq!("hell", format!("{}", result)); |
| 1015 | + | assert_eq!(success, 1); |
| 1016 | + | } |
| 1017 | + | } |
| 1018 | + | |