Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions c/meterpreter/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ DOCKER_CONTAINER=rapid7/msf-ubuntu-x64-meterpreter:latest
COMMON_GEN=-Wno-dev -DUSE_STATIC_MSVC_RUNTIMES=ON
COMMON_GEN_X86=-DCMAKE_TOOLCHAIN_FILE=../toolsets/i686-w64-mingw32.cmake -DBUILD_ARCH=Win32 ${COMMON_GEN}
COMMON_GEN_X64=-DCMAKE_TOOLCHAIN_FILE=../toolsets/x86_64-w64-mingw32.cmake -DBUILD_ARCH=x64 ${COMMON_GEN}
COMMON_GEN_ARM64=-DCMAKE_TOOLCHAIN_FILE=../toolsets/aarch64-w64-mingw32.cmake -DBUILD_ARCH=ARM64 ${COMMON_GEN}
COMMON_BUILD=--config Release

all: meterpreter

clean: meterpreter-x64-clean meterpreter-x86-clean
clean: meterpreter-x64-clean meterpreter-x86-clean meterpreter-arm64-clean

install:
@cp -f output/*.dll ../../../metasploit-framework/data/meterpreter
Expand All @@ -16,7 +17,22 @@ install:
### Build all
##########################################################################################

meterpreter: meterpreter-x86 meterpreter-x64
meterpreter: meterpreter-x86 meterpreter-x64 meterpreter-arm64

meterpreter-arm64: meterpreter-arm64-gen meterpreter-arm64-build
meterpreter-arm64-gen:
@cmake -S workspace -B workspace/build/mingw-arm64 $(COMMON_GEN_ARM64)
meterpreter-arm64-build:
@cmake --build workspace/build/mingw-arm64 $(COMMON_BUILD)
meterpreter-arm64-clean:
@rm -rf workspace/build/mingw-arm64* && rm -rf output/*.arm64.dll
meterpreter-metsrv-arm64: meterpreter-metsrv-arm64-gen meterpreter-metsrv-arm64-build
meterpreter-metsrv-arm64-gen:
@cmake -S workspace -B workspace/build/mingw-arm64-metsrv -DBUILD_ALL=OFF -DBUILD_METSRV=ON $(COMMON_GEN_ARM64)
meterpreter-metsrv-arm64-build:
@cmake --build workspace/build/mingw-arm64-metsrv $(COMMON_BUILD)

clean: meterpreter-x64-clean meterpreter-x86-clean meterpreter-arm64-clean

meterpreter-x86: meterpreter-x86-gen meterpreter-x86-build

Expand Down
278 changes: 142 additions & 136 deletions c/meterpreter/source/common/common.h
Original file line number Diff line number Diff line change
@@ -1,136 +1,142 @@
/*!
* @file common.h
* @brief Declarations for various common components used across the Meterpreter suite.
*/
#ifndef _METERPRETER_SOURCE_COMMON_COMMON_H
#define _METERPRETER_SOURCE_COMMON_COMMON_H

/*! @brief Set to 0 for "normal", and 1 to "verbose", comment out to disable completely. */
//#define DEBUGTRACE 0

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define SAFE_FREE(x) if(x){free(x);x=NULL;}

#include <winsock2.h>
#include <windows.h>

// Simple trick to get the current meterpreters arch
#define PROCESS_ARCH_UNKNOWN 0
#define PROCESS_ARCH_X86 1
#define PROCESS_ARCH_X64 2
#define PROCESS_ARCH_IA64 3

#ifdef _WIN64
#define dwMeterpreterArch PROCESS_ARCH_X64
#else
#define dwMeterpreterArch PROCESS_ARCH_X86
#endif

#ifdef __MINGW32__
#define ERROR_DBG_TERMINATE_THREAD 691L
#define ERROR_UNHANDLED_EXCEPTION 574L
#define ERROR_UNSUPPORTED_COMPRESSION 618L
#define ERROR_NOT_CAPABLE 775L
#define ERROR_NOTHING_TO_TERMINATE 778L
#define __try
#define __except(x) if(0)

#undef GetExceptionCode
#define GetExceptionCode() 0

#undef GetExceptionInformation
#define GetExceptionInformation() NULL
#endif

typedef struct __UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} _UNICODE_STRING, * _PUNICODE_STRING;

typedef DWORD __u32;
typedef struct ___u128 {
__u32 a1;
__u32 a2;
__u32 a3;
__u32 a4;
}__u128;

/*
* Avoid conflicts with Windows crypto API defines
*/
#undef OCSP_RESPONSE
#undef PKCS7_SIGNER_INFO
#undef X509_EXTENSIONS
#undef X509_CERT_PAIR
#undef X509_NAME

#ifdef DEBUGTRACE
#include "common_logging.h"
#define dprintf(...) real_dprintf(__VA_ARGS__)
#define INIT_LOGGING(metConfig) init_logging(metConfig->session.log_path);
#define SET_LOGGING_CONTEXT(api) set_logging_context(api->logging.get_logging_context(), api->logging.get_lock());
#if DEBUGTRACE == 1
#define vdprintf dprintf
#else
#define vdprintf(...) do{}while(0);
#endif
#else
#define dprintf(...) do{}while(0);
#define vdprintf(...) do{}while(0);
#define SET_LOGGING_CONTEXT(...)
#define INIT_LOGGING(...)
#endif

/*! @brief Sets `dwResult` to the return value of `GetLastError()`, prints debug output, then does `break;` */
#define BREAK_ON_ERROR( str ) { dwResult = GetLastError(); dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); break; }
/*! @brief Sets `dwResult` to `error`, prints debug output, then `break;` */
#define BREAK_WITH_ERROR( str, err ) { dwResult = err; dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); break; }
/*! @brief Sets `dwResult` to the return value of `WASGetLastError()`, prints debug output, then does `break;` */
#define BREAK_ON_WSAERROR( str ) { dwResult = WSAGetLastError(); dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); break; }
/*! @brief Sets `dwResult` to the return value of `GetLastError()`, prints debug output, then does `continue;` */
#define CONTINUE_ON_ERROR( str ) { dwResult = GetLastError(); dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); continue; }

/*! @brief Close a service handle if not already closed and set the handle to NULL. */
#define CLOSE_SERVICE_HANDLE( h ) if( h ) { CloseServiceHandle( h ); h = NULL; }
/*! @brief Close a handle if not already closed and set the handle to NULL. */
#define CLOSE_HANDLE( h ) if( h ) { DWORD dwHandleFlags; if(GetHandleInformation( h , &dwHandleFlags)) CloseHandle( h ); h = NULL; }

/*!
* @brief Output a debug string to the debug console.
* @details The function emits debug strings via `OutputDebugStringA`, hence all messages can be viewed
* using Visual Studio's _Output_ window, _DebugView_ from _SysInternals_, or _Windbg_.
*/
static _inline void real_dprintf(char *format, ...)
{
va_list args;
char buffer[1024];
size_t len;
_snprintf_s(buffer, sizeof(buffer), sizeof(buffer)-1, "[%04x] ", GetCurrentThreadId());
len = strlen(buffer);
va_start(args, format);
vsnprintf_s(buffer + len, sizeof(buffer)-len, sizeof(buffer)-len - 3, format, args);
strcat_s(buffer, sizeof(buffer), "\r\n");
OutputDebugStringA(buffer);
#ifdef DEBUGTRACE
log_to_file(buffer);
#endif
va_end(args);
}

#include "common_base.h"
#include "common_core.h"
#include "common_remote.h"
#include "common_channel.h"
#include "common_list.h"
#include "common_config.h"
#include "common_pivot_tree.h"
#include "common_thread.h"
#include "common_scheduler.h"
#include "common_command_ids.h"

#endif
/*!
* @file common.h
* @brief Declarations for various common components used across the Meterpreter suite.
*/
#ifndef _METERPRETER_SOURCE_COMMON_COMMON_H
#define _METERPRETER_SOURCE_COMMON_COMMON_H

/*! @brief Set to 0 for "normal", and 1 to "verbose", comment out to disable completely. */
//#define DEBUGTRACE 0

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#define SAFE_FREE(x) if(x){free(x);x=NULL;}

// Minimum Windows Vista for inet_pton and modern APIs
#include <winsock2.h>
#ifdef _M_ARM64
#include <ws2tcpip.h>
#endif
#include <windows.h>

// Simple trick to get the current meterpreters arch
#define PROCESS_ARCH_UNKNOWN 0
#define PROCESS_ARCH_X86 1
#define PROCESS_ARCH_X64 2
#define PROCESS_ARCH_IA64 3
#define PROCESS_ARCH_ARM64 4
#ifdef _M_ARM64
#define dwMeterpreterArch PROCESS_ARCH_ARM64
#elif defined(_WIN64)
#define dwMeterpreterArch PROCESS_ARCH_X64
#else
#define dwMeterpreterArch PROCESS_ARCH_X86
#endif

#ifdef __MINGW32__
#define ERROR_DBG_TERMINATE_THREAD 691L
#define ERROR_UNHANDLED_EXCEPTION 574L
#define ERROR_UNSUPPORTED_COMPRESSION 618L
#define ERROR_NOT_CAPABLE 775L
#define ERROR_NOTHING_TO_TERMINATE 778L
#define __try
#define __except(x) if(0)

#undef GetExceptionCode
#define GetExceptionCode() 0

#undef GetExceptionInformation
#define GetExceptionInformation() NULL
#endif

typedef struct __UNICODE_STRING
{
USHORT Length;
USHORT MaximumLength;
PWSTR Buffer;
} _UNICODE_STRING, * _PUNICODE_STRING;

typedef DWORD __u32;
typedef struct ___u128 {
__u32 a1;
__u32 a2;
__u32 a3;
__u32 a4;
}__u128;

/*
* Avoid conflicts with Windows crypto API defines
*/
#undef OCSP_RESPONSE
#undef PKCS7_SIGNER_INFO
#undef X509_EXTENSIONS
#undef X509_CERT_PAIR
#undef X509_NAME

#ifdef DEBUGTRACE
#include "common_logging.h"
#define dprintf(...) real_dprintf(__VA_ARGS__)
#define INIT_LOGGING(metConfig) init_logging(metConfig->session.log_path);
#define SET_LOGGING_CONTEXT(api) set_logging_context(api->logging.get_logging_context(), api->logging.get_lock());
#if DEBUGTRACE == 1
#define vdprintf dprintf
#else
#define vdprintf(...) do{}while(0);
#endif
#else
#define dprintf(...) do{}while(0);
#define vdprintf(...) do{}while(0);
#define SET_LOGGING_CONTEXT(...)
#define INIT_LOGGING(...)
#endif

/*! @brief Sets `dwResult` to the return value of `GetLastError()`, prints debug output, then does `break;` */
#define BREAK_ON_ERROR( str ) { dwResult = GetLastError(); dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); break; }
/*! @brief Sets `dwResult` to `error`, prints debug output, then `break;` */
#define BREAK_WITH_ERROR( str, err ) { dwResult = err; dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); break; }
/*! @brief Sets `dwResult` to the return value of `WASGetLastError()`, prints debug output, then does `break;` */
#define BREAK_ON_WSAERROR( str ) { dwResult = WSAGetLastError(); dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); break; }
/*! @brief Sets `dwResult` to the return value of `GetLastError()`, prints debug output, then does `continue;` */
#define CONTINUE_ON_ERROR( str ) { dwResult = GetLastError(); dprintf( "%s. error=%d (0x%x)", str, dwResult, (ULONG_PTR)dwResult ); continue; }

/*! @brief Close a service handle if not already closed and set the handle to NULL. */
#define CLOSE_SERVICE_HANDLE( h ) if( h ) { CloseServiceHandle( h ); h = NULL; }
/*! @brief Close a handle if not already closed and set the handle to NULL. */
#define CLOSE_HANDLE( h ) if( h ) { DWORD dwHandleFlags; if(GetHandleInformation( h , &dwHandleFlags)) CloseHandle( h ); h = NULL; }

/*!
* @brief Output a debug string to the debug console.
* @details The function emits debug strings via `OutputDebugStringA`, hence all messages can be viewed
* using Visual Studio's _Output_ window, _DebugView_ from _SysInternals_, or _Windbg_.
*/
static _inline void real_dprintf(char *format, ...)
{
va_list args;
char buffer[1024];
size_t len;
_snprintf_s(buffer, sizeof(buffer), sizeof(buffer)-1, "[%04x] ", GetCurrentThreadId());
len = strlen(buffer);
va_start(args, format);
vsnprintf_s(buffer + len, sizeof(buffer)-len, sizeof(buffer)-len - 3, format, args);
strcat_s(buffer, sizeof(buffer), "\r\n");
OutputDebugStringA(buffer);
#ifdef DEBUGTRACE
log_to_file(buffer);
#endif
va_end(args);
}

#include "common_base.h"
#include "common_core.h"
#include "common_remote.h"
#include "common_channel.h"
#include "common_list.h"
#include "common_config.h"
#include "common_pivot_tree.h"
#include "common_thread.h"
#include "common_scheduler.h"
#include "common_command_ids.h"

#endif
1 change: 0 additions & 1 deletion c/meterpreter/source/def/metsrv.def
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
NAME server.dll
EXPORTS
ReflectiveLoader @1 NONAME PRIVATE
Loading