Skip to content

Instantly share code, notes, and snippets.

@adisbladis
Created August 10, 2017 14:47
Show Gist options
  • Save adisbladis/dc8f2a0a8a5bb263ab849aaa87d01b36 to your computer and use it in GitHub Desktop.
Save adisbladis/dc8f2a0a8a5bb263ab849aaa87d01b36 to your computer and use it in GitHub Desktop.
An elemental ircd decloaker
/*
Made by Adam Hose <adis@blad.is>
Licensed under the GNU General Public License v2.0
Most code is straight up copied from elemental ircd
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define HOSTLEN 63
#define FNV1_32_INIT 0x811c9dc5UL
typedef uint32_t u_int32_t;
u_int32_t
fnv_hash(const unsigned char *s, int bits)
{
u_int32_t h = FNV1_32_INIT;
while (*s) {
h ^= *s++;
h += (h<<1) + (h<<4) + (h<<7) + (h << 8) + (h << 24);
}
if (bits < 32)
h = ((h >> bits) ^ h) & ((1<<bits)-1);
return h;
}
size_t
rb_strlcpy(char *dest, const char *src, size_t size)
{
size_t ret = strlen(src);
if(dest == src)
return ret;
if(size) {
size_t len = (ret >= size) ? size - 1 : ret;
memcpy(dest, src, len);
dest[len] = '\0';
}
return ret;
}
static void
do_host_cloak_ip(const char *inbuf, char *outbuf)
{
/* None of the characters in this table can be valid in an IP */
char chartable[] = "ghijklmnopqrstuvwxyz";
char *tptr;
uint32_t accum = fnv_hash((const unsigned char*) inbuf, 32);
int sepcount = 0;
int totalcount = 0;
rb_strlcpy(outbuf, inbuf, HOSTLEN + 1);
for (tptr = outbuf; *tptr != '\0'; tptr++) {
if (*tptr == '.') {
sepcount++;
continue;
}
if (sepcount < 2)
continue;
*tptr = chartable[(*tptr + accum) % 20];
accum = (accum << 1) | (accum >> 31);
}
}
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Elemental-IRCd ip decloaker\n");
printf("Usage: <ip_prefix> <cloaked>\n");
printf("Example: 139.59 139.59.xyz.xyz\n");
return 1;
}
char *ip_prefix = argv[1];
char *expected = argv[2];
char *ip;
char *mangledhost;
for(int i = 0; i <= 255; i++) {
for(int j = 0; j <= 255; j++) {
mangledhost = malloc(HOSTLEN + 1);
ip = malloc(HOSTLEN + 1);
sprintf(ip, "%s.%i.%i", ip_prefix, i, j);
do_host_cloak_ip(ip, mangledhost);
if(strcmp(mangledhost, expected) == 0) {
printf("Found: %s\n", ip);
break;
}
free(mangledhost);
free(ip);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment