• STATISTIQUES
  • Il y a eu un total de 1 membres et 5452 visiteurs sur le site dans les dernières 24h pour un total de 5 453 personnes!


    Membres: 2 452
    Discussions: 3 572
    Messages: 32 822
    Tutoriels: 77
    Téléchargements: 38
    Sites dans l'annuaire: 58


  • ANNUAIRE
  • [EN] Packet Storm
    Packet Storm est un site qui combine nouvelles de la sécurité informatique, téléchargemen...
    Vulnérabilités
    [EN] Astalavista
    Un site aux ressources incontournable depuis plusieurs années, Astalavista est réellement devenue un cl...
    Hacking
    [FR] NewbieContest
    Nous vous proposons une série de challenges regroupant plusieurs domaines allant de l'exploitation de fail...
    Hacking
    [FR] Le top web
    Nous offrons une sélection la plus large possible de resources webmaster gratuites, hébergement gratuit...
    Webmaster
    [FR] WeChall
    Audio: 3, Coding: 11, Cracking: 9, Crypto: 18, Encoding: 11, Exploit: 44, Forensics: 1, Fun: 6, HTTP: 6, Image: 8, Java:...
    Challenges
    [FR] Asp-php
    Tutoriaux sur ASP, PHP, ASP.net, XML, SQL, Javascript, HTML, VML - Scripts et ressources pour webmasters - Forums d&#...
    Programmation
    [EN] xda-developers
    Très bon site pour les gros bidouilleurs de smartphone de windows à androïd et de Apple jusqu'...
    Phreaking

  • DONATION
  • Si vous avez trouvé ce site internet utile, nous vous invitons à nous faire un don du montant de votre choix via Paypal. Ce don servira à financer notre hébergement.

    MERCI!




Note de ce sujet :
  • Moyenne : 0 (0 vote(s))
  • 1
  • 2
  • 3
  • 4
  • 5
[C] redirection de stream tcp d'un port a un autre
17-10-2014, 12h06 (Modification du message : 17-10-2014, 12h07 par ark.)
Message : #1
ark Hors ligne
Psyckomodo!
*****



Messages : 1,033
Sujets : 48
Points: 317
Inscription : Sep 2011
[C] redirection de stream tcp d'un port a un autre
Yo,

Je sais pas si ca vous interesse, j'ai code un petit tool qui permet de faire l'equivalent de :
Code BASH :
$> nc -l port1 | nc -l port2


Mais ce en gerant du multi input / multi output.
C'est a dire que le serveur va ecouter sur 2 ports differents, un pour l'input, l'autre pour l'output. Et va rediriger tout ce qui se dit sur l'input vers tous les outputs.

Et du coup, ca permet de montre un exemple d'utilisation de select pour monitorer des socket en lecture (Oui, j'aurais pu le faire en ecriture aussi, mais ca a pas trop d'interet dans ce cas je pense.)

Bref, voila le code :
Code C :
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <arpa/inet.h>

#define BUFF_SIZE 4096
#define BACKLOG 20

struct clients {
    int _fd;
    struct clients *_next;
};

static int run = 1;

void siginthdlr(int __attribute__((unused)) sig) {
    run = 0;
}

int set_fds(struct clients *cli, fd_set *rfd) {

    struct clients *tmp = cli;
    int max = 0;

    while (tmp != NULL) {
        if (tmp->_fd > max)
            max = tmp->_fd;
        FD_SET(tmp->_fd, rfd);
        tmp = tmp->_next;
    }
    return max;
}

int add_node(int sock, struct clients **list) {
 
    struct clients *node = NULL;

    node = malloc(sizeof(*node));
    if (node == NULL)
        return -1;
    node->_fd = accept(sock, NULL, NULL);

    node->_next = NULL;
    if (*list)
        node->_next = *list;
    *list = node;
    return 0;
}

/* because lazy to do better */
void rm_node(struct clients **cli, struct clients *node) {

    struct clients *tmp = *cli;  

    close(node->_fd);
    if (node == *cli) {
        *cli = node->_next;
        free(node);
        return;
    }

    while (tmp && tmp->_next != node) {
        tmp = tmp->_next;
    }
    tmp->_next = node->_next;
    free(node);
}

void free_list(struct clients *lst) {
    if (lst->_next)
        free_list(lst->_next);
    free(lst);
}

int server_run(short iport, short oport) {

    int issock; /* input server socket */
    int ossock; /* output server socket */
    struct clients *icli = NULL; /* input client list */
    struct clients *ocli = NULL; /* output client list */
    struct clients *tmp = NULL;
    struct clients *tmp2 = NULL;
    struct sockaddr_in isin;
    struct sockaddr_in osin;
    char buffer[BUFF_SIZE] = {0};
    fd_set rfd;
    int max, maxtmp, ret;

    /* Socket creation */
    issock = socket(AF_INET, SOCK_STREAM, 0);
    ossock = socket(AF_INET, SOCK_STREAM, 0);
    if (issock == 0 || ossock == 0) {
        perror("socket");
        return -1;
    }

    isin.sin_family = AF_INET;
    isin.sin_port = htons(iport);
    isin.sin_addr.s_addr = htonl(INADDR_ANY);
    osin.sin_family = AF_INET;
    osin.sin_port = htons(oport);
    osin.sin_addr.s_addr = htonl(INADDR_ANY);
   
    if (bind(issock, (struct sockaddr *)&isin, sizeof(isin)) == -1 ||
        bind(ossock, (struct sockaddr *)&osin, sizeof(osin)) == -1) {
        perror("bind");
        return -1;
    }
    if (listen(issock, BACKLOG) != 0 ||
        listen(ossock, BACKLOG) != 0) {
        perror("listen");
        return -1;
    }

    while (run) {

        /* Select initialization */
        FD_ZERO(&rfd);
        FD_SET(issock, &rfd);
        FD_SET(ossock, &rfd);
        max = set_fds(icli, &rfd);
        maxtmp = set_fds(ocli, &rfd);
        max = (maxtmp > max) ? maxtmp : max;
        if (issock > max)
            max = issock;
        if (ossock > max)
            max = ossock;

        /* Select */
        if ((select(max + 1, &rfd, NULL, NULL, NULL)) == -1) {
            perror("select");
        }

        /* Check fds */
        if (FD_ISSET(issock, &rfd) != 0)
            add_node(issock, &icli);
        if (FD_ISSET(ossock, &rfd) != 0)
           add_node(ossock, &ocli);

        /* remove output clients that have closed their connexions */
        tmp = ocli;
        while (tmp) {
            if (FD_ISSET(tmp->_fd, &rfd) != 0) {
                ret = read(tmp->_fd, buffer, 1);
                if (ret == 0)
                    rm_node(&ocli, tmp);
            }
            tmp = tmp->_next;
        }

        /* copy input to output */
        tmp = icli;
        while (tmp) {
            if (FD_ISSET(tmp->_fd, &rfd) != 0) {
                tmp2 = ocli;
                ret = read(tmp->_fd, buffer, sizeof(buffer));
                if (ret <= 0) {
                   rm_node(&icli, tmp);
                }
                buffer[ret - 1] = 0;
                while (tmp2) {
                    write(tmp2->_fd, buffer, strlen(buffer));
                    tmp2 = tmp2->_next;
                }
            }
            tmp = tmp->_next;
        }
    }

    free_list(icli);
    free_list(ocli);
    return 0;
}

int main(int argc, char *argv[]) {

    short iport, oport;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s iport oport\n", *argv);
        fprintf(stderr, "\tiport\tthe port to listen input\n");
        fprintf(stderr, "\toport\tthe port for output stream\n");
        return 1;
    }    

    iport = atoi(argv[1]);
    oport = atoi(argv[2]);
    signal(SIGINT, &siginthdlr);
    if (server_run(iport, oport) == -1)
        return 1;
    return 0;
}


N'hesitez pas a me faire part de vos remarques. :)

Code que vous pourrez retrouver sur mon github ici : https://github.com/Ark444/PoCz/tree/master/tcp_redir
+1 (3) -1 (0) Répondre


Sujets apparemment similaires…
Sujet Auteur Réponses Affichages Dernier message
  [C] Port Scanner sakiir 10 4,719 25-12-2012, 00h50
Dernier message: sakiir

Atteindre :


Utilisateur(s) parcourant ce sujet : 1 visiteur(s)
N-PN
Accueil | Challenges | Tutoriels | Téléchargements | Forum | Retourner en haut