exploit the possibilities
Home Files News &[SERVICES_TAB]About Contact Add New

relay.v4.c

relay.v4.c
Posted Nov 24, 2004
Authored by Thun

This forwards connections on any port you want to any host & port you like. Added the ability to select which device to listen on. Based on Laq's relay.3.

tags | tool
systems | unix
SHA-256 | 086dfbc690fc8acaf175d245b3348248fc74d730c4f0b737150ad04bf943a604

relay.v4.c

Change Mirror Download
/*

Modified 11/23/2004
by thun

Based on Laq's relay .3

.3a
Added ability to set device to listen on

*/

#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/sockios.h>

#define VERSION "0.4"

struct ifreq ifreq;
struct sockaddr_in *saptr = NULL;

int do_connect(char *host, char *port);
int do_listen(char *port);

int once=0;
int any=1;

int main(int ac, char **av)
{
fd_set s_read, d_read;
char buffer[2048];
int s_sock, d_sock;
int fd;
int i;

if (ac < 4)
{
fprintf(stderr, "\nUsage: %s lport rhost rport [options]\n", av[0]);
fprintf(stderr, "\nOptions:\n");
fprintf(stderr, "\t-once Run once then die.\n");
fprintf(stderr, "\t-i eth[X] Set device (example -i eth0).\n");
fprintf(stderr, "\nRelay %s by Thun based on Laq's relay .3\n\n", VERSION);
exit(1);
}

if (ac >= 5)
for (i=4; i <ac; i++)
{
//check options

if (!strcmp(av[i],"-once"))
once=1;

if (!strcmp(av[i],"-i"))
{
strncpy(ifreq.ifr_name, av[i+1], IF_NAMESIZE);
any=0;


}

}



printf("\nRelay %s\n", VERSION );
printf("\t*Relaying port %s -> %s:%s\n", av[1], av[2], av[3]);

if (once)
printf("\t*Running only once\n");



fd = open("/dev/tty", O_RDWR);

if (ioctl(fd, TIOCNOTTY, "1") < 0)
{
perror("\t** ERROR ioctl");
exit(1);
}


if (fork() != 0)
exit(1);
else
close(fd);

while(1)
{
s_sock = do_listen(av[1]);
d_sock = do_connect(av[2], av[3]);

while(1)
{
struct timeval timeout;
int status;

FD_ZERO(&s_read);
FD_SET(s_sock, &s_read);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
bzero(buffer, 2048);
select(s_sock+1, &s_read, NULL, NULL, &timeout);

if (FD_ISSET(s_sock, &s_read))
{
status = recv(s_sock, buffer, 2048, 0);
if (status == 0)
exit(1);
if (send(d_sock, buffer, status, 0) == 0)
exit(1);
}

FD_ZERO(&d_read);
FD_SET(d_sock, &d_read);
timeout.tv_sec = 1;
timeout.tv_usec = 0;
bzero(buffer, 2048);
select(d_sock+1, &d_read, NULL, NULL, &timeout);

if (FD_ISSET(d_sock, &d_read))
{
status = recv(d_sock, buffer, 2048, 0);
if (status == 0)
exit(1);
if (send(s_sock, buffer, status, 0) == 0)
exit(1);
}
}






}
}

int do_connect(char *host, char *port)
{
struct hostent *he;
struct sockaddr_in addr;
int sock;

sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
perror("\t** ERROR socket");
exit(1);
}

addr.sin_port = htons(strtol(port, NULL, 0));
addr.sin_family = AF_INET;

he = gethostbyname(host);
if (he == NULL)
addr.sin_addr.s_addr = inet_addr(host);
else
memcpy((void *)&addr.sin_addr, he->h_addr_list[0], sizeof(struct
in_addr));

if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("\t** ERROR connect");
exit(1);
}

return sock;
}

int do_listen(char *port)
{
struct sockaddr_in addr;
int mainsock;
int sock;
int len;

mainsock = socket(AF_INET, SOCK_STREAM, 0);
if (mainsock < 0)
{
perror("\t** ERROR socket");
exit(1);
}





addr.sin_family = AF_INET;

if (any)
{

printf("\t*Listening on any.\n");
addr.sin_addr.s_addr = INADDR_ANY;
}
else
{
if (ioctl(mainsock, SIOCGIFADDR, &ifreq) < 0)
{
perror("\t** ERROR ioctl");
exit(1);
}

//print out the address
saptr = (struct sockaddr_in *)&ifreq.ifr_addr;

printf("\t*Listening on %s ip %s\n",
ifreq.ifr_name,
inet_ntoa(saptr->sin_addr) );

addr.sin_addr.s_addr = inet_addr(inet_ntoa(saptr->sin_addr));


}

addr.sin_port = htons(strtol(port, NULL, 0));

if (bind(mainsock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("\t** ERROR bind");
exit(1);
}

if (listen(mainsock, 3) < 0)
{
perror("\t** ERROR listen");
exit(1);
}

while (1)
{
len = sizeof(addr);
sock = accept(mainsock, (struct sockaddr *)&addr, &len);

if (sock < 0)
{
perror("\t** ERROR accept");
exit(1);
}

if (once)
break;

if (fork() == 0)
{
close(sock);
continue;
}
else
break;
}

return sock;
}
Login or Register to add favorites

File Archive:

November 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Nov 1st
    30 Files
  • 2
    Nov 2nd
    0 Files
  • 3
    Nov 3rd
    0 Files
  • 4
    Nov 4th
    12 Files
  • 5
    Nov 5th
    44 Files
  • 6
    Nov 6th
    18 Files
  • 7
    Nov 7th
    9 Files
  • 8
    Nov 8th
    8 Files
  • 9
    Nov 9th
    3 Files
  • 10
    Nov 10th
    0 Files
  • 11
    Nov 11th
    14 Files
  • 12
    Nov 12th
    20 Files
  • 13
    Nov 13th
    63 Files
  • 14
    Nov 14th
    18 Files
  • 15
    Nov 15th
    0 Files
  • 16
    Nov 16th
    0 Files
  • 17
    Nov 17th
    0 Files
  • 18
    Nov 18th
    0 Files
  • 19
    Nov 19th
    0 Files
  • 20
    Nov 20th
    0 Files
  • 21
    Nov 21st
    0 Files
  • 22
    Nov 22nd
    0 Files
  • 23
    Nov 23rd
    0 Files
  • 24
    Nov 24th
    0 Files
  • 25
    Nov 25th
    0 Files
  • 26
    Nov 26th
    0 Files
  • 27
    Nov 27th
    0 Files
  • 28
    Nov 28th
    0 Files
  • 29
    Nov 29th
    0 Files
  • 30
    Nov 30th
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2024 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close