elfswitch.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Name: elfswitch
  3. * Version: 0.1.0
  4. * Description: Very simple program to make
  5. * an ELF file momentarily unexecutable by
  6. * modifying the entry address.
  7. * (Apply again to nullify the effect)
  8. * This program can be useful when
  9. * handling infected executables.
  10. * Dependencies: bzbutil, bzbfile
  11. */
  12. #include "bzbfile.h"
  13. #include "bzbio.h"
  14. #include "bzbstring.h"
  15. using namespace bzbio;
  16. using namespace bzbstring;
  17. constexpr int VERSION_MAJOR = 0;
  18. constexpr int VERSION_MINOR = 1;
  19. constexpr int VERSION_PATCH = 0;
  20. const char* PROGNAME = "elfswitch";
  21. void print_help_exit(int err = 0) {
  22. println("Usage: ", PROGNAME, " [ -h | --help ] | <filename>");
  23. println("Negate the entry address of the specified ELF to",
  24. " make it unexecutable");
  25. println("-h | --help\tShow this help");
  26. exit(err);
  27. }
  28. void print_version_exit() {
  29. print(PROGNAME, " v");
  30. println(VERSION_MAJOR, ".", VERSION_MINOR, ".", VERSION_PATCH);
  31. println("This program is part of the Balzebub project.");
  32. exit(0);
  33. }
  34. int main(int argc, char const *argv[]) {
  35. if(argc != 2) {
  36. print_help_exit(1);
  37. }
  38. std::string arg(argv[1]);
  39. if(arg == "-v" || arg == "--version") {
  40. print_version_exit();
  41. }
  42. if(arg == "-h" || arg == "--help") {
  43. print_help_exit();
  44. }
  45. std::string filename = arg;
  46. bool is_32bit;
  47. try {
  48. is_32bit = bzbfile::get_elf_class(filename) == bzbfile::ELF_CLASS_32;
  49. } catch(...) {
  50. exit_on_error("ERROR Unable to read file", 2);
  51. }
  52. std::vector<char> data;
  53. try {
  54. if(is_32bit) {
  55. data = bzbfile::read_bin(filename, 4, 24);
  56. } else {
  57. data = bzbfile::read_bin(filename, 8, 24);
  58. }
  59. } catch(...) {
  60. exit_on_error("ERROR Unable to read file", 2);
  61. }
  62. if(data.size() != 8 && data.size() != 4) {
  63. exit_on_error("ERROR The file is corrupted", 2);
  64. }
  65. uint64_t address = 0x0;
  66. readint(data, 0, &address);
  67. println("Old entry address: 0x", std::hex, address, std::dec);
  68. println("New entry address: 0x", std::hex, ~address, std::dec);
  69. for(size_t i = 0; i < data.size(); ++i) {
  70. uint8_t c = ~(*((uint8_t*) &data[i]));
  71. data[i] = *((char*) &c);
  72. }
  73. try {
  74. bzbfile::write_bin(filename, 24, data);
  75. } catch(...) {
  76. exit_on_error("ERROR Unable to write to file", 3);
  77. }
  78. return 0;
  79. }