Scan the entire Internet looking for webservers

This revision is from 2024/02/10 06:24. 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;
}

Add DNS

<pre>

#include #include #include #include #include #include #include #include #define DNS_PORT 53 #define PORT_80 80 #define PORT_443 443 int check_all_ports(char *ip_address) { int dns_result = check_dns_server(ip_address); if (dns_result) { fprintf(stdout, "DNS server detected: %sn", ip_address); return 1; } char *open_ip = check_port_open(PORT_80, ip_address); if (open_ip != NULL) { fprintf(stdout, "HTTP server detected: %s:%dn", open_ip, PORT_80); free(open_ip); } open_ip = check_port_open(PORT_443, ip_address); if (open_ip != NULL) { fprintf(stdout, "HTTPS server detected: %s:%dn", open_ip, PORT_443); free(open_ip); } return dns_result || (open_ip != NULL); } 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; } } int main() { FILE *csv_file = fopen("servers.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); if (check_all_ports(ip_address)) { fprintf(csv_file, "%sn", ip_address); } } } } } fclose(csv_file); return 0; }

  

📝 📜 ⏱️ ⬆️