2 * Copyright (c) 2006-2007 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 #include <netinet/in.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
30 #include <arpa/inet.h>
32 #include <arpa/nameser.h>
34 #include <arpa/nameser8_compat.h>
43 static void send_ping(int fd);
44 static void send_chunk(int fd);
49 struct sockaddr_in peer;
50 static char *topdomain;
54 /* Current IP packet */
55 static char activepacket[4096];
60 static uint16_t chunkid;
69 send_packet(int fd, char cmd, const char *data, const size_t datalen)
81 len = dns_build_hostname(buf + 1, sizeof(buf) - 1, data, datalen, topdomain);
82 len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf));
84 sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer));
90 return (packetlen != 0);
94 read_dns(int fd, char *buf, int buflen)
96 struct sockaddr_in from;
103 addrlen = sizeof(struct sockaddr);
104 if ((r = recvfrom(fd, packet, sizeof(packet), 0,
105 (struct sockaddr*)&from, &addrlen)) == -1) {
110 rv = dns_decode(buf, buflen, &q, QR_ANSWER, packet, r);
112 if (is_sending() && chunkid == q.id) {
113 /* Got ACK on sent packet */
114 packetpos += lastlen;
115 if (packetpos == packetlen) {
116 /* Packet completed */
130 tunnel_tun(int tun_fd, int dns_fd)
132 unsigned long outlen;
138 if ((read = read_tun(tun_fd, in, sizeof(in))) <= 0)
141 outlen = sizeof(out);
143 compress2((uint8_t*)out, &outlen, (uint8_t*)in, inlen, 9);
145 memcpy(activepacket, out, MIN(outlen, sizeof(activepacket)));
156 tunnel_dns(int tun_fd, int dns_fd)
158 unsigned long outlen;
164 if ((read = read_dns(dns_fd, in, sizeof(in))) <= 0)
167 outlen = sizeof(out);
169 if (uncompress((uint8_t*)out, &outlen, (uint8_t*)in, inlen) != Z_OK)
172 write_tun(tun_fd, out, outlen);
180 tunnel(int tun_fd, int dns_fd)
195 FD_SET(tun_fd, &fds);
196 FD_SET(dns_fd, &fds);
198 i = select(MAX(tun_fd, dns_fd) + 1, &fds, NULL, NULL, &tv);
206 if (i == 0) /* timeout */
209 if (FD_ISSET(tun_fd, &fds)) {
210 if (tunnel_tun(tun_fd, dns_fd) <= 0)
213 if (FD_ISSET(dns_fd, &fds)) {
214 if (tunnel_dns(tun_fd, dns_fd) <= 0)
226 char hex[] = "0123456789ABCDEF";
240 avail = packetlen - packetpos;
242 lastlen = dns_build_hostname(buf + 1, sizeof(buf) - 1, p, avail, topdomain);
244 if (lastlen == avail)
249 code |= (userid << 1);
251 len = dns_encode(packet, sizeof(packet), &q, QR_QUERY, buf, strlen(buf));
253 sendto(fd, packet, len, 0, (struct sockaddr*)&peer, sizeof(peer));
257 send_login(int fd, char *login, int len)
261 memset(data, 0, sizeof(data));
263 memcpy(&data[1], login, MIN(len, 16));
265 data[17] = (rand_seed >> 8) & 0xff;
266 data[18] = (rand_seed >> 0) & 0xff;
270 send_packet(fd, 'L', data, sizeof(data));
285 data[1] = (rand_seed >> 8) & 0xff;
286 data[2] = (rand_seed >> 0) & 0xff;
290 send_packet(fd, 'P', data, sizeof(data));
294 send_version(int fd, uint32_t version)
298 data[0] = (version >> 24) & 0xff;
299 data[1] = (version >> 16) & 0xff;
300 data[2] = (version >> 8) & 0xff;
301 data[3] = (version >> 0) & 0xff;
303 data[4] = (rand_seed >> 8) & 0xff;
304 data[5] = (rand_seed >> 0) & 0xff;
308 send_packet(fd, 'V', data, sizeof(data));
312 handshake(int dns_fd)
327 for (i = 0; running && i < 5; i++) {
331 send_version(dns_fd, VERSION);
334 FD_SET(dns_fd, &fds);
336 r = select(dns_fd + 1, &fds, NULL, NULL, &tv);
339 read = read_dns(dns_fd, in, sizeof(in));
347 payload = (((in[4] & 0xff) << 24) |
348 ((in[5] & 0xff) << 16) |
349 ((in[6] & 0xff) << 8) |
352 if (strncmp("VACK", in, 4) == 0) {
356 printf("Version ok, both running 0x%08x. You are user #%d\n", VERSION, userid);
358 } else if (strncmp("VNAK", in, 4) == 0) {
359 errx(1, "you run 0x%08x, server runs 0x%08x. giving up\n",
362 } else if (strncmp("VFUL", in, 4) == 0) {
363 errx(1, "server full, all %d slots are taken. try again later\n", payload);
367 warnx("did not receive proper login challenge\n");
370 printf("Retrying version check...\n");
372 errx(1, "couldn't connect to server");
376 login_calculate(login, 16, password, seed);
378 for (i=0; running && i<5 ;i++) {
382 send_login(dns_fd, login, 16);
385 FD_SET(dns_fd, &fds);
387 r = select(dns_fd + 1, &fds, NULL, NULL, &tv);
390 read = read_dns(dns_fd, in, sizeof(in));
398 if (strncmp("LNAK", in, 4) == 0) {
399 printf("Bad password\n");
401 } else if (sscanf(in, "%64[^-]-%64[^-]-%d",
402 server, client, &mtu) == 3) {
406 if (tun_setip(client) == 0 &&
407 tun_setmtu(mtu) == 0) {
410 warnx("Received handshake with bad data");
413 printf("Received bad handshake\n");
418 printf("Retrying login...\n");
425 set_target(const char *host)
429 h = gethostbyname(host);
431 err(1, "couldn't resolve name %s", host);
433 memset(&peer, 0, sizeof(peer));
434 peer.sin_family = AF_INET;
435 peer.sin_port = htons(53);
436 peer.sin_addr = *((struct in_addr *) h->h_addr);
441 extern char *__progname;
443 printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] "
444 "nameserver topdomain\n", __progname);
450 extern char *__progname;
452 printf("iodine IP over DNS tunneling client\n");
453 printf("Usage: %s [-v] [-h] [-f] [-u user] [-t chrootdir] [-d device] "
454 "[-P password] nameserver topdomain\n", __progname);
455 printf(" -v to print version info and exit\n");
456 printf(" -h to print this help and exit\n");
457 printf(" -f to keep running in foreground\n");
458 printf(" -u name to drop privileges and run as user 'name'\n");
459 printf(" -t dir to chroot to directory dir\n");
460 printf(" -d device to set tunnel device name\n");
461 printf(" -P password used for authentication (max 32 chars will be used)\n");
462 printf("nameserver is the IP number of the relaying nameserver\n");
463 printf("topdomain is the FQDN that is delegated to the tunnel endpoint.\n");
471 printf("iodine IP over DNS tunneling client\n");
472 printf("version: 0.4.0 from 2007-03-25\n");
478 main(int argc, char **argv)
489 memset(password, 0, 33);
496 while ((choice = getopt(argc, argv, "vfhu:t:d:P:")) != -1) {
517 strncpy(password, optarg, 32);
526 if (geteuid() != 0) {
527 printf("Run as root and you'll be happy.\n");
537 topdomain = strdup(argv[1]);
540 pw = getpwnam(username);
542 printf("User %s does not exist!\n", username);
547 if (strlen(password) == 0) {
548 printf("Enter password on stdin:\n");
549 scanf("%32s", password);
553 if ((tun_fd = open_tun(device)) == -1)
555 if ((dns_fd = open_dns(0, INADDR_ANY)) == -1)
559 signal(SIGINT, sighandler);
560 signal(SIGTERM, sighandler);
562 if(handshake(dns_fd))
565 printf("Sending queries for %s to %s\n", argv[1], argv[0]);
570 if (setgid(pw->pw_gid) < 0 || setuid(pw->pw_uid) < 0) {
571 printf("Could not switch to user %s!\n", username);
580 tunnel(tun_fd, dns_fd);