diff --git a/server/2015Remote/HideScreenSpyDlg.cpp b/server/2015Remote/HideScreenSpyDlg.cpp index f76ef86..c05d07f 100644 --- a/server/2015Remote/HideScreenSpyDlg.cpp +++ b/server/2015Remote/HideScreenSpyDlg.cpp @@ -8,6 +8,7 @@ #include "HideScreenSpyDlg.h" #include "2015RemoteDlg.h" #include +#include #ifdef _DEBUG #define new DEBUG_NEW @@ -908,15 +909,59 @@ void CHideScreenSpyDlg::OnTimer(UINT_PTR nIDEvent) __super::OnTimer(nIDEvent); } +namespace { + +// ===== JPEG 解码的致命错误处理 ===== +// +// libjpeg 默认的 error_exit 会调用 exit(EXIT_FAILURE)。本函数的输入是远程客户端发来的 +// JPEG 字节流,也就是说一个结构非法的包就能让整个服务端进程直接退出——这在 MCP/客户端 +// 都可能不可信的场景下是不可接受的。这里改用 setjmp/longjmp 把致命错误转成「返回 false」, +// 与本函数既有的其它失败路径行为一致。 +// +// 注意:libjpeg 的 warning(例如「数据截断」)走 emit_message,不经过 error_exit, +// 因此原本能容错的轻微损坏图像行为不变,只有真正的致命错误才会走到这里。 +struct JpegErrorMgr { + jpeg_error_mgr pub; // 必须是第一个成员:libjpeg 只持有 jpeg_error_mgr* + jmp_buf jb; +}; + +void JpegErrorExit(j_common_ptr cinfo) +{ + longjmp(reinterpret_cast(cinfo->err)->jb, 1); +} + +void JpegSilentMessage(j_common_ptr /*cinfo*/) +{ + // 默认实现往 stderr 打日志,GUI 进程里看不见;畸形包还可能被反复发送, + // 保持静默以免刷屏。 +} + +} // namespace + bool CHideScreenSpyDlg::JPG_BMP(int cbit, void* input, int inlen, void* output) { - struct jpeg_decompress_struct jds; - struct jpeg_error_mgr jem; + // 值初始化:longjmp 回来要 destroy 这个对象,而 jpeg_destroy 会读 mem 等指针。 + // 不清零的话这些是栈上的垃圾值,destroy 会踩野指针。 + struct jpeg_decompress_struct jds = {}; + JpegErrorMgr jem; + + jds.err = jpeg_std_error(&jem.pub); + jem.pub.error_exit = JpegErrorExit; + jem.pub.output_message = JpegSilentMessage; + + // created 在 setjmp 之后写入、longjmp 之后读取,必须 volatile,否则回到这里时 + // 可能读到未定义的值。create 失败时 mem 状态不确定,此时不 destroy 更安全。 + volatile bool created = false; + if (setjmp(jem.jb) != 0) { + if (created) { + jpeg_destroy_decompress(&jds); + } + return false; + } - // 设置错误处理 - jds.err = jpeg_std_error(&jem); // 创建解压结构 jpeg_create_decompress(&jds); + created = true; // 设置读取(输入)位置 jpeg_mem_src(&jds, (byte*)input, inlen); // 读取头部信息 diff --git a/server/2015Remote/file_server.h b/server/2015Remote/file_server.h deleted file mode 100644 index c9c90a4..0000000 --- a/server/2015Remote/file_server.h +++ /dev/null @@ -1,95 +0,0 @@ -#include -#include -#include -#include -#include - -#undef min -#undef max -#include "httplib.h" -#ifndef max -#define max(a,b) (((a) > (b)) ? (a) : (b)) -#endif -#ifndef min -#define min(a,b) (((a) < (b)) ? (a) : (b)) -#endif - -#pragma comment(lib, "ws2_32.lib") - -namespace fs = std::filesystem; - -class FileDownloadServer -{ -public: - FileDownloadServer(int port = 8080) : port_(port) - { - char exe_path[MAX_PATH]; - GetModuleFileNameA(NULL, exe_path, MAX_PATH); - root_dir_ = fs::path(exe_path).parent_path() / "Payloads"; - fs::create_directories(root_dir_); - } - - bool Start() - { - server_.Get("/payloads/(.*)", [this](const httplib::Request& req, httplib::Response& res) { - std::string filename = req.matches[1]; - - if (filename.empty() || filename.find("..") != std::string::npos) { - res.status = 403; - return; - } - - fs::path filepath = root_dir_ / filename; - - if (!fs::exists(filepath) || !fs::is_regular_file(filepath)) { - res.status = 404; - return; - } - - auto filesize = fs::file_size(filepath); - std::string path_str = filepath.string(); - - res.set_header("Content-Disposition", - "attachment; filename=\"" + filename + "\""); - - res.set_content_provider(filesize, "application/octet-stream", - [path_str](size_t offset, size_t length, httplib::DataSink& sink) { - std::ifstream file(path_str, std::ios::binary); - if (!file) return false; - file.seekg(offset); - - char buffer[65536]; - size_t remaining = length; - while (remaining > 0 && file) { - size_t to_read = (std::min)(remaining, sizeof(buffer)); - file.read(buffer, to_read); - size_t n = (size_t)file.gcount(); - if (n == 0) break; - sink.write(buffer, n); - remaining -= n; - } - return true; - } - ); - }); - - server_thread_ = std::thread([this]() { - server_.listen("0.0.0.0", port_); - }); - - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - return server_.is_running(); - } - - void Stop() - { - server_.stop(); - if (server_thread_.joinable()) server_thread_.join(); - } - -private: - httplib::Server server_; - fs::path root_dir_; - int port_; - std::thread server_thread_; -}; diff --git a/server/2015Remote/httplib.h b/server/2015Remote/httplib.h index 851ae4c..0373bf0 100644 --- a/server/2015Remote/httplib.h +++ b/server/2015Remote/httplib.h @@ -4303,6 +4303,37 @@ inline bool read_content_with_length(Stream &strm, uint64_t len, return true; } +// [YAMA PATCH] Strict Content-Length parser. +// +// Do NOT use strtoull here: it accepts a leading sign, so "-1" wraps around to +// ULLONG_MAX, and it does not report overflow. read_content then treats that as +// an oversized body and calls skip_content_with_length(strm, ULLONG_MAX), which +// loops until it has consumed 18 EB -- a client that keeps trickling bytes never +// trips the read timeout, so the worker thread is stuck forever. With +// ThreadPool(32) that is an unauthenticated DoS: 32 such connections wedge the +// whole server. Ported from cpp-httplib 0.51.0/0.64.0 (is_invalid_content_length). +// Only pure decimal digits are accepted, and the value must fit in uint64_t. +inline bool parse_content_length_strict(const std::string &s, uint64_t &out) +{ + // ULLONG_MAX has 20 digits; anything longer cannot be a valid length. + if (s.empty() || s.size() > 20) { + return false; + } + uint64_t v = 0; + for (char c : s) { + if (c < '0' || c > '9') { + return false; + } + auto d = static_cast(c - '0'); + if (v > ((std::numeric_limits::max)() - d) / 10) { + return false; // overflow + } + v = v * 10 + d; + } + out = v; + return true; +} + inline void skip_content_with_length(Stream &strm, uint64_t len) { char buf[CPPHTTPLIB_RECV_BUFSIZ]; @@ -4484,7 +4515,14 @@ bool read_content(Stream &strm, T &x, size_t payload_max_length, int &status, } else if (!has_header(x.headers, "Content-Length")) { ret = read_content_without_length(strm, out); } else { - auto len = get_header_value_u64(x.headers, "Content-Length", 0, 0); + // [YAMA PATCH] Reject malformed Content-Length before it reaches + // skip_content_with_length. See parse_content_length_strict above. + uint64_t len = 0; + if (!parse_content_length_strict( + x.get_header_value("Content-Length"), len)) { + status = StatusCode::BadRequest_400; + return false; + } if (len > payload_max_length) { exceed_payload_max_length = true; skip_content_with_length(strm, len);