Advertisement
badeip

Untitled

Jul 6th, 2025
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.87 KB | None | 0 0
  1.         commands_["write"] = CommandFunc([](needle::Needle *nlp, const std::vector<std::string>& args) -> bool
  2.         {
  3.             if (args.size() < 3) {
  4.                 spdlog::error("usage: write <address> [type] <value...>\n"
  5.                                 "  address: memory address\n"
  6.                                 "  type: data type (optional, defaults to uint8_t)\n"
  7.                                 "  value: hex bytes for uint8_t, or value for other types");
  8.                 return false;
  9.             }
  10.  
  11.             uintptr_t addr = evaluate_address(*nlp, args[1]);
  12.             size_t type_idx = 2;
  13.  
  14.             if (args.size() == 3) {
  15.                 type_idx = SIZE_MAX;
  16.             } else {
  17.                 for (const auto& known_type : {
  18.                     "uint8_t", "uint16_t", "uint32_t", "uint64_t", "int8_t", "int16_t", "int32_t", "int64_t", "float", "double", "string"
  19.                 }) {
  20.                     if (args[2] == known_type) {
  21.                         type_idx = 2;
  22.                         break;
  23.                     }
  24.                 }
  25.                 if (type_idx == 2 && args.size() > 4) {
  26.                     type_idx = SIZE_MAX;
  27.                 }
  28.             }
  29.  
  30.             const auto type = (type_idx == SIZE_MAX) ? "uint8_t" : args[type_idx];
  31.  
  32.             if (type == "uint8_t") {
  33.                 std::vector<uint8_t> bytes;
  34.                 size_t start_idx = (type_idx == SIZE_MAX) ? 2 : type_idx + 1;
  35.  
  36.                 for (size_t i = start_idx; i < args.size(); ++i) {
  37.                     try {
  38.                         unsigned long byte_val = std::stoul(args[i], nullptr, 16);
  39.                         if (byte_val > 0xFF) {
  40.                             spdlog::error("Byte value out of range: {}", args[i]);
  41.                             return false;
  42.                         }
  43.                         bytes.push_back(static_cast<uint8_t>(byte_val));
  44.                     } catch (...) {
  45.                         spdlog::error("Invalid hex byte: {}", args[i]);
  46.                         return false;
  47.                     }
  48.                 }
  49.  
  50.                 spdlog::info("write: {:#x} = (uint8_t[{}]) [{}]", addr, bytes.size(),
  51.                     fmt::format("{:#04x}", fmt::join(bytes, " ")));
  52.  
  53.                 if (!compat::write_mem(nlp->proc.handle, addr, bytes)) {
  54.                     spdlog::error("WriteProcessMemory({:#x}), failed: {}", addr, GetLastError());
  55.                     return false;
  56.                 }
  57.                 spdlog::info("Write succeeded");
  58.                 return true;
  59.             }
  60.  
  61.             std::string val = args[type_idx + 1];
  62.             if (args.size() > type_idx + 2) {
  63.                 val.clear();
  64.                 for (size_t i = type_idx + 1; i < args.size(); ++i) {
  65.                     if (i > type_idx + 1) val += " ";
  66.                     val += args[i];
  67.                 }
  68.             }
  69.  
  70.             mem::ValueVariant value;
  71.             try {
  72.                 value = mem::parse_argument(val, type);
  73.             } catch (const std::exception& e) {
  74.                 spdlog::error("Error parsing value: {}", e.what());
  75.                 return false;
  76.             }
  77.  
  78.             std::visit([&](const auto& tp) {
  79.                 if constexpr (std::is_same_v<std::decay_t<decltype(tp)>, std::string>) {
  80.                     spdlog::info("write: {:#014x} = (std::string) \"{}\"", addr, tp);
  81.                 } else if constexpr (std::is_integral_v<std::decay_t<decltype(tp)>>) {
  82.                     spdlog::info("write: {:#014x} = ({}) {:#x}", addr, type, tp);
  83.                 } else {
  84.                     spdlog::info("write: {:#014x} = ({}) {}", addr, type, tp);
  85.                 }
  86.             }, value);
  87.  
  88.             if (!mem::write_value(nlp->proc.handle, value, addr)) {
  89.                 spdlog::error("Write failed: {}", GetLastError());
  90.                 return false;
  91.             }
  92.             spdlog::info("Write succeeded");
  93.             return true;
  94.         });        commands_["write"] = CommandFunc([](needle::Needle *nlp, const std::vector<std::string>& args) -> bool
  95.         {
  96.             if (args.size() < 3) {
  97.                 spdlog::error("usage: write <address> [type] <value...>\n"
  98.                                 "  address: memory address\n"
  99.                                 "  type: data type (optional, defaults to uint8_t)\n"
  100.                                 "  value: hex bytes for uint8_t, or value for other types");
  101.                 return false;
  102.             }
  103.  
  104.             uintptr_t addr = evaluate_address(*nlp, args[1]);
  105.             size_t type_idx = 2;
  106.  
  107.             if (args.size() == 3) {
  108.                 type_idx = SIZE_MAX;
  109.             } else {
  110.                 for (const auto& known_type : {
  111.                     "uint8_t", "uint16_t", "uint32_t", "uint64_t", "int8_t", "int16_t", "int32_t", "int64_t", "float", "double", "string"
  112.                 }) {
  113.                     if (args[2] == known_type) {
  114.                         type_idx = 2;
  115.                         break;
  116.                     }
  117.                 }
  118.                 if (type_idx == 2 && args.size() > 4) {
  119.                     type_idx = SIZE_MAX;
  120.                 }
  121.             }
  122.  
  123.             const auto type = (type_idx == SIZE_MAX) ? "uint8_t" : args[type_idx];
  124.  
  125.             if (type == "uint8_t") {
  126.                 std::vector<uint8_t> bytes;
  127.                 size_t start_idx = (type_idx == SIZE_MAX) ? 2 : type_idx + 1;
  128.  
  129.                 for (size_t i = start_idx; i < args.size(); ++i) {
  130.                     try {
  131.                         unsigned long byte_val = std::stoul(args[i], nullptr, 16);
  132.                         if (byte_val > 0xFF) {
  133.                             spdlog::error("Byte value out of range: {}", args[i]);
  134.                             return false;
  135.                         }
  136.                         bytes.push_back(static_cast<uint8_t>(byte_val));
  137.                     } catch (...) {
  138.                         spdlog::error("Invalid hex byte: {}", args[i]);
  139.                         return false;
  140.                     }
  141.                 }
  142.  
  143.                 spdlog::info("write: {:#x} = (uint8_t[{}]) [{}]", addr, bytes.size(),
  144.                     fmt::format("{:#04x}", fmt::join(bytes, " ")));
  145.  
  146.                 if (!compat::write_mem(nlp->proc.handle, addr, bytes)) {
  147.                     spdlog::error("WriteProcessMemory({:#x}), failed: {}", addr, GetLastError());
  148.                     return false;
  149.                 }
  150.                 spdlog::info("Write succeeded");
  151.                 return true;
  152.             }
  153.  
  154.             std::string val = args[type_idx + 1];
  155.             if (args.size() > type_idx + 2) {
  156.                 val.clear();
  157.                 for (size_t i = type_idx + 1; i < args.size(); ++i) {
  158.                     if (i > type_idx + 1) val += " ";
  159.                     val += args[i];
  160.                 }
  161.             }
  162.  
  163.             mem::ValueVariant value;
  164.             try {
  165.                 value = mem::parse_argument(val, type);
  166.             } catch (const std::exception& e) {
  167.                 spdlog::error("Error parsing value: {}", e.what());
  168.                 return false;
  169.             }
  170.  
  171.             std::visit([&](const auto& tp) {
  172.                 if constexpr (std::is_same_v<std::decay_t<decltype(tp)>, std::string>) {
  173.                     spdlog::info("write: {:#014x} = (std::string) \"{}\"", addr, tp);
  174.                 } else if constexpr (std::is_integral_v<std::decay_t<decltype(tp)>>) {
  175.                     spdlog::info("write: {:#014x} = ({}) {:#x}", addr, type, tp);
  176.                 } else {
  177.                     spdlog::info("write: {:#014x} = ({}) {}", addr, type, tp);
  178.                 }
  179.             }, value);
  180.  
  181.             if (!mem::write_value(nlp->proc.handle, value, addr)) {
  182.                 spdlog::error("Write failed: {}", GetLastError());
  183.                 return false;
  184.             }
  185.             spdlog::info("Write succeeded");
  186.             return true;
  187.         });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement