1
mirror of https://github.com/rapid7/metasploit-payloads synced 2025-02-16 00:24:29 +01:00

Cleanup registered commands using the pseudo extension's deinit callback

This commit is contained in:
Spencer McIntyre 2021-01-14 12:11:57 -05:00
parent ebdcd95615
commit 9aece96e94
3 changed files with 26 additions and 8 deletions
c/meterpreter/source/metsrv

View File

@ -106,22 +106,28 @@ Command* extensionCommands = NULL;
/*!
* @brief Register dispatch routines provided by the meterpreter core.
* @return Returns the first command of the array of commands that was registered.
*/
Command* register_base_dispatch_routines(void) {
Command* register_base_dispatch_routines(void)
{
Command* pFirstCommand = NULL;
command_register_all(baseCommands);
pFirstCommand = extensionCommands;
while (pFirstCommand && pFirstCommand->command_id != baseCommands[0].command_id) {
dprintf("[COMMAND LIST] Command: %p command id %u != %u", pFirstCommand, pFirstCommand->command_id, baseCommands[0].command_id);
pFirstCommand = pFirstCommand->next;
}
if (pFirstCommand) {
dprintf("Found first command: %p (id: %u)", pFirstCommand, pFirstCommand->command_id);
}
return pFirstCommand;
}
/*!
* @brief Deregister dispatch routines provided by the meterpreter core.
*/
void deregister_base_dispatch_routines(void)
{
command_deregister_all(baseCommands);
}
/*!
* @brief Register a full list of commands with meterpreter.
* @param commands The array of commands that are to be registered for the module/extension.

View File

@ -13,6 +13,8 @@ DWORD command_register(Command *command);
DWORD command_deregister(Command *command);
VOID command_join_threads( VOID );
BOOL command_handle( Remote *remote, Packet *packet );
Command* register_base_dispatch_routines(void);
void deregister_base_dispatch_routines(void);
#endif

View File

@ -147,6 +147,17 @@ DWORD request_core_enumextcmd(Remote* remote, Packet* packet)
return ERROR_SUCCESS;
}
/*
* Deinitialize the core pseudo extension
*/
static DWORD deinit_server_extension(Remote* remote)
{
command_deregister_all(customCommands);
deregister_base_dispatch_routines();
return ERROR_SUCCESS;
}
/*
* Registers custom command handlers
*/
@ -160,10 +171,11 @@ VOID register_dispatch_routines()
PEXTENSION pExtension = (PEXTENSION)malloc(sizeof(EXTENSION));
if (pExtension) {
memset(pExtension, 0, sizeof(EXTENSION));
pExtension->deinit = deinit_server_extension;
pExtension->end = pFirstCommand;
pExtension->start = extensionCommands;
list_push(gExtensionList, pExtension);
dprintf("[EXTENSTION] Registered core pseudo extension %p", pExtension);
dprintf("[CORE] Registered the core pseudo extension %p", pExtension);
}
}
@ -188,8 +200,6 @@ VOID deregister_dispatch_routines(Remote * remote)
free(extension);
}
command_deregister_all(customCommands);
list_destroy(gExtensionList);
}