1
mirror of https://github.com/rapid7/metasploit-payloads synced 2025-01-02 11:36:22 +01:00

Implement a sleep in windows that lasts longer

This commit is contained in:
OJ 2015-06-03 13:51:09 +10:00
parent 94a814fbc9
commit d89cd69bc5
4 changed files with 45 additions and 4 deletions

View File

@ -4,8 +4,10 @@
*/
#include "common.h"
#ifdef _WIN32
typedef NTSTATUS(*PNTDELAYEXECUTION)(BOOL alertable, PLARGE_INTEGER delayInterval);
/*!
* @brief Returns a unix timestamp in UTC.
* @return Integer value representing the UTC Unix timestamp of the current time.
@ -22,6 +24,41 @@ int current_unix_timestamp(void) {
ularge.HighPart = file_time.dwHighDateTime;
return (long)((ularge.QuadPart - 116444736000000000) / 10000000L);
}
/*!
* @brief Sleep for the given number of seconds.
* @param seconds DWORD value representing the number of seconds to sleep.
* @remark This was implemented so that extended sleep times can be used (beyond the
* 49 day limit imposed by Sleep()). With this implementation it's possible
* to wait for up to 3.1415926 metric buttloads of seconds.
* NtDelayExecution takes a parameter that is measured in 100-ns units, and
* negative values delay the thread relative to the current time. This is
* a 63-bit value (1 bit for sign), which means it should be possible to wait
* for approximately 29,227 years.
*/
VOID sleep(DWORD seconds)
{
static PNTDELAYEXECUTION pNtDelayExecution = NULL;
if (pNtDelayExecution == NULL)
{
pNtDelayExecution = (PNTDELAYEXECUTION)GetProcAddress(GetModuleHandleA("ntdll"), "NtDelayExecution");
dprintf("[SLEEP] delay pointer is %p", pNtDelayExecution);
}
// if still null, fallback on sleep
if (pNtDelayExecution == NULL)
{
Sleep(seconds * 1000);
}
else
{
LARGE_INTEGER l = { 0 };
// negative value sets a relative timeout
l.QuadPart = -((LONGLONG)seconds * 10000000);
pNtDelayExecution(FALSE, &l);
}
}
#else
#include <sys/time.h>

View File

@ -181,6 +181,8 @@ void real_dprintf(char *filename, int line, const char *function, char *format,
#ifdef _WIN32
VOID sleep(DWORD seconds);
#ifdef DEBUGTRACE
#define dprintf(...) real_dprintf(__VA_ARGS__)
#if DEBUGTRACE == 1

View File

@ -439,7 +439,9 @@ DWORD server_setup(MetsrvConfig* config)
if (remote->next_transport_wait > 0)
{
dprintf("[TRANS] Sleeping for %u seconds ...", remote->next_transport_wait);
Sleep(remote->next_transport_wait * 1000);
sleep(remote->next_transport_wait);
// the wait is a once-off thing, needs to be reset each time
remote->next_transport_wait = 0;
}

View File

@ -40,7 +40,7 @@ static DWORD reverse_tcp_run(SOCKET reverseSocket, SOCKADDR* sockAddr, int sockA
}
dprintf("[TCP RUN] Connection failed, sleeping for %u s", retryWait);
Sleep(retryWait * 1000);
sleep(retryWait);
} while (((DWORD)current_unix_timestamp() - (DWORD)start) < retryTotal);
if (result == SOCKET_ERROR)
@ -149,7 +149,7 @@ static DWORD reverse_tcp6(const char* host, const char* service, ULONG scopeId,
}
dprintf("[TCP RUN] Connection failed, sleeping for %u s", retryWait);
Sleep(retryWait * 1000);
sleep(retryWait);
} while (((DWORD)current_unix_timestamp() - (DWORD)start) < retryTotal);
closesocket(socketHandle);