Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- commands_["write"] = CommandFunc([](needle::Needle *nlp, const std::vector<std::string>& args) -> bool
- {
- if (args.size() < 3) {
- spdlog::error("usage: write <address> [type] <value...>\n"
- " address: memory address\n"
- " type: data type (optional, defaults to uint8_t)\n"
- " value: hex bytes for uint8_t, or value for other types");
- return false;
- }
- uintptr_t addr = evaluate_address(*nlp, args[1]);
- size_t type_idx = 2;
- if (args.size() == 3) {
- type_idx = SIZE_MAX;
- } else {
- for (const auto& known_type : {
- "uint8_t", "uint16_t", "uint32_t", "uint64_t", "int8_t", "int16_t", "int32_t", "int64_t", "float", "double", "string"
- }) {
- if (args[2] == known_type) {
- type_idx = 2;
- break;
- }
- }
- if (type_idx == 2 && args.size() > 4) {
- type_idx = SIZE_MAX;
- }
- }
- const auto type = (type_idx == SIZE_MAX) ? "uint8_t" : args[type_idx];
- if (type == "uint8_t") {
- std::vector<uint8_t> bytes;
- size_t start_idx = (type_idx == SIZE_MAX) ? 2 : type_idx + 1;
- for (size_t i = start_idx; i < args.size(); ++i) {
- try {
- unsigned long byte_val = std::stoul(args[i], nullptr, 16);
- if (byte_val > 0xFF) {
- spdlog::error("Byte value out of range: {}", args[i]);
- return false;
- }
- bytes.push_back(static_cast<uint8_t>(byte_val));
- } catch (...) {
- spdlog::error("Invalid hex byte: {}", args[i]);
- return false;
- }
- }
- spdlog::info("write: {:#x} = (uint8_t[{}]) [{}]", addr, bytes.size(),
- fmt::format("{:#04x}", fmt::join(bytes, " ")));
- if (!compat::write_mem(nlp->proc.handle, addr, bytes)) {
- spdlog::error("WriteProcessMemory({:#x}), failed: {}", addr, GetLastError());
- return false;
- }
- spdlog::info("Write succeeded");
- return true;
- }
- std::string val = args[type_idx + 1];
- if (args.size() > type_idx + 2) {
- val.clear();
- for (size_t i = type_idx + 1; i < args.size(); ++i) {
- if (i > type_idx + 1) val += " ";
- val += args[i];
- }
- }
- mem::ValueVariant value;
- try {
- value = mem::parse_argument(val, type);
- } catch (const std::exception& e) {
- spdlog::error("Error parsing value: {}", e.what());
- return false;
- }
- std::visit([&](const auto& tp) {
- if constexpr (std::is_same_v<std::decay_t<decltype(tp)>, std::string>) {
- spdlog::info("write: {:#014x} = (std::string) \"{}\"", addr, tp);
- } else if constexpr (std::is_integral_v<std::decay_t<decltype(tp)>>) {
- spdlog::info("write: {:#014x} = ({}) {:#x}", addr, type, tp);
- } else {
- spdlog::info("write: {:#014x} = ({}) {}", addr, type, tp);
- }
- }, value);
- if (!mem::write_value(nlp->proc.handle, value, addr)) {
- spdlog::error("Write failed: {}", GetLastError());
- return false;
- }
- spdlog::info("Write succeeded");
- return true;
- }); commands_["write"] = CommandFunc([](needle::Needle *nlp, const std::vector<std::string>& args) -> bool
- {
- if (args.size() < 3) {
- spdlog::error("usage: write <address> [type] <value...>\n"
- " address: memory address\n"
- " type: data type (optional, defaults to uint8_t)\n"
- " value: hex bytes for uint8_t, or value for other types");
- return false;
- }
- uintptr_t addr = evaluate_address(*nlp, args[1]);
- size_t type_idx = 2;
- if (args.size() == 3) {
- type_idx = SIZE_MAX;
- } else {
- for (const auto& known_type : {
- "uint8_t", "uint16_t", "uint32_t", "uint64_t", "int8_t", "int16_t", "int32_t", "int64_t", "float", "double", "string"
- }) {
- if (args[2] == known_type) {
- type_idx = 2;
- break;
- }
- }
- if (type_idx == 2 && args.size() > 4) {
- type_idx = SIZE_MAX;
- }
- }
- const auto type = (type_idx == SIZE_MAX) ? "uint8_t" : args[type_idx];
- if (type == "uint8_t") {
- std::vector<uint8_t> bytes;
- size_t start_idx = (type_idx == SIZE_MAX) ? 2 : type_idx + 1;
- for (size_t i = start_idx; i < args.size(); ++i) {
- try {
- unsigned long byte_val = std::stoul(args[i], nullptr, 16);
- if (byte_val > 0xFF) {
- spdlog::error("Byte value out of range: {}", args[i]);
- return false;
- }
- bytes.push_back(static_cast<uint8_t>(byte_val));
- } catch (...) {
- spdlog::error("Invalid hex byte: {}", args[i]);
- return false;
- }
- }
- spdlog::info("write: {:#x} = (uint8_t[{}]) [{}]", addr, bytes.size(),
- fmt::format("{:#04x}", fmt::join(bytes, " ")));
- if (!compat::write_mem(nlp->proc.handle, addr, bytes)) {
- spdlog::error("WriteProcessMemory({:#x}), failed: {}", addr, GetLastError());
- return false;
- }
- spdlog::info("Write succeeded");
- return true;
- }
- std::string val = args[type_idx + 1];
- if (args.size() > type_idx + 2) {
- val.clear();
- for (size_t i = type_idx + 1; i < args.size(); ++i) {
- if (i > type_idx + 1) val += " ";
- val += args[i];
- }
- }
- mem::ValueVariant value;
- try {
- value = mem::parse_argument(val, type);
- } catch (const std::exception& e) {
- spdlog::error("Error parsing value: {}", e.what());
- return false;
- }
- std::visit([&](const auto& tp) {
- if constexpr (std::is_same_v<std::decay_t<decltype(tp)>, std::string>) {
- spdlog::info("write: {:#014x} = (std::string) \"{}\"", addr, tp);
- } else if constexpr (std::is_integral_v<std::decay_t<decltype(tp)>>) {
- spdlog::info("write: {:#014x} = ({}) {:#x}", addr, type, tp);
- } else {
- spdlog::info("write: {:#014x} = ({}) {}", addr, type, tp);
- }
- }, value);
- if (!mem::write_value(nlp->proc.handle, value, addr)) {
- spdlog::error("Write failed: {}", GetLastError());
- return false;
- }
- spdlog::info("Write succeeded");
- return true;
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement