Scan the entire Internet looking for webservers

This revision is from 2024/02/10 09:41. You can Restore it.

#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define PORT_80 80
#define PORT_443 443

char *check_port_open(int port, char *ip_address) {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("Failed to create socket");
        return NULL;
    }

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    inet_pton(AF_INET, ip_address, &server_addr.sin_addr);

    int result = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
    close(sock);

    if (result == 0) {
        printf("Port %d is open on %sn", port, ip_address);
        return strdup(ip_address); // Allocate memory for the copied IP address
    } else {
        if (errno != ECONNREFUSED && errno != ENETUNREACH) {
            perror("Failed to connect");
        }
        return NULL;
    }
}

nt main() {
    FILE *csv_file = fopen("open_ports.csv", "w");
    if (csv_file == NULL) {
        perror("Failed to open CSV file");
        return 1;
    }

    for (int i = 0; i < 256; ++i) { // Loop for first octet
        for (int j = 0; j < 256; ++j) { // Loop for second octet
            for (int k = 0; k < 256; ++k) { // Loop for third octet
                for (int l = 1; l <= 255; ++l) { // Loop for fourth octet, start from 1 to exclude 0.0.0.0

                    char ip_address[16];
                    snprintf(ip_address, 16, "%d.%d.%d.%d", i, j, k, l);

                    char *open_ip = check_port_open(PORT_80, ip_address);
                    if (open_ip != NULL) {
                        fprintf(csv_file, "%s,%dn", open_ip, PORT_80);
                        free(open_ip);
                    }

                    open_ip = check_port_open(PORT_443, ip_address);
                    if (open_ip != NULL) {
                        fprintf(csv_file, "%s,%dn", open_ip, PORT_443);
                        free(open_ip);
                    }
                }
            }
        }
    }

    fclose(csv_file);
    return 0;
}

Another with DNS...

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define DNS_PORT 53
#define PORT_80 80
#define PORT_443 443

int check_dns_server(char *ip_address) {
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(DNS_PORT);
    inet_pton(AF_INET, ip_address, &server_addr.sin_addr);

    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock == -1) {
        perror("Failed to create socket");
        return 0;
    }

    int result = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
    close(sock);

    if (result == 0) {
        return 1;
    } else {
        return 0;
    }
}

int check_web_server(int port, char *ip_address) {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("Failed to create socket");
        return 0;
    }

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    inet_pton(AF_INET, ip_address, &server_addr.sin_addr);

    int result = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
    close(sock);

    if (result == 0) {
        return 1;
    } else {
        return 0;
    }
}

int main() {
    FILE *dns_file = fopen("dns.csv", "w");
    FILE *ssl_file = fopen("ssl.csv", "w");
    FILE *http_file = fopen("http.csv", "w");
    
    if (dns_file == NULL || ssl_file == NULL || http_file == NULL) {
        perror("Failed to open CSV file");
        return 1;
    }

    for (int i = 0; i < 256; ++i) { // Loop for first octet
        for (int j = 0; j < 256; ++j) { // Loop for second octet
            for (int k = 0; k < 256; ++k) { // Loop for third octet
                for (int l = 1; l <= 255; ++l) { // Loop for fourth octet, start from 1 to exclude 0.0.0.0

                    char ip_address[16];
                    snprintf(ip_address, 16, "%d.%d.%d.%d", i, j, k, l);

                    if (check_dns_server(ip_address)) {
                        fprintf(dns_file, "%sn", ip_address);
                    } else if (check_web_server(PORT_80, ip_address)) {
                        fprintf(http_file, "%sn", ip_address);
                    } else if (check_web_server(PORT_443, ip_address)) {
                        fprintf(ssl_file, "%sn", ip_address);
                    }
                }
            }
        }
    }

    fclose(dns_file);
    fclose(ssl_file);
    fclose(http_file);
    return 0;
}

Resume Feature


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#define DNS_PORT 53
#define PORT_80 80
#define PORT_443 443
#define IP_FILE "last_ip.txt"

int check_dns_server(char *ip_address) {
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(DNS_PORT);
    inet_pton(AF_INET, ip_address, &server_addr.sin_addr);

    int sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock == -1) {
        perror("Failed to create socket");
        return 0;
    }

    int result = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
    close(sock);

    if (result == 0) {
        return 1;
    } else {
        return 0;
    }
}

int check_web_server(int port, char *ip_address) {
    int sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("Failed to create socket");
        return 0;
    }

    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(port);
    inet_pton(AF_INET, ip_address, &server_addr.sin_addr);

    int result = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr));
    close(sock);

    if (result == 0) {
        return 1;
    } else {
        return 0;
    }
}

void save_last_ip(char *ip_address) {
    FILE *ip_file = fopen(IP_FILE, "w");
    if (ip_file != NULL) {
        fprintf(ip_file, "%sn", ip_address);
        fclose(ip_file);
    }
}

void load_last_ip(char *ip_address) {
    FILE *ip_file = fopen(IP_FILE, "r");
    if (ip_file != NULL) {
        fscanf(ip_file, "%s", ip_address);
        fclose(ip_file);
    }
}

int main() {
    FILE *dns_file = fopen("dns.csv", "w");
    FILE *ssl_file = fopen("ssl.csv", "w");
    FILE *http_file = fopen("http.csv", "w");
    
    if (dns_file == NULL || ssl_file == NULL || http_file == NULL) {
        perror("Failed to open CSV file");
        return 1;
    }

    char last_ip[16];
    load_last_ip(last_ip);

    int start_octet, start_octet_2, start_octet_3, start_octet_4;
    sscanf(last_ip, "%d.%d.%d.%d", &start_octet, &start_octet_2, &start_octet_3, &start_octet_4);

    for (int i = start_octet; i < 256; ++i) { // Loop for first octet
        for (int j = start_octet_2; j < 256; ++j) { // Loop for second octet
            for (int k = start_octet_3; k < 256; ++k) { // Loop for third octet
                for (int l = start_octet_4; l <= 255; ++l) { // Loop for fourth octet, start from 1 to exclude 0.0.0.0

                    char ip_address[16];
                    snprintf(ip_address, 16, "%d.%d.%d.%d", i, j, k, l);

                    if (check_dns_server(ip_address)) {
                        fprintf(dns_file, "%sn", ip_address);
                    } else if (check_web_server(PORT_80, ip_address)) {
                        fprintf(http_file, "%sn", ip_address);
                    } else if (check_web_server(PORT_443, ip_address)) {
                        fprintf(ssl_file, "%sn", ip_address);
                    }

                    save_last_ip(ip_address);
                }
                start_octet_4 = 1; // Reset the fourth octet after finishing the loop
            }
            start_octet_3 = 0; // Reset the third octet after finishing the loop
        }
        start_octet_2 = 0; // Reset the second octet after finishing the loop
    }

    fclose(dns_file);
    fclose(ssl_file);
    fclose(http_file);
    return 0;
}

  

📝 📜 ⏱️ ⬆️