-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpbproxy.c
More file actions
310 lines (257 loc) · 9.4 KB
/
pbproxy.c
File metadata and controls
310 lines (257 loc) · 9.4 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <netdb.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/aes.h>
#include <openssl/rand.h>
#define BUFFER_SZ 4096
#define IV_SZ 8
struct thread_args {
int socket_fd;
struct sockaddr connect_address;
int connect_address_length;
struct sockaddr_in socket_service;
AES_KEY aes_key;
};
void* create_connection(void *args) {
if (args == NULL) {
pthread_exit(0);
}
struct thread_args *connection_params = (struct thread_args *) args;
int socket_fd = connection_params->socket_fd;
struct sockaddr connect_address = connection_params->connect_address;
int connect_address_length = connection_params->connect_address_length;
struct sockaddr_in socket_service = connection_params->socket_service;
AES_KEY aes_key = connection_params->aes_key;
int fd_service = socket(AF_INET, SOCK_STREAM, 0), packet_len;
char buffer[BUFFER_SZ];
if (connect(fd_service, (struct sockaddr *) &socket_service, sizeof(socket_service)) == -1) {
fprintf(stderr, "Connection failed.\n");
pthread_exit(0);
}
fprintf(stderr, "New connection established\n");
int flgs = fcntl(socket_fd, F_GETFL);
fcntl(socket_fd, F_SETFL, flgs | O_NONBLOCK);
flgs = fcntl(fd_service, F_GETFL);
fcntl(fd_service, F_SETFL, flgs | O_NONBLOCK);
unsigned char ivec[AES_BLOCK_SIZE], ecount[AES_BLOCK_SIZE], IV[IV_SZ];
unsigned int num;
unsigned char ivec_d[AES_BLOCK_SIZE], ecount_d[AES_BLOCK_SIZE], IV_d[IV_SZ];
unsigned int num_d;
RAND_bytes(IV, 8);
num = 0;
memset(ecount, 0, AES_BLOCK_SIZE);
memset(ivec + 8, 0, 8);
memcpy(ivec, IV, 8);
num_d = 0;
memset(ecount_d, 0, AES_BLOCK_SIZE);
memset(ivec_d + 8, 0, 8);
while (1) {
packet_len = read(socket_fd, buffer, BUFFER_SZ);
while (packet_len > 0) {
unsigned char decrypted[packet_len - 8];
memcpy(IV_d, buffer, 8);
memcpy(ivec_d, IV_d, 8);
AES_ctr128_encrypt(buffer + 8, decrypted, packet_len - 8, &aes_key, ivec_d, ecount_d, &num_d);
if (write(fd_service, decrypted, packet_len - 8) < 0) {
fprintf(stderr, "Closing connection.\n");
close(socket_fd);
close(fd_service);
free(connection_params);
pthread_exit(0);
}
if (packet_len < BUFFER_SZ) {
break;
}
}
if (packet_len == 0) {
break;
}
packet_len = read(fd_service, buffer, BUFFER_SZ);
while (packet_len > 0) {
unsigned char encrypted[packet_len];
char *payload = (char*) malloc(packet_len + 8);
memcpy(payload, IV, 8);
AES_ctr128_encrypt(buffer, encrypted, packet_len, &aes_key, ivec, ecount, &num);
memcpy(payload + 8, encrypted, packet_len);
if (write(socket_fd, payload, packet_len + 8) < 0) {
fprintf(stderr, "Closing connection.\n");
close(socket_fd);
close(fd_service);
free(connection_params);
pthread_exit(0);
}
free(payload);
if (packet_len < BUFFER_SZ) {
break;
}
}
if (packet_len == 0) {
break;
}
}
fprintf(stderr, "Closing connection.\n");
close(socket_fd);
close(fd_service);
free(connection_params);
pthread_exit(0);
}
int init_server_mode(long l_port, struct hostent *d_host, long d_port, AES_KEY aes_key) {
pthread_t connection_thread;
struct sockaddr_in socket_client, socket_service;
memset(&socket_client, 0, sizeof(socket_client));
memset(&socket_service, 0, sizeof(socket_service));
int fd_client = socket(AF_INET, SOCK_STREAM, 0);
socket_client.sin_family = AF_INET;
socket_client.sin_addr.s_addr = htons(INADDR_ANY);
socket_client.sin_port = htons(l_port);
socket_service.sin_family = AF_INET;
socket_service.sin_addr.s_addr = ((struct in_addr *)(d_host->h_addr))->s_addr;
socket_service.sin_port = htons(d_port);
bind(fd_client, (struct sockaddr *) &socket_client, sizeof(socket_client));
if (listen(fd_client, 8) < 0) {
fprintf(stderr, "Listening has failed.\n");
return 0;
}
while (1) {
int socket_fd, connect_address_length;
struct sockaddr connect_address;
socket_fd = accept(fd_client, &connect_address, &connect_address_length);
if (socket_fd > 0) {
struct thread_args *args = (struct thread_args *) malloc(sizeof(struct thread_args));
args->socket_fd = socket_fd;
args->connect_address = connect_address;
args->connect_address_length = connect_address_length;
args->socket_service = socket_service;
args->aes_key = aes_key;
pthread_create(&connection_thread, 0, create_connection, (void*) args);
pthread_detach(connection_thread);
}
}
return 0;
}
int init_client_mode(struct hostent *d_host, long d_port, AES_KEY aes_key) {
struct sockaddr_in socket_proxy;
memset(&socket_proxy, 0, sizeof(socket_proxy));
int socket_fd = socket(AF_INET, SOCK_STREAM, 0), packet_len;
char buffer[BUFFER_SZ];
socket_proxy.sin_family = AF_INET;
socket_proxy.sin_addr.s_addr = ((struct in_addr *)(d_host->h_addr))->s_addr;
socket_proxy.sin_port = htons(d_port);
if (connect(socket_fd, (struct sockaddr *) &socket_proxy, sizeof(socket_proxy)) == -1) {
fprintf(stderr, "Connection failed.\n");
return 0;
}
fcntl(socket_fd, F_SETFL, O_NONBLOCK);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
unsigned char ivec[AES_BLOCK_SIZE], ecount[AES_BLOCK_SIZE], IV[IV_SZ];
unsigned int num;
unsigned char ivec_d[AES_BLOCK_SIZE], ecount_d[AES_BLOCK_SIZE], IV_d[IV_SZ];
unsigned int num_d;
RAND_bytes(IV, 8);
num = 0;
memset(ecount, 0, AES_BLOCK_SIZE);
memset(ivec + 8, 0, 8);
memcpy(ivec, IV, 8);
num_d = 0;
memset(ecount_d, 0, AES_BLOCK_SIZE);
memset(ivec_d + 8, 0, 8);
while(1) {
packet_len = read(STDIN_FILENO, buffer, BUFFER_SZ);
while (packet_len > 0) {
unsigned char encrypted[packet_len];
char *payload = (char*) malloc(packet_len + 8);
memcpy(payload, IV, 8);
AES_ctr128_encrypt(buffer, encrypted, packet_len, &aes_key, ivec, ecount, &num);
memcpy(payload + 8, encrypted, packet_len);
if (write(socket_fd, payload, packet_len + 8) < 0) {
close(socket_fd);
return 0;
}
free(payload);
if (packet_len < BUFFER_SZ) {
break;
}
}
packet_len = read(socket_fd, buffer, BUFFER_SZ);
while (packet_len > 0) {
unsigned char decrypted[packet_len - 8];
memcpy(IV_d, buffer, 8);
memcpy(ivec_d, IV_d, 8);
AES_ctr128_encrypt(buffer + 8, decrypted, packet_len - 8, &aes_key, ivec_d, ecount_d, &num_d);
write(STDOUT_FILENO, decrypted, packet_len - 8);
if (packet_len < BUFFER_SZ) {
break;
}
}
}
return 0;
}
int main(int argc, char *argv[]) {
int opt, server = 0;
char *key = NULL, *listening_port = NULL, *destination = NULL, *destination_port = NULL;
while ((opt = getopt(argc, argv, "l:k:")) != -1) {
switch(opt) {
case 'l':
listening_port = optarg;
server = 1;
break;
case 'k':
key = optarg;
break;
case '?':
if (optopt == 'l' || optopt == 'k') return 0;
else break;
default:
break;
}
}
if (optind < argc - 2 || optind > argc - 2) {
fprintf(stderr, "Please enter the correct number of arguments.\n");
fprintf(stderr, "Usage: ./pbproxy [-l port] -k keyfile destination port\n");
return 0;
}
else if (optind == argc - 2) {
destination = argv[optind];
destination_port = argv[optind + 1];
}
// read key file
FILE *fp = fopen(key, "rb");
if (!fp) {
fprintf(stderr, "Given key is not valid. Please try again.\n");
return 0;
}
fseek(fp, 0L, SEEK_END);
int _key_size = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char *buf = malloc(_key_size * sizeof(char));
fread(buf, sizeof(char), _key_size, fp);
const char *_key = buf;
fclose(fp);
AES_KEY aes_key;
if (AES_set_encrypt_key(_key, 128, &aes_key) < 0) {
fprintf(stderr, "AES encrypted key error.\n");
return 0;
}
long l_port, d_port;
d_port = atol(destination_port);
struct hostent *d_host = gethostbyname(destination);
if (!d_host) {
fprintf(stderr, "Destination host name is not valid. Please try again.\n");
return 0;
}
if (server) {
l_port = atol(listening_port);
init_server_mode(l_port, d_host, d_port, aes_key);
}
else {
init_client_mode(d_host, d_port, aes_key);
}
return 0;
}