fix(server): stop two remote-input paths from killing the server
Both bugs let a remote peer terminate the server process: the first needs no credentials at all, the second needs only a malicious client. 1) cpp-httplib 0.14.3 -- unauthenticated DoS via Content-Length Content-Length was parsed with strtoull, which accepts a leading sign, so "-1" wrapped around to ULLONG_MAX. read_content then treated that as an oversized body and called skip_content_with_length(strm, ULLONG_MAX), a loop that only exits after consuming 18 EB -- a client trickling bytes never trips the read timeout, so the worker thread is stuck forever. With ThreadPool(32) that is 32 connections to wedge the whole server. Body parsing runs before the MCP token check, so no token is required. Backported from upstream 0.51.0/0.64.0 as parse_content_length_strict(): only plain decimal digits are accepted and overflow is rejected. Upgrading the library instead is not possible -- 0.25.0+ requires _WIN32_WINNT >= 0x0A00 (Win10) while this project targets 0x0602 (Win8), and the last Win8-compatible release (0.20.0) predates the fix. Verified: negative, leading-plus, overflow and trailing-garbage values now all return 400; 40 concurrent "-1" requests are rejected in under 50 ms with the thread pool still healthy; plain and chunked requests are unaffected. 2) HideScreenSpyDlg::JPG_BMP -- malformed JPEG terminated the process The decoder was set up with libjpeg's default error_exit, which calls exit(EXIT_FAILURE). Since the input is a JPEG stream sent by the remote client, one structurally invalid frame killed the whole server. Replaced with a setjmp/longjmp handler that turns fatal errors into "return false", matching this function's existing failure paths. Warnings still go through emit_message, so truncated-but-recoverable images behave exactly as before. Verified against 36 malformed samples: 19 used to terminate the process, none do now (the same 19 return false), and valid JPEGs decode identically. 3) Drop the dead file_server.h An older HTTP file-serving implementation, superseded by WebService.cpp and referenced by nothing: no include anywhere, no instantiation, no entry in the vcxproj or any build script. Removing it keeps the real HTTP attack surface -- httplib's /mcp plus WebService -- unambiguous for anyone auditing this code.
This commit is contained in:
1 parent
8807321f1d
commit
4f9bfb0fdc
3 files changed
+88
-100
No files matched your search
@@ -8,6 +8,7 @@
|
||||
#include "HideScreenSpyDlg.h"
|
||||
#include "2015RemoteDlg.h"
|
||||
#include <windows.h>
|
||||
#include <csetjmp>
|
||||
|
||||
#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<JpegErrorMgr*>(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);
|
||||
// 读取头部信息
|
||||
|
||||
@@ -1,95 +0,0 @@
|
||||
#include <Windows.h>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
|
||||
#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_;
|
||||
};
|
||||
@@ -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<uint64_t>(c - '0');
|
||||
if (v > ((std::numeric_limits<uint64_t>::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);
|
||||
|
||||
Reference in new issue
Block a user