Skip to content

Commit 88f66be

Browse files
committed
6.5 pre5 resolve windows bug
1 parent 39e3bba commit 88f66be

4 files changed

Lines changed: 32 additions & 12 deletions

File tree

c/meterpreter/source/extensions/stdapi/server/net/socket/tcp.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ DWORD tcp_channel_client_close(Channel *channel, Packet *request, LPVOID context
108108
{
109109
TcpClientContext *ctx = (TcpClientContext *)context;
110110

111-
dprintf( "[TCP] tcp_channel_client_close. channel=0x%08X, ctx=0x%08X", channel, ctx );
111+
dprintf("[TCP] tcp_channel_client_close. channel=%p id=%u ctx=%p", (void*)channel, met_api->channel.get_id(channel), (void*)ctx);
112112

113113
if (ctx)
114114
{
@@ -185,15 +185,27 @@ DWORD tcp_channel_client_local_notify(Remote * remote, TcpClientContext * ctx)
185185

186186
if (dwBytesRead == 0)
187187
{
188-
dprintf("[TCP] tcp_channel_client_local_notify. [closed] channel=0x%08X read=0x%.8x", ctx->channel, dwBytesRead);
188+
dprintf("[TCP] tcp_channel_client_local_notify. [closed] channel=0x%08X id=%u fd=%u read=0x%.8x", ctx->channel, ctx->channel ? met_api->channel.get_id(ctx->channel) : 0, (DWORD)ctx->fd, dwBytesRead);
189189

190-
// Set the native channel operations context to NULL
191-
met_api->channel.set_native_io_context(ctx->channel, NULL);
190+
// Set the native channel operations context to NULL so the channel
191+
// close handler won't try to free the context again.
192+
if (ctx->channel)
193+
{
194+
met_api->channel.set_native_io_context(ctx->channel, NULL);
195+
}
192196

193-
// Sleep for a quarter second
197+
// NULL out the channel pointer before sleeping/freeing to prevent a
198+
// race where the framework sends core_channel_close concurrently,
199+
// channel_destroy fires with ctx=NULL (skipping the ctx->channel=NULL
200+
// assignment in tcp_channel_client_close), and free_socket_context
201+
// subsequently calls channel_close on a dangling/recycled pointer.
202+
ctx->channel = NULL;
203+
204+
// Sleep for a quarter second to let the framework drain buffered data
205+
// before the socket is closed.
194206
Sleep(250);
195207

196-
// Free the context
208+
// Free the context (closes the socket)
197209
free_tcp_client_context(ctx);
198210

199211
// Stop processing
@@ -418,6 +430,9 @@ VOID free_socket_context(SocketContext *ctx)
418430
{
419431
dprintf("[TCP] free_socket_context. ctx=0x%08X", ctx);
420432

433+
// Capture fd before closing so it can be logged accurately below
434+
SOCKET originalFd = ctx->fd;
435+
421436
// Close the socket and notification handle
422437
if (ctx->fd)
423438
{
@@ -427,6 +442,7 @@ VOID free_socket_context(SocketContext *ctx)
427442

428443
if (ctx->channel)
429444
{
445+
dprintf("[TCP] free_socket_context. closing channel=0x%08X id=%u fd=%u", ctx->channel, met_api->channel.get_id(ctx->channel), (DWORD)originalFd);
430446
met_api->channel.close(ctx->channel, ctx->remote, NULL, 0, NULL);
431447
ctx->channel = NULL;
432448
}

c/meterpreter/source/extensions/stdapi/server/net/socket/tcp_server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ VOID free_tcp_server_context(TcpServerContext * ctx)
9090
break;
9191
}
9292

93-
dprintf("[TCP-SERVER] free_tcp_server_context. ctx=0x%08X", ctx);
93+
dprintf("[TCP-SERVER] free_tcp_server_context. ctx=0x%08X channel=0x%08X id=%u fd=%u", ctx, ctx->channel, ctx->channel ? met_api->channel.get_id(ctx->channel) : 0, (DWORD)ctx->fd);
9494

9595
if (ctx->fd)
9696
{
@@ -129,7 +129,7 @@ DWORD tcp_channel_server_close(Channel * channel, Packet * request, LPVOID conte
129129

130130
do
131131
{
132-
dprintf("[TCP-SERVER] tcp_channel_server_close. channel=0x%08X, ctx=0x%08X", channel, ctx);
132+
dprintf("[TCP-SERVER] tcp_channel_server_close. channel=0x%08X id=%u ctx=0x%08X", channel, met_api->channel.get_id(channel), ctx);
133133

134134
if (!ctx)
135135
{

c/meterpreter/source/metsrv/channel.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ DWORD channel_write(Channel *channel, Remote *remote, Tlv *addend, DWORD addendL
599599
packet_add_tlv_raw(request, TLV_TYPE_CHANNEL_DATA, buffer, length);
600600
}
601601

602-
packet_add_tlv_uint(request, TLV_TYPE_LENGTH, channel_get_id(channel));
602+
packet_add_tlv_uint(request, TLV_TYPE_LENGTH, length);
603603

604604
// Initialize the packet completion routine
605605
if (completionRoutine)
@@ -628,6 +628,8 @@ DWORD channel_close(Channel *channel, Remote *remote, Tlv *addend,
628628
DWORD res = ERROR_SUCCESS;
629629
Tlv commandIdTlv;
630630

631+
dprintf("[CHANNEL] channel_close ENTER. channel=%p id=%u", (void*)channel, channel ? channel_get_id(channel) : 0);
632+
631633
do
632634
{
633635
Packet *request = packet_create(PACKET_TLV_TYPE_REQUEST, 0);
@@ -658,7 +660,7 @@ DWORD channel_close(Channel *channel, Remote *remote, Tlv *addend,
658660
realRequestCompletion = &requestCompletion;
659661
}
660662

661-
dprintf("[CHANNEL] channel_close. channel=0x%08X completion=0x%.8x", channel, completionRoutine);
663+
dprintf("[CHANNEL] channel_close. channel=0x%08X id=%u completion=0x%.8x", channel, channel ? channel_get_id(channel) : 0, completionRoutine);
662664

663665
// Transmit the packet with the supplied completion routine, if any.
664666
res = packet_transmit(remote, request, realRequestCompletion);

gem/Rakefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,15 @@ end
9595

9696
task :win_compile do
9797
Dir.chdir(c_source) do
98-
system('cmd.exe /c make.bat')
98+
success = system('cmd.exe /c make.bat')
99+
abort('win_compile failed: make.bat could not be executed or exited with a non-zero status. Check the build output for errors.') unless success
99100
end
100101
end
101102

102103
task :java_compile do
103104
Dir.chdir(java_source) do
104-
system('mvn package -Ddeploy.path=output -Dandroid.sdk.path=$ANDROID_HOME -Dandroid.ndk.path=$ANDROID_NDK_HOME -Dandroid.release=true -q -P deploy')
105+
success = system('mvn package -Ddeploy.path=output -Dandroid.sdk.path=$ANDROID_HOME -Dandroid.ndk.path=$ANDROID_NDK_HOME -Dandroid.release=true -q -P deploy')
106+
abort('java_compile failed: mvn could not be executed or exited with a non-zero status. Check the build output for errors.') unless success
105107
end
106108
end
107109

0 commit comments

Comments
 (0)