Skip to content

Instantly share code, notes, and snippets.

@wuub
Last active December 25, 2015 20:59
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wuub/7039758 to your computer and use it in GitHub Desktop.
Save wuub/7039758 to your computer and use it in GitHub Desktop.
LD_PRELOAD hook for vaurien proof of concept
  1. compile shared library:
$ cc -shared -fPIC -Wl,--no-as-needed -ldl -lpthread -Wl,-soname=forward.so -o forward.so forward.c
  1. Run standard redis server in separate terminal
$ redis-server
  1. Start vaurien with 10000 port offset
$ vaurien --proxy 127.0.0.1:16379 --backend 127.0.0.1:6379 --protocol redis --behavior 100:delay
  1. Test code without vaurien
$ python test.py 
12
0.00104689598083
  1. Test code WITH vaurien but without changing any configuration
LD_PRELOAD=./forward.so python test.py 
Proxy: 6379 -> 16379
Proxy: 6379 -> 16379
12
2.00729990005
  1. Don't use it in production :-)

KUDOS and inspiration: proxychains library

#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
typedef int (*connect_t)(int, const struct sockaddr *, socklen_t);
int connect(int sock, const struct sockaddr *addr, socklen_t len) {
struct sockaddr_in* addrin = ((struct sockaddr_in *) addr);
int orig_port = ntohs(addrin->sin_port);
addrin->sin_port = htons(ntohs(addrin->sin_port) + 10000);
printf("Proxy: %d -> %d\n", orig_port, ntohs(((struct sockaddr_in *) addr)->sin_port));
connect_t true_connect = dlsym(RTLD_NEXT, "connect");
return true_connect(sock, addr, len);
}
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement, division, absolute_import, print_function
import redis
import time
red = redis.StrictRedis()
start = time.time()
red.set("hi", 12)
print(red.get("hi"))
print (time.time() - start)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment