main.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. use hex;
  2. use rand;
  3. use std::fs::File;
  4. use std::io::{Write, Read};
  5. use std::io::{Seek, SeekFrom};
  6. use std::env;
  7. mod encoder;
  8. mod reorder;
  9. mod cpu_data;
  10. use crate::encoder::Encoder;
  11. use crate::cpu_data::Arch as EncArch;
  12. fn usage(args: &Vec<String>) {
  13. println!("Usage: {} -i shellcode.bin -o shellcode.out.bin", args[0]);
  14. std::process::exit(1);
  15. }
  16. fn main() {
  17. let number_of_loops = 1;
  18. // Parsing the Arguments
  19. let args: Vec<String> = env::args().collect();
  20. // variable if code should be reordered
  21. let mut reorder = false;
  22. if args.contains(&"--reorder".to_string()) {
  23. reorder = true;
  24. }
  25. if args.len() < 5 {
  26. usage(&args);
  27. }
  28. if args[1] != "-i" || args[3] != "-o" {
  29. usage(&args);
  30. }
  31. // Get the Input/Output File-Paths
  32. let input_file = &args[2];
  33. let output_file = &args[4];
  34. let mut shellcode = match File::open(input_file) {
  35. Ok(v) => v,
  36. Err(_) => {
  37. println!("[-] Input File not found, exiting");
  38. std::process::exit(1);
  39. }
  40. };
  41. // Obtain the Size of the input
  42. let size = shellcode.seek(SeekFrom::End(0)).unwrap();
  43. println!("[+] Reading 0x{:x} Bytes from {}", size, input_file);
  44. shellcode.seek(SeekFrom::Start(0)).unwrap();
  45. // Input Buffer for the shellcode
  46. let mut payload = vec![0; size as usize];
  47. shellcode.read(&mut payload).unwrap();
  48. let prog = reorder::AsmProgram::new(&payload);
  49. if reorder {
  50. payload = prog.reorder();
  51. }
  52. let enc = Encoder::new(EncArch::X64, payload.clone());
  53. enc.encode();
  54. /*
  55. // The Encoding Phase
  56. for i in 0..number_of_loops {
  57. println!("[*] Encoding Round {}", i);
  58. // Generate random key
  59. let key: u8 = rand::random();
  60. // encode the payload
  61. let mut encoded_payload = encoder::encode(payload.clone(), key);
  62. //println!("[*] Generating Stub...");
  63. let mut stub = encoder::get_decoder_stub(key, encoded_payload.len());
  64. // Append the encoded_payload to the stub
  65. stub.append(&mut encoded_payload);
  66. // Set the [stub+payload] as the new raw payload
  67. payload = stub.clone();
  68. }
  69. //Write Payload to File:
  70. let mut file = match File::create(output_file) {
  71. Ok(v) => v,
  72. Err(_) => {
  73. println!("[-] Cannot open Output File, outputting hex encoded payload:");
  74. println!("{}", hex::encode(payload));
  75. std::process::exit(1);
  76. }
  77. };
  78. file.write(&payload).unwrap();
  79. println!("[+] Done, Generated encoded payload @ {}", output_file);
  80. */
  81. }