Skip to content

Commit a13910a

Browse files
committed
fixing named pipe server detecting closed clients
1 parent 9ab8287 commit a13910a

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

Payload_Type/apollo/CHANGELOG.MD

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [v2.3.5] - 2025-03-03
8+
9+
### Changed
10+
11+
- Fixed a bug in Apollo's named pipe server that would break on writes for immediate disconnects from clients
12+
713
## [v2.3.4] - 2025-03-03
814

915
### Changed

Payload_Type/apollo/apollo/agent_code/ApolloInterop/Classes/Pipes/AsyncNamedPipeServer.cs

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
using ApolloInterop.Structs.ApolloStructs;
2+
using ApolloInterop.Utils;
23
using System;
34
using System.Collections.Concurrent;
45
using System.IO.Pipes;
56
using System.Security.AccessControl;
67
using System.Security.Principal;
8+
using System.Threading;
9+
using System.Threading.Tasks;
710

811
namespace ApolloInterop.Classes
912
{
@@ -64,6 +67,7 @@ public void Stop()
6467

6568
private void CreateServerPipe()
6669
{
70+
DebugHelp.DebugWriteLine($"Creating Named Pipe: {_pipeName}");
6771
NamedPipeServerStream pipe = new NamedPipeServerStream(
6872
_pipeName,
6973
PipeDirection.InOut,
@@ -76,7 +80,23 @@ private void CreateServerPipe()
7680
);
7781
//NamedPipeServerStream pipe = new NamedPipeServerStream(_pipeName, PipeDirection.InOut, -1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
7882
// wait for client to connect async
79-
pipe.BeginWaitForConnection(OnClientConnected, pipe);
83+
try
84+
{
85+
pipe.BeginWaitForConnection(OnClientConnected, pipe);
86+
}catch(Exception ex)
87+
{
88+
89+
}
90+
91+
}
92+
93+
private bool IsPersistentConnectionAsync(NamedPipeServerStream pipeServer)
94+
{
95+
// Try to complete the read task within the timeout
96+
var completedTask = Task.WhenAny(
97+
Task.Delay(500)
98+
);
99+
return pipeServer.IsConnected;
80100
}
81101

82102

@@ -92,6 +112,7 @@ private void OnMessageReceived(NamedPipeMessageArgs args)
92112

93113
private void OnDisconnect(NamedPipeMessageArgs args)
94114
{
115+
DebugHelp.DebugWriteLine($"Client disconnected from Named Pipe: {_pipeName}");
95116
Disconnect?.Invoke(this, args);
96117
}
97118

@@ -100,7 +121,11 @@ private void OnClientConnected(IAsyncResult result)
100121
// complete connection
101122
NamedPipeServerStream pipe = (NamedPipeServerStream)result.AsyncState;
102123
pipe.EndWaitForConnection(result);
103-
124+
DebugHelp.DebugWriteLine($"Client connected to Named Pipe: {_pipeName}");
125+
if(!IsPersistentConnectionAsync(pipe))
126+
{
127+
pipe.Close();
128+
}
104129
// create client pipe structure
105130
IPCData pd = new IPCData()
106131
{

Payload_Type/apollo/apollo/agent_code/NamedPipeProfile/NamedPipeProfile.cs

+11-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ internal struct AsyncPipeState
2828
internal CancellationTokenSource Cancellation;
2929
internal ST.Task Task;
3030
}
31-
private static JsonSerializer _jsonSerializer = new JsonSerializer();
3231
private string _namedPipeName;
3332
private AsyncNamedPipeServer _server;
3433
private bool _encryptedExchangeCheck;
@@ -86,10 +85,17 @@ public NamedPipeProfile(Dictionary<string, string> data, ISerializer serializer,
8685
byte[] currentChunkBytes = BitConverter.GetBytes(currentChunk);
8786
Array.Reverse(currentChunkBytes);
8887
DebugHelp.DebugWriteLine($"sending chunk {currentChunk}/{totalChunksToSend} with size {chunkData.Length + 8}");
89-
pipe.BeginWrite(sizeBytes, 0, sizeBytes.Length, OnAsyncMessageSent, p);
90-
pipe.BeginWrite(totalChunkBytes, 0, totalChunkBytes.Length, OnAsyncMessageSent, p);
91-
pipe.BeginWrite(currentChunkBytes, 0, currentChunkBytes.Length, OnAsyncMessageSent, p);
92-
pipe.BeginWrite(chunkData, 0, chunkData.Length, OnAsyncMessageSent, p);
88+
try
89+
{
90+
pipe.BeginWrite(sizeBytes, 0, sizeBytes.Length, OnAsyncMessageSent, p);
91+
pipe.BeginWrite(totalChunkBytes, 0, totalChunkBytes.Length, OnAsyncMessageSent, p);
92+
pipe.BeginWrite(currentChunkBytes, 0, currentChunkBytes.Length, OnAsyncMessageSent, p);
93+
pipe.BeginWrite(chunkData, 0, chunkData.Length, OnAsyncMessageSent, p);
94+
}catch(Exception ex)
95+
{
96+
break;
97+
}
98+
9399
}
94100
//pipe.BeginWrite(result, 0, result.Length, OnAsyncMessageSent, pipe);
95101
}

Payload_Type/apollo/apollo/mythic/agent_functions/builder.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Apollo(PayloadType):
2121
supported_os = [
2222
SupportedOS.Windows
2323
]
24-
version = "2.3.4"
24+
version = "2.3.5"
2525
wrapper = False
2626
wrapped_payloads = ["scarecrow_wrapper", "service_wrapper"]
2727
note = """

0 commit comments

Comments
 (0)