-
-
Save hasherezade/26f3ae0316337cf97e04bd0b578cea50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <winsock2.h> | |
#include <time.h> | |
#include <stdint.h> | |
#include "peb_lookup.h" | |
typedef struct FunctionStruct_t_s | |
{ | |
decltype(&listen) _listen; | |
decltype(&send) _send; | |
decltype(&bind) _bind; | |
decltype(&closesocket) _closesocket; | |
decltype(&htons) _htons; | |
decltype(&time) _time; | |
decltype(&exit) _exit; | |
decltype(&WSAStartup) _WSAStartup; | |
decltype(&ctime) _ctime; | |
decltype(&accept) _accept; | |
decltype(&calloc) _calloc; | |
decltype(&printf) _printf; | |
decltype(&free) _free; | |
decltype(&WSACleanup) _WSACleanup; | |
decltype(&socket) _socket; | |
decltype(&strlen) _strlen; | |
decltype(&WSAGetLastError) _WSAGetLastError; | |
} FunctionStruct_t; | |
typedef struct GlobalStruct_t_s | |
{ | |
int PORT; | |
} GlobalStruct_t; | |
void initializeWinsock(FunctionStruct_t* functions_p, GlobalStruct_t* globals_p) | |
{ | |
WSADATA wsaData; | |
if (functions_p->_WSAStartup((WORD)(((BYTE)(((DWORD_PTR)2) & 0xff)) | (((WORD)((BYTE)(((DWORD_PTR)2) & 0xff))) << 8)), &wsaData) != 0) | |
{ | |
functions_p->_printf("Failed to initialize Winsock: %d\n", functions_p->_WSAGetLastError()); | |
functions_p->_exit(1); | |
} | |
} | |
SOCKET createSocket(FunctionStruct_t* functions_p, GlobalStruct_t* globals_p) | |
{ | |
SOCKET serverSocket = functions_p->_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); | |
if (serverSocket == INVALID_SOCKET) | |
{ | |
functions_p->_printf("Could not create socket: %d\n", functions_p->_WSAGetLastError()); | |
functions_p->_WSACleanup(); | |
functions_p->_exit(1); | |
} | |
return serverSocket; | |
} | |
void bindSocket(FunctionStruct_t* functions_p, GlobalStruct_t* globals_p, SOCKET serverSocket) | |
{ | |
struct sockaddr_in server; | |
server.sin_family = AF_INET; | |
server.sin_addr.s_addr = INADDR_ANY; | |
server.sin_port = functions_p->_htons(globals_p->PORT); | |
if (functions_p->_bind(serverSocket, (struct sockaddr*)(&server), sizeof(server)) == SOCKET_ERROR) | |
{ | |
functions_p->_printf("Bind failed with error code: %d\n", functions_p->_WSAGetLastError()); | |
functions_p->_closesocket(serverSocket); | |
functions_p->_WSACleanup(); | |
functions_p->_exit(1); | |
} | |
} | |
void listenForConnections(FunctionStruct_t* functions_p, GlobalStruct_t* globals_p, SOCKET serverSocket) | |
{ | |
if (functions_p->_listen(serverSocket, 3) == SOCKET_ERROR) | |
{ | |
functions_p->_printf("Listen failed with error code: %d\n", functions_p->_WSAGetLastError()); | |
functions_p->_closesocket(serverSocket); | |
functions_p->_WSACleanup(); | |
functions_p->_exit(1); | |
} | |
functions_p->_printf("Server is listening on port %d...\n", globals_p->PORT); | |
} | |
void acceptConnection(FunctionStruct_t* functions_p, GlobalStruct_t* globals_p, SOCKET serverSocket) | |
{ | |
struct sockaddr_in client; | |
int c = sizeof(struct sockaddr_in); | |
SOCKET clientSocket = functions_p->_accept(serverSocket, (struct sockaddr*)(&client), &c); | |
if (clientSocket == INVALID_SOCKET) | |
{ | |
functions_p->_printf("Accept failed with error code: %d\n", functions_p->_WSAGetLastError()); | |
functions_p->_closesocket(serverSocket); | |
functions_p->_WSACleanup(); | |
functions_p->_exit(1); | |
} | |
functions_p->_printf("Connection accepted.\n"); | |
time_t currentTime; | |
functions_p->_time(¤tTime); | |
char* timeString = functions_p->_ctime(¤tTime); | |
functions_p->_printf("Time is %s", timeString); | |
int32_t ret = functions_p->_send(clientSocket, timeString, functions_p->_strlen(timeString), 0); | |
if ((-1) == ret) | |
{ | |
int error = functions_p->_WSAGetLastError(); | |
functions_p->_printf("Send Failed with error: %d.\n", error); | |
} | |
else | |
{ | |
functions_p->_printf("Sent %d bytes.\n", ret); | |
} | |
functions_p->_closesocket(clientSocket); | |
} | |
GlobalStruct_t* init_GlobalStruct(FunctionStruct_t* functions_p) | |
{ | |
GlobalStruct_t* globals_p = (GlobalStruct_t*)functions_p->_calloc(1, sizeof(GlobalStruct_t)); | |
if (NULL == globals_p) | |
{ | |
goto G_CLEANUP; | |
} | |
globals_p->PORT = 12345; | |
G_CLEANUP: | |
return globals_p; | |
} | |
bool init_FunctionStruct(FunctionStruct_t* functions_p) | |
{ | |
LPVOID kernel32_handle = get_module_by_name((const LPWSTR)L"kernel32.dll"); | |
if (NULL == kernel32_handle) | |
{ | |
return false; | |
} | |
LPVOID load_lib = get_func_by_name((HMODULE)kernel32_handle, (LPSTR)"LoadLibraryA"); | |
if (NULL == load_lib) | |
{ | |
return false; | |
} | |
LPVOID get_proc = get_func_by_name((HMODULE)kernel32_handle, (LPSTR)"GetProcAddress"); | |
if (NULL == get_proc) | |
{ | |
return false; | |
} | |
auto _LoadLibraryA = reinterpret_cast<decltype(&LoadLibraryA)>(load_lib); | |
auto _GetProcAddress = reinterpret_cast<decltype(&GetProcAddress)>(get_proc); | |
HMODULE WS232_dll = _LoadLibraryA("ws2_32.dll"); | |
if (NULL == WS232_dll) | |
{ | |
return false; | |
} | |
functions_p->_listen = reinterpret_cast<decltype(&listen)>(_GetProcAddress((HMODULE)WS232_dll, "listen")); | |
if (!functions_p->_listen) | |
{ | |
return false; | |
} | |
functions_p->_send = reinterpret_cast<decltype(&send)>(_GetProcAddress((HMODULE)WS232_dll, "send")); | |
if (!functions_p->_send) | |
{ | |
return false; | |
} | |
functions_p->_bind = reinterpret_cast<decltype(&bind)>(_GetProcAddress((HMODULE)WS232_dll, "bind")); | |
if (!functions_p->_bind) | |
{ | |
return false; | |
} | |
functions_p->_closesocket = reinterpret_cast<decltype(&closesocket)>(_GetProcAddress((HMODULE)WS232_dll, "closesocket")); | |
if (!functions_p->_closesocket) | |
{ | |
return false; | |
} | |
functions_p->_htons = reinterpret_cast<decltype(&htons)>(_GetProcAddress((HMODULE)WS232_dll, "htons")); | |
if (!functions_p->_htons) | |
{ | |
return false; | |
} | |
HMODULE msvcrt_handle = _LoadLibraryA("msvcrt.dll"); | |
if (NULL == msvcrt_handle) | |
{ | |
return false; | |
} | |
functions_p->_time = reinterpret_cast<decltype(&time)>(_GetProcAddress((HMODULE)msvcrt_handle, "time")); | |
if (!functions_p->_time) | |
{ | |
return false; | |
} | |
HMODULE ucrtbase_handle = _LoadLibraryA("ucrtbase.dll"); | |
if (NULL == ucrtbase_handle) | |
{ | |
return false; | |
} | |
functions_p->_exit = reinterpret_cast<decltype(&exit)>(_GetProcAddress((HMODULE)ucrtbase_handle, "exit")); | |
if (!functions_p->_exit) | |
{ | |
return false; | |
} | |
functions_p->_WSAStartup = reinterpret_cast<decltype(&WSAStartup)>(_GetProcAddress((HMODULE)WS232_dll, "WSAStartup")); | |
if (!functions_p->_WSAStartup) | |
{ | |
return false; | |
} | |
functions_p->_ctime = reinterpret_cast<decltype(&ctime)>(_GetProcAddress((HMODULE)msvcrt_handle, "ctime")); | |
if (!functions_p->_ctime) | |
{ | |
return false; | |
} | |
functions_p->_accept = reinterpret_cast<decltype(&accept)>(_GetProcAddress((HMODULE)WS232_dll, "accept")); | |
if (!functions_p->_accept) | |
{ | |
return false; | |
} | |
functions_p->_calloc = reinterpret_cast<decltype(&calloc)>(_GetProcAddress((HMODULE)ucrtbase_handle, "calloc")); | |
if (!functions_p->_calloc) | |
{ | |
return false; | |
} | |
functions_p->_printf = reinterpret_cast<decltype(&printf)>(_GetProcAddress((HMODULE)msvcrt_handle, "printf")); | |
if (!functions_p->_printf) | |
{ | |
return false; | |
} | |
functions_p->_free = reinterpret_cast<decltype(&free)>(_GetProcAddress((HMODULE)msvcrt_handle, "free")); | |
if (!functions_p->_free) | |
{ | |
return false; | |
} | |
functions_p->_WSACleanup = reinterpret_cast<decltype(&WSACleanup)>(_GetProcAddress((HMODULE)WS232_dll, "WSACleanup")); | |
if (!functions_p->_WSACleanup) | |
{ | |
return false; | |
} | |
functions_p->_socket = reinterpret_cast<decltype(&socket)>(_GetProcAddress((HMODULE)WS232_dll, "socket")); | |
if (!functions_p->_socket) | |
{ | |
return false; | |
} | |
functions_p->_strlen = reinterpret_cast<decltype(&strlen)>(_GetProcAddress((HMODULE)msvcrt_handle, "strlen")); | |
if (!functions_p->_strlen) | |
{ | |
return false;; | |
} | |
functions_p->_WSAGetLastError = reinterpret_cast<decltype(&WSAGetLastError)>(_GetProcAddress((HMODULE)WS232_dll, "WSAGetLastError")); | |
if (!functions_p->_WSAGetLastError) | |
{ | |
return false;; | |
} | |
return true; | |
} | |
int main() | |
{ | |
FunctionStruct_t functions_p; | |
if (!init_FunctionStruct(&functions_p)) | |
{ | |
goto F_CLEANUP; | |
} | |
GlobalStruct_t* globals_p = NULL; | |
globals_p = init_GlobalStruct(&functions_p); | |
if (NULL == globals_p) | |
{ | |
goto G_CLEANUP; | |
} | |
initializeWinsock(&functions_p, globals_p); | |
SOCKET serverSocket = createSocket(&functions_p, globals_p); | |
bindSocket(&functions_p, globals_p, serverSocket); | |
listenForConnections(&functions_p, globals_p, serverSocket); | |
while (1) | |
{ | |
acceptConnection(&functions_p, globals_p, serverSocket); | |
} | |
functions_p._closesocket(serverSocket); | |
functions_p._WSACleanup(); | |
functions_p._free(globals_p); | |
G_CLEANUP: | |
F_CLEANUP: | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment