mitmd

mitm TCP connections
Log | Files | Refs | README

commit 460d9f75e27caaaf8b06a85dcda5833c022913c6
Author: hhvn <dev@hhvn.uk>
Date:   Wed, 16 Jun 2021 20:48:29 +0100

makefile mitmd.c arg.h: init

Diffstat:
Aarg.h | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amakefile | 6++++++
Amitmd.c | 197+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 273 insertions(+), 0 deletions(-)

diff --git a/arg.h b/arg.h @@ -0,0 +1,70 @@ +/* + * Copy me if you can. + * by 20h + * + * 2012-2016 Christoph Lohmann <20h at r-36 dot net> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#ifndef ARG_H__ +#define ARG_H__ + +extern char *argv0; + +/* use main(int argc, char *argv[]) */ +#define ARGBEGIN for (argv0 = *argv, argv++, argc--;\ + argv[0] && argv[0][0] == '-'\ + && argv[0][1];\ + argc--, argv++) {\ + char argc_;\ + char **argv_;\ + int brk_;\ + if (argv[0][1] == '-' && argv[0][2] == '\0') {\ + argv++;\ + argc--;\ + break;\ + }\ + int i_;\ + for (i_ = 1, brk_ = 0, argv_ = argv;\ + argv[0][i_] && !brk_;\ + i_++) {\ + if (argv_ != argv)\ + break;\ + argc_ = argv[0][i_];\ + switch (argc_) + +#define ARGEND }\ + } + +#define ARGC() argc_ + +#define EARGF(x) ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + ((x), abort(), (char *)0) :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#define ARGF() ((argv[0][i_+1] == '\0' && argv[1] == NULL)?\ + (char *)0 :\ + (brk_ = 1, (argv[0][i_+1] != '\0')?\ + (&argv[0][i_+1]) :\ + (argc--, argv++, argv[0]))) + +#endif diff --git a/makefile b/makefile @@ -0,0 +1,6 @@ +BIN = mitmd + +build: $(BIN) + +.c: + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $< diff --git a/mitmd.c b/mitmd.c @@ -0,0 +1,197 @@ +/* Copyright (c) 2021 hhvn <dev@hhvn.uk> */ + +#include <stdio.h> +#include <netdb.h> +#include <string.h> +#include <stdlib.h> +#include <stdarg.h> +#include <signal.h> +#include <libgen.h> +#include <unistd.h> +#include <limits.h> +#include <errno.h> +#include <sys/socket.h> +#include <sys/types.h> +#include <sys/wait.h> +#include "arg.h" + +#define CQUEUE 255 +#define CLIENT_STYLE "\033[96m>\033[0m" +#define REMOTE_STYLE "\033[90m<\033[0m" + +char *argv0; +char *myhost = "localhost", + *myport = "9999", + *host, + *port; + +void +die(const int exitc, const char *format, ...) { + va_list ap; + + va_start(ap, format); + fprintf(stderr, "Fatal: "); + vfprintf(stderr, format, ap); + va_end(ap); + + exit(exitc); +} + +void +sighandler(int signal) { + switch (signal) { + case SIGCHLD: + while (waitpid(-1, NULL, WNOHANG) == 0); + break; + default: + exit(EXIT_SUCCESS); + } +} + +int +read_line(int fd, char *dest, size_t len) { + size_t i = 0; + char c = 0; + + do { + if (read(fd, &c, sizeof(char)) != sizeof(char)) + return 0; + dest[i++] = c; + } while (c != '\n' && i < len); + + dest[i] = '\0'; + return 1; +} + + +int +getsock(struct addrinfo *hints, char *host, char *port) { + struct addrinfo *ai; + int sret, sockopt; + int fd; + + if ((sret = getaddrinfo(host, port, hints, &ai)) != 0 || ai == NULL) + die(1, "getaddrinfo() for %s:%s, %s\n", host, port, gai_strerror(sret)); + + fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol); + sockopt = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (int*)&sockopt, sizeof(int)) == -1) + die(1, "setsockopt(): %s\n", strerror(errno)); + if (bind(fd, ai->ai_addr, ai->ai_addrlen) == -1) + die(1, "bind(): %s\n", strerror(errno)); + if (listen(fd, CQUEUE) == -1) + die(1, "listen(): %s\n", strerror(errno)); + + freeaddrinfo(ai); + + return fd; +} + +void +mitm(int client, char *host, char *port) { + struct addrinfo hints; + struct addrinfo *ai; + char bufc[NL_TEXTMAX]; + char bufr[NL_TEXTMAX]; + int sret, remote; + pid_t pid; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if ((sret = getaddrinfo(host, port, &hints, &ai)) != 0 || ai == NULL) { + dprintf(client, "%s%s\n", "getaddrinfo(): ", gai_strerror(sret)); + goto cleanup; + } + if ((remote = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) == 1) { + dprintf(client, "%s%s\n", "socket(): ", strerror(errno)); + goto cleanup; + } + if (connect(remote, ai->ai_addr, ai->ai_addrlen) == -1) { + dprintf(client, "%s%s\n", "connect(): ", strerror(errno)); + goto cleanup; + } + + switch (pid = fork()) { + case -1: + goto cleanup; + case 0: + while (read_line(client, bufc, sizeof(bufc)) != 0) { + printf("%03d%s%03d %s", client, CLIENT_STYLE, remote, bufc); + write(remote, bufc, strlen(bufc)); + } + exit(EXIT_SUCCESS); + default: + while (read_line(remote, bufr, sizeof(bufr)) != 0) { + printf("%03d%s%03d %s", client, REMOTE_STYLE, remote, bufr); + write(client, bufr, strlen(bufr)); + } + goto cleanup; + } + +cleanup: + freeaddrinfo(&hints); + freeaddrinfo(ai); +} + +void +usage(void) { + fprintf(stderr, "usage: %s [-i myinterface] [-p myport] host port\n", basename(argv0)); + exit(2); +} + +int +main(int argc, char *argv[]) { + struct sockaddr_storage addr; + struct addrinfo hints; + socklen_t addrlen; + int sock, handle; + pid_t pid; + + ARGBEGIN { + case 'i': + myhost = EARGF(usage()); + break; + case 'p': + myport = EARGF(usage()); + break; + default: + usage(); + } ARGEND; + + if (argc != 2) + usage(); + + host = argv[0]; + port = argv[1]; + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; + hints.ai_flags = AI_PASSIVE; + hints.ai_socktype = SOCK_STREAM; + sock = getsock(&hints, myhost, myport); + + signal(SIGCHLD, sighandler); + + for (;;) { + addrlen = sizeof(addr); + if ((handle = accept(sock, (struct sockaddr *)&addr, &addrlen)) == -1) + die(1, "accept(): %s\n", strerror(errno)); + + switch (pid = fork()) { + case -1: + fprintf(stderr, "fork(): %s\n", strerror(errno)); + shutdown(handle, SHUT_RDWR); + close(handle); + break; + case 0: + mitm(handle, host, port); + shutdown(handle, SHUT_RDWR); + close(handle); + exit(EXIT_SUCCESS); + } + } + + return EXIT_SUCCESS; +}