1 (edited by xush 2016-01-02 10:14:28)

Topic: DDNet's great obfuscation

Because DDNet. Have fun.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

ssize_t getdelim(char **linep, size_t *n, int delim, FILE *fp){
    int ch;
    size_t i = 0;
    if(!linep || !n || !fp){
        errno = EINVAL;
        return -1;
    }
    if(*linep == NULL){
        if(NULL==(*linep = malloc(*n=128))){
            *n = 0;
            errno = ENOMEM;
            return -1;
        }
    }
    while((ch = fgetc(fp)) != EOF){
        if(i + 1 >= *n){
            char *temp = realloc(*linep, *n + 128);
            if(!temp){
                errno = ENOMEM;
                return -1;
            }
            *n += 128;
            *linep = temp;
        }
        (*linep)[i++] = ch;
        if(ch == delim)
            break;
    }
    (*linep)[i] = '\0';
    return !i && ch == EOF ? -1 : i;
}
ssize_t getline(char **linep, size_t *n, FILE *fp){
    return getdelim(linep, n, '\n', fp);
}

inline int ms_rand(int *seed)
{
    *seed = *seed*0x343fd+0x269EC3;  // a=214013, b=2531011
    return (*seed >> 0x10) & 0x7FFF;
}

int main()
{
    int seed = 0;
    int tmpseed = 0;
    int port = 0;
    int result = 0;
    int i = 0;

    FILE *ips = fopen("ips.txt", "r");
    if(ips == NULL)
    {
        printf("error opening ips.txt");
        return 0;
    }

    FILE *fix = fopen("ipsfix.txt", "w");
    if(fix == NULL)
    {
        printf("error opening ipsfix.txt");
        return 0;
    }

    char * line = NULL;
    size_t len = 0;
    char *cip;
    char *cport;

    while (getline(&line, &len, ips) != -1)
    {
        cip = strtok(line, ":");
        cport = strtok(NULL, ":");

        port = atoi(cport);
        seed = time(NULL);
        for(i = 0;i < 6*60*60; i++)
        {
            tmpseed = seed;
            result = (ms_rand(&tmpseed) % 64511) + 1024;
            ms_rand(&tmpseed);
            if(((ms_rand(&tmpseed) % 64511) + 1024) == port)
            {
                break;
            }
            result = 0;
            seed--;
        }

        if(!result)
            fprintf (fix, "%s:%s", cip, cport);
        else
            fprintf (fix, "%s:%d\n", cip, result);
    }

    fclose(ips);
    fclose(fix);

    if(!remove("ips.txt"))
    {
        printf("File deleted successfully\n");
    }
    else
    {
        printf("Error: unable to delete the file\n");
    }

    if(!rename("ipsfix.txt", "ips.txt"))
    {
        printf("File renamed successfully\n");
    }
    else
    {
        printf("Error: unable to rename the file\n");
    }
    return 0;
}
Real programmers don't comment their code - it was hard to write, it should be hard to understand.
Proudly verkeckt since 2010.

2 (edited by Henningstone 2016-01-02 17:00:58)

Re: DDNet's great obfuscation

Because no errors. ^^ // EDIT: another fixes and nice error messages wink

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

ssize_t getdelim(char **linep, size_t *n, int delim, FILE *fp)
{
    int ch;
    size_t i = 0;
    if(!linep || !n || !fp)
    {
        errno = EINVAL;
        return -1;
    }
    if(*linep == NULL && ((*linep = (char*)malloc(*n=128)) == NULL))
    {
        *n = 0;
        errno = ENOMEM;
        return -1;
    }
    while((ch = fgetc(fp)) != EOF)
    {
        if(i + 1 >= *n)
        {
            char *temp = (char*)realloc(*linep, *n + 128);
            if(!temp)
            {
                errno = ENOMEM;
                return -1;
            }
            *n += 128;
            *linep = temp;
        }
        (*linep)[i++] = ch;
        if(ch == delim)
            break;
    }
    (*linep)[i] = '\0';
    return !i && ch == EOF ? -1 : i;
}
ssize_t getline(char **linep, size_t *n, FILE *fp)
{
    return getdelim(linep, n, '\n', fp);
}

inline int ms_rand(int *seed)
{
    *seed = *seed*0x343fd+0x269EC3;  // a=214013, b=2531011
    return (*seed >> 0x10) & 0x7FFF;
}

int main()
{
    int seed = 0;
    int tmpseed = 0;
    int port = 0;
    int result = 0;
    int i = 0;

    FILE *ips = fopen("ips.txt", "r");
    if(ips == NULL)
    {
        printf("error opening ips.txt\n");
        return 1;
    }

    FILE *fix = fopen("ipsfix.txt", "w");
    if(fix == NULL)
    {
        printf("error opening ipsfix.txt\n");
        return 1;
    }

    char *line = NULL;
    size_t len = 0;
    char *cip;
    char *cport;

    while (getline(&line, &len, ips) != -1)
    {
        cip = strtok(line, ":");
        cport = strtok(NULL, ":");
        
        if(!cport)
        {
            printf("no port defined for IP %s", line);
            continue;
        }
        
        port = atoi(cport);
        seed = time(NULL);
        for(i = 0;i < 6*60*60; i++)
        {
            tmpseed = seed;
            result = (ms_rand(&tmpseed) % 64511) + 1024;
            ms_rand(&tmpseed);
            if(((ms_rand(&tmpseed) % 64511) + 1024) == port)
            {
                break;
            }
            result = 0;
            seed--;
        }

        if(!result)
            fprintf (fix, "%s:%s", cip, cport);
        else
            fprintf (fix, "%s:%d\n", cip, result);
    }

    fclose(ips);
    fclose(fix);

    if(!remove("ips.txt"))
    {
        printf("File 'ips.txt' deleted successfully\n");
    }
    else
    {
        printf("Error: unable to delete file 'ips.txt'\n");
    }

    if(!rename("ipsfix.txt", "ips.txt"))
    {
        printf("File 'ipsfix.txt' successfully renamed to 'ips.txt'\n");
    }
    else
    {
        printf("Error: unable to rename file 'ipsfix.txt' to 'ips.txt'\n");
    }
    return 0;
}

// EDIT2: Ok, nothing to do, here you go. Also a cli version for processing a bunch of files + more user friendly tongue
PS: BE CAREFUL WITH WILDCARDS, AS USUAL: Think before you type

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>

ssize_t getdelim(char **linep, size_t *n, int delim, FILE *fp)
{
    int ch;
    size_t i = 0;
    if(!linep || !n || !fp)
    {
        errno = EINVAL;
        return -1;
    }
    if(*linep == NULL && ((*linep = (char*)malloc(*n=128)) == NULL))
    {
        *n = 0;
        errno = ENOMEM;
        return -1;
    }
    while((ch = fgetc(fp)) != EOF)
    {
        if(i + 1 >= *n)
        {
            char *temp = (char*)realloc(*linep, *n + 128);
            if(!temp)
            {
                errno = ENOMEM;
                return -1;
            }
            *n += 128;
            *linep = temp;
        }
        (*linep)[i++] = ch;
        if(ch == delim)
            break;
    }
    (*linep)[i] = '\0';
    return !i && ch == EOF ? -1 : i;
}
ssize_t getline(char **linep, size_t *n, FILE *fp)
{
    return getdelim(linep, n, '\n', fp);
}

inline int ms_rand(int *seed)
{
    *seed = *seed*0x343fd+0x269EC3;  // a=214013, b=2531011
    return (*seed >> 0x10) & 0x7FFF;
}

int main(int argc, char **argv)
{
    if(argc == 1)
        printf("Usage: %s [file1] [file2] ... [filen]", argv[0]);

    for(int argn = 1; argn < argc; argn++)
    {
        const char *filename;
        if(!filename) filename = argv[1];
        
        printf("Starting Job for file '%s'\n", filename);

        int seed = 0;
        int tmpseed = 0;
        int port = 0;
        int result = 0;
        int i = 0;

        FILE *ips = fopen(filename, "r");
        if(ips == NULL)
        {
            printf("error opening %s\n", filename);
            return 1;
        }

        FILE *fix = fopen("ipsfix.txt", "w");
        if(fix == NULL)
        {
            printf("error opening ipsfix.txt\n");
            return 1;
        }

        char *line = NULL;
        size_t len = 0;
        char *cip;
        char *cport;

        while (getline(&line, &len, ips) != -1)
        {
            cip = strtok(line, ":");
            cport = strtok(NULL, ":");
    
            if(!cport)
            {
                printf("no port defined for IP %s", line);
                continue;
            }
    
            port = atoi(cport);
            seed = time(NULL);
            for(i = 0;i < 6*60*60; i++)
            {
                tmpseed = seed;
                result = (ms_rand(&tmpseed) % 64511) + 1024;
                ms_rand(&tmpseed);
                if(((ms_rand(&tmpseed) % 64511) + 1024) == port)
                    break;

                result = 0;
                seed--;
            }

            if(!result)
            {
                //printf("result:0 - %s:%s", cip, cport);
                fprintf (fix, "%s:%s", cip, cport);
            }
            else
            {
                //printf("result:%i - %s:%s", result, cip, cport);
                fprintf (fix, "%s:%d\n", cip, result);
            }
        }
        printf("EOF reached, file '%s' done.\n", filename);

        fclose(ips);
        fclose(fix);

        if(!remove(filename))
        {
            printf("File '%s' deleted successfully\n", filename);
        }
        else
        {
            printf("Error: unable to delete file '%s'\n", filename);
        }

        if(!rename("ipsfix.txt", filename))
        {
            printf("File 'ipsfix.txt' successfully renamed to '%s'\n\n", filename);
        }
        else
        {
            printf("Error: unable to rename file 'ipsfix.txt' to '%s'\n\n", filename);
        }
    }
    return 0;
}

3

Re: DDNet's great obfuscation

It is not meant to be user friendly :b

Real programmers don't comment their code - it was hard to write, it should be hard to understand.
Proudly verkeckt since 2010.

4

Re: DDNet's great obfuscation

I had nothing to do, and then I saw you post. Making it user friendly and fixing it's bugs was just a side effect of reading the code ^^ Good job anyway I'd say! cool

5

Re: DDNet's great obfuscation

I am not sure what is going on here. Did xush obfuscate or partially deobfuscate?

Clan: Riot (I'm one of three leaders: Mile, Deku, pie)
Host teeworlds maps on a fng/ctf/dm/ddrace server for testing:http://riotproductions.tk/teewo/ broken-need reinstall nginx http://riotproductions.tk/bounce?whatEven, Teeworlds NA Discord chat

6

Re: DDNet's great obfuscation

You must go to your very own center to unleash the power and the force of this.

Real programmers don't comment their code - it was hard to write, it should be hard to understand.
Proudly verkeckt since 2010.

7 (edited by Deepfinder 2016-06-03 16:26:00)

Re: DDNet's great obfuscation

what is that? ^-^

I will be banned if I troll again ...

8

Re: DDNet's great obfuscation

hello can u answer me? devs? this is offical forum, so why no answer me?
WHAT IS THAT?

I will be banned if I troll again ...

9

Re: DDNet's great obfuscation

If you can't understand this, it's not meant for you. Outdated anyways, got fixed right after I released it.

Real programmers don't comment their code - it was hard to write, it should be hard to understand.
Proudly verkeckt since 2010.

10

Re: DDNet's great obfuscation

It's a rigmarole

11

Re: DDNet's great obfuscation

One dev that knows that this source do? Only xush?

12

Re: DDNet's great obfuscation

A look at this here might help, you will also find some familiar numbers there: https://en.wikipedia.org/wiki/Linear_co … _generator

Luck is allowed

13

Re: DDNet's great obfuscation

HMH wrote:

A look at this here might help, you will also find some familiar numbers there: https://en.wikipedia.org/wiki/Linear_co … _generator

thanks!

I will be banned if I troll again ...

14

Re: DDNet's great obfuscation

HMH wrote:

A look at this here might help, you will also find some familiar numbers there: https://en.wikipedia.org/wiki/Linear_co … _generator

Thx! My skills are limited wink

Ok, He "crack" the port generation... and? It was useful for spoofing?

15

Re: DDNet's great obfuscation

unsigned char* wrote:

Ok, He "crack" the port generation... and? It was useful for spoofing?

Yes, the port randomization was a primitive solution to prevent packet injection via ip spoofing. Back then the DDNet protocol wasn't tokenized (see the patch I applied to your HClient last year).

Source port randomization is still pretty important though, e.g. there are DDoS mitigation systems that track game connections using source and destination ips/ports, allowing them to filter not game-related traffic.
So, the source port can also be seen like a secret token for mitigation systems. If it's known, one can bypass the mitigation system with ease.

16

Re: DDNet's great obfuscation

east wrote:
unsigned char* wrote:

Ok, He "crack" the port generation... and? It was useful for spoofing?

Yes, the port randomization was a primitive solution to prevent packet injection via ip spoofing. Back then the DDNet protocol wasn't tokenized (see the patch I applied to your HClient last year).

Source port randomization is still pretty important though, e.g. there are DDoS mitigation systems that track game connections using source and destination ips/ports, allowing them to filter not game-related traffic.
So, the source port can also be seen like a secret token for mitigation systems. If it's known, one can bypass the mitigation system with ease.

Thx east!! and yes, i known the problem but not know how xploit it wink I never use spoofing or ddos attacks... I work with ERP's.. not with security and hacking wink

Now i understand good... and the world continues to exist...
P.S: Put stones on the road, for me, is a harmful practice.

Cheers!

17

Re: DDNet's great obfuscation

How is oe harmful? We are speaking hypothetically.

Real programmers don't comment their code - it was hard to write, it should be hard to understand.
Proudly verkeckt since 2010.

18

Re: DDNet's great obfuscation

xush wrote:

How is oe harmful? We are speaking hypothetically.

The stones = no comments wink