Scan the entire Internet looking for webservers

This revision is from 2024/02/10 11:24. You can Restore it.

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

#define SERVER_ADDR_SIZE sizeof(struct sockaddr_in)

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


int is_address_reachable(const char *ip_address, int port) {
    struct sockaddr_in sa;
    int sock;
    fd_set fdset;
    struct timeval tv;
    int flags;

    memset(&sa, 0, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = inet_addr(ip_address);
    sa.sin_port = htons(port);

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock < 0) {
        perror("Socket creation error");
        return 0;
    }

    // Set the socket to non-blocking mode
    flags = fcntl(sock, F_GETFL, 0);
    fcntl(sock, F_SETFL, flags | O_NONBLOCK);

    if (connect(sock, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
        if (errno == EINPROGRESS) {
            // Connection attempt is in progress, wait for it to complete or timeout
            FD_ZERO(&fdset);
            FD_SET(sock, &fdset);
            tv.tv_sec = 1; // Timeout value in seconds
            tv.tv_usec = 0;
            if (select(sock + 1, NULL, &fdset, NULL, &tv) > 0) {
                int optval;
                socklen_t optlen = sizeof(optval);
                getsockopt(sock, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen);
                if (optval == 0) {
                    // Connection succeeded
                    close(sock);
                    return 1;
                }
            }
        }
        // Either connection failed or timed out
        close(sock);
        return 0;
    }

    // Connection succeeded immediately
    close(sock);
    return 1;
}

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() {
    int sock = 0;
    FILE *ssl_file = fopen("ssl.csv", "w");
    FILE *http_file = fopen("http.csv", "w");
    
    if (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);
                    printf("Scanning: %sn", ip_address); // Output the current IP being scanned

                        if (is_address_reachable(ip_address, 80)) fprintf(http_file, "%sn", ip_address);

                        if (is_address_reachable(ip_address, 443)) 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(ssl_file);
    fclose(http_file);
    return 0;
}

  

📝 📜 ⏱️ ⬆️