execshellcode.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Name: execshellcode
  3. * Version: 0.1.1
  4. * Description: A simple utility to test shellcode
  5. * This program runs shellcode using the mmap function.
  6. * In modern systems with varying protections
  7. * it may not work properly.
  8. * Dependencies:
  9. */
  10. #include "bzbio.h"
  11. #include <string.h>
  12. #include <sys/mman.h>
  13. #include <iostream>
  14. #include <cstdlib>
  15. #include <sstream>
  16. using namespace bzbio;
  17. constexpr int VERSION_MAJOR = 0;
  18. constexpr int VERSION_MINOR = 1;
  19. constexpr int VERSION_PATCH = 1;
  20. unsigned char hex_to_char(std::string str) {
  21. if(str.size() != 2) {
  22. println("ERROR Wrong format in hex_to_char()");
  23. exit(1);
  24. }
  25. char high_nibble = 0;
  26. char low_nibble = 0;
  27. unsigned char h = str[0];
  28. unsigned char l = str[1];
  29. if(h < '0' && h > '9' && h < 'A' && h > 'F' && h < 'a' && h > 'f') {
  30. println("ERROR Wrong format in hex_to_char()");
  31. exit(1);
  32. }
  33. if(l < '0' && l > '9' && l < 'A' && l > 'F' && l < 'a' && l > 'f') {
  34. println("ERROR Wrong format in hex_to_char()");
  35. exit(1);
  36. }
  37. if(h >= 'A' && h <= 'F')
  38. h += 32;
  39. if(l >= 'A' && l <= 'F')
  40. l += 32;
  41. if(h >= '0' && h <= '9') {
  42. high_nibble = (h - '0') << 4;
  43. } else {
  44. high_nibble = (h - 'a' + 10) << 4;
  45. }
  46. if(l >= '0' && l <= '9') {
  47. low_nibble = l - '0';
  48. } else {
  49. low_nibble = l - 'a' + 10;
  50. }
  51. return high_nibble | low_nibble;
  52. }
  53. void print_help() {
  54. println("\nUsage: execshellcode [ --backslash | --nobackslash ] <shellcode>");
  55. println("\t--backslash\tRead shellcode with backslashes in it (\\\\YZ format)");
  56. println("\t--nobackslash\tRead shellcode with no backslashes in it (YX format)\n");
  57. println("\tIf you try to execute normally formatted shellcode e.g. \\x03");
  58. println("\tyou don't need to use --backslash. If you enter shellcode");
  59. println("\tforcing a backslash in your terminal like \\\\x03, use --backslash.\n");
  60. exit(0);
  61. }
  62. void print_version_exit() {
  63. print("execshellcode v");
  64. println(VERSION_MAJOR, "." ,VERSION_MINOR , ".", VERSION_PATCH);
  65. println("This program is part of the Balzebub project.");
  66. exit(0);
  67. }
  68. int main(int argc, char const *argv[]) {
  69. if(argc < 2) {
  70. print_help();
  71. }
  72. std::string shellcode = argv[1];
  73. unsigned char* buffer = nullptr;
  74. size_t buffer_size = 0;
  75. size_t byte_size = 3;
  76. if(shellcode == "--help" || shellcode == "-h") {
  77. print_help();
  78. } else if(shellcode == "--version" || shellcode == "-v") {
  79. print_version_exit();
  80. }
  81. if(argc == 3) {
  82. if(shellcode == "--backslash") {
  83. shellcode = argv[2];
  84. byte_size = 4;
  85. } else if(shellcode == "--nobackslash") {
  86. shellcode = argv[2];
  87. byte_size = 2;
  88. }
  89. }
  90. if(shellcode.size() % byte_size != 0) {
  91. println("ERROR Wrong format\nUse \\xYZ format or"
  92. "\\\\xYZ format with --backslash option or"
  93. "YZ with --nobackslash option.");
  94. exit(-3);
  95. }
  96. buffer = new unsigned char[shellcode.size() / byte_size];
  97. buffer_size = shellcode.size() / byte_size;
  98. if(!buffer) {
  99. println("ERROR Cannot allocate buffer...");
  100. exit(-4);
  101. }
  102. // Convert the shellcode string to char
  103. for (size_t i = 0, l = 0; i < shellcode.size(); i += byte_size, l++) {
  104. if(byte_size == 3) {
  105. if(shellcode[i] == 'x') {
  106. std::string str = shellcode.substr(i + 1, 2);
  107. buffer[l] = hex_to_char(str);
  108. }
  109. } else if(byte_size == 4) {
  110. if(shellcode[i] == '\\' && shellcode[i + 1] == 'x') {
  111. std::string str = shellcode.substr(i + 2, 2);
  112. buffer[l] = hex_to_char(str);
  113. }
  114. } else if(byte_size == 2) {
  115. std::string str = shellcode.substr(i, 2);
  116. buffer[l] = hex_to_char(str);
  117. }
  118. }
  119. // Create an anonymous executable mapped buffer and write the shellcode in it
  120. void* executable = mmap(nullptr, buffer_size,
  121. PROT_WRITE | PROT_EXEC | PROT_READ,
  122. MAP_ANONYMOUS | MAP_PRIVATE,
  123. -1, 0);
  124. if(executable == MAP_FAILED) {
  125. perror("mmap");
  126. exit(-1);
  127. }
  128. memcpy(executable, (void*) buffer, buffer_size);
  129. println("Executing", buffer_size, "bytes shellcode using mmap...\n");
  130. ((int(*)()) executable)();
  131. munmap(executable, buffer_size);
  132. return 0;
  133. }