Skip to content

Instantly share code, notes, and snippets.

@deseven deseven/source_random.c
Last active Dec 24, 2017

What would you like to do?
Randomizing srcip PoC
Copyright (C) 2000 Daniel Ryde
Copyright (C) 2017 deseven <>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
Lesser General Public License for more details.
LD_PRELOAD library to make connect() to use a set of IP addresses
specified via the enviroment variable SRCADDRS.
Compile on Linux with:
gcc -nostartfiles -fpic -shared source_random.c -o -ldl -D_GNU_SOURCE
Example in bash:
SRCADDRS=",," LD_PRELOAD=./ curl url url url url
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <dlfcn.h>
#include <errno.h>
#include <time.h>
#include <arpa/inet.h>
int (*real_bind)(int, const struct sockaddr *, socklen_t);
int (*real_connect)(int, const struct sockaddr *, socklen_t);
char *src_addrs_env;
int num_addrs = 0;
char* src_addrs[254];
unsigned long int src_addr_saddr;
unsigned long int inaddr_any_saddr;
struct sockaddr_in local_sockaddr_in[] = { 0 };
void _init (void) {
const char *err;
real_bind = dlsym (RTLD_NEXT, "bind");
if ((err = dlerror ()) != NULL) {
fprintf (stderr, "dlsym (bind): %s\n", err);
real_connect = dlsym (RTLD_NEXT, "connect");
if ((err = dlerror ()) != NULL) {
fprintf (stderr, "dlsym (connect): %s\n", err);
inaddr_any_saddr = htonl (INADDR_ANY);
if (src_addrs_env = getenv ("SRCADDRS")) {
const char s[3] = ",; ";
char *token;
token = strtok(src_addrs_env,s);
while(token != NULL) {
src_addrs[num_addrs] = token;
token = strtok(NULL,s);
} else {
printf("no source addrs are defined\nset env variable SRCADDRS first\n");
int connect (int fd, const struct sockaddr *sk, socklen_t sl) {
static struct sockaddr_in *rsk_in;
int randomAddr = rand() % num_addrs;
rsk_in = (struct sockaddr_in *)sk;
/* printf("connect: %d %s:%d\n", fd, inet_ntoa (rsk_in->sin_addr.s_addr),
ntohs (rsk_in->sin_port));*/
src_addr_saddr = inet_addr (src_addrs[randomAddr]);
local_sockaddr_in->sin_family = AF_INET;
local_sockaddr_in->sin_addr.s_addr = src_addr_saddr;
local_sockaddr_in->sin_port = htons (0);
if ((rsk_in->sin_family == AF_INET)
&& (src_addrs)) {
real_bind (fd, (struct sockaddr *)local_sockaddr_in, sizeof (struct sockaddr));
return real_connect (fd, sk, sl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.