Re: Firewalling ....

Page principale

Répondre à ce message
Auteur: Edgar Bonet
Date:  
À: Liste Guilde
Sujet: Re: Firewalling ....
Samedi 24 janvier, Jérôme Kieffer a écrit :
> Question : est ce qu'il existe des machine avec leurs 65000 ports
> d'ouverts (même si il y a rien ou presque deriere) pour les portscanner
> et voir ce qui passe ou pas.
>
> Si ca existe pas, comment faire une machine de la sorte (quel demon
> utiliser ?)


Voilà un petit exercice sympa de programmation Linux. Je te livre ma
solution. Pour l'utiliser tu fais :

    gcc -DIGNORE_BIND_ERRORS -O openports.c -o openports
    su
    echo 100000 >> /proc/sys/fs/file-max
    ./openports 1 65535
    # Control-C quand tu en as marre


-- 
Edgar Bonet           Maison : 04 76 21 29 16    Bureau : 04 76 88 10 96
3 rue Jean Prévost    Mobile : 06 77 19 79 39    Fax    : 04 76 88 11 91
38000 Grenoble        guilde@???     www.edgar-bonet.org

/*
* openports: open all the ports in the specified range.
*
* Usage: openports first last
*
* It is an error to try to open a port that is already open, unless you
* compile the program with -DIGNORE_BIND_ERRORS, in which case all
* errors returned by bind() are ignored.
*
* This program forks a child for every PORTS_PER_CHILD (1000 by
* default) ports to open. If you change this value, make sure it is
* less than the maximum number of open files a process can have (1024
* on my system) minus the number of files already open at program's
* startup (3 in most cases).
*
* If the program fails with "socket: Too many open files in system",
* try to increase the value set in /proc/sys/fs/file-max.
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>

#ifndef PORTS_PER_CHILD
# define PORTS_PER_CHILD 1000
#endif

char *usage = "Usage: openports first last";

/*
 * Called by the parent process via atexit(): kills all processes in the
 * current session.
 */
void kill_children(void)
{
    kill(0, SIGTERM);
}


/* The normal way to stop this program is by Ctrl-C or SIGTERM. */
void signal_hadler(int signum)
{
    (void) signum;
    exit(EXIT_SUCCESS);
}


/* Open all the ports in the specified range in the current process. */
void openports(int first, int last)
{
    int port, s;
    struct sockaddr_in addr;
    char *message;
    int ret;


    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;


    for (port = first; port <= last; port++) {


        message = "socket";
        s = socket(PF_INET, SOCK_STREAM, 0);
        if (s == -1) goto die;


        message = "bind";
        addr.sin_port = htons(port);
        ret = bind(s, (struct sockaddr *) &addr, sizeof addr);
        if (ret == -1) {
#ifdef IGNORE_BIND_ERRORS
            continue;
#else
            goto die;
#endif
        }


        message = "listen";
        if (listen(s, 1) == -1) goto die;
    }
    return;


die:
    perror(message);
    fprintf(stderr, "port = %d\n", port);
    exit(EXIT_FAILURE);
}


int main(int argc, char *argv[])
{
    int first, last;
    int child_first, child_last;
    int nb_children;
    int i;
    pid_t pid;


    /* Usage: openports first last */
    if (argc != 3) {
        fprintf(stderr, usage);
        return EXIT_FAILURE;
    }
    first = atoi(argv[1]);
    last = atoi(argv[2]);


    /*
     * Make a new process group. When we receive Control-C,
     * signal_hadler() will ensure a normal termination and atexit() we
     * will kill all the processes in our group.
     */
    setsid();  /* if error we are already group leader */
    if (atexit(kill_children) != 0) {
        fprintf(stderr, "atexit failed\n");
        return EXIT_FAILURE;
    }
    if (signal(SIGINT,  signal_hadler) == SIG_ERR
     || signal(SIGTERM, signal_hadler) == SIG_ERR) {
        perror("signal");
        return EXIT_FAILURE;
    }


    /* Split the job and fork() children to handle the bits. */
    nb_children = (last-first) / PORTS_PER_CHILD + 1;
    for (i=0; i<nb_children; i++) {


        child_first = first + i * PORTS_PER_CHILD;
        child_last = child_first + PORTS_PER_CHILD - 1;
        if (child_last > last) child_last = last;


        pid = fork();
        if (pid == -1) {
            perror("fork");
            return EXIT_FAILURE;
        }
        if (pid == 0) {
            openports(child_first, child_last);
            pause();
            return EXIT_SUCCESS;
        }
    }


    /* Wait for Control-C. */
    pause();
    return EXIT_SUCCESS;
}