-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcovert.cpp
84 lines (65 loc) · 2.48 KB
/
covert.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define _CRT_SECURE_NO_WARNINGS
#include "covert.h"
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
SOCKET singleton_socket;
SOCKET getRawSocket(const char* host, u_short port)
{
WSADATA wsa_data;
WORD req_version = MAKEWORD(2, 2);
if (!singleton_socket) {
if (0 != WSAStartup(req_version, &wsa_data)) {
printf("[!] ERR:: Socket init failed. ERR_CODE %d\n", WSAGetLastError());
singleton_socket = NULL;
}
else {
singleton_socket = socket(AF_INET, SOCK_STREAM, 0);
if (singleton_socket == INVALID_SOCKET) {
printf("[!] ERR:: Socket creation failed. ERR_CODE %d\n", WSAGetLastError());
singleton_socket = NULL;
}
else {
struct sockaddr_in server_config;
unsigned long addr = inet_addr(host);
char* ip;
if (INADDR_NONE != addr) {
server_config.sin_addr.s_addr = addr;
}
else {
ip = inet_ntoa(*(struct in_addr*)gethostbyname(host)->h_addr_list[0]);
server_config.sin_addr.s_addr = inet_addr(ip);
}
server_config.sin_family = AF_INET;
server_config.sin_port = htons(port);
if (connect(singleton_socket, (struct sockaddr*)&server_config, sizeof(server_config)) < 0) {
printf("[!] ERR:: Socket connection failed. ERR_CODE %d\n", WSAGetLastError());
singleton_socket = NULL;
}
else {
printf("[i] Connection stablished with %s:%i\n", host, port);
printf("Sending...\n");
}
}
}
}
return singleton_socket;
}
int sendBytesRaw(SOCKET s, const char* buffer, int buffer_size, long pos)
{
int sent_bytes = 0;
int tried = 0;
int real_size;
char* full_buffer = prepareBuffer(buffer, buffer_size, pos, &real_size);
sent_bytes = send(s, full_buffer, real_size, 0);
while (sent_bytes < 0 && tried < MAX_RETRIES) {
printf("[!] ERR:: Could not send %iB. Retrying... %i\n", buffer_size, tried);
sent_bytes = send(s, full_buffer, real_size, 0);
++tried;
}
free(full_buffer);
return sent_bytes;
}
void closeSocket() {
closesocket(singleton_socket);
}