I see, learn and rediscover… everyday!
 
Obfuscate C Code – Part II

Obfuscate C Code – Part II

Hi all, this is my second post on C code obfuscation. You can check out all my posts on obfuscation here.

In this post, we’ll see two techniques which one can use in C code obfuscation. The more we apply these to our code, the more obfuscated our code will result in. And of course, this is not the complete guide for obfuscation. Just the two important techniques I find interesting. 🙂

Recursion
The first one is recursion. Yes recursion. 🙂 Well, before we start recursion we should know this. “To iterate is human, but to recurse is divine.” ;-). The great point about recursion is that it can make any simple code complicated. Before I explain what I’m going to do, let me post the code final code here.

T
=__LINE__;
#define FF T*T*T*T*T+T*T*T+T*T*T*T-T/T
char *___="He that falls in love with himself will have no rivals.";_$_(int _,int __){(__-_==(T/T)?printf("%c",*(___+_)):(_$_(_,(__-_)/T+_),_$_((__-_)/T+_, __)));}main(){_$_(T-T, FF);}

The code simply prints the quote “He that falls in love with himself will have no rivals.”, quoted by Benjamin Franklin.

So let us start with a simple code which does that.
#include <stdio.h>
int main() {
    printf("He that falls in love with himself will have no rivals.");
    return 0;
}

Now, let us see how to introduce recursion into this. You can say, to print a string, first print the first half of the string and then the second half of the string. Got it? Yes, it is like this

Print the string
    Print the first half of the string
        Print the first half of the first half of the string
        …
        …
        
        Print the second half of the first half of the string
        …
        ….
    Print the second half of the string
        Print the first half of the second half of the string
        …
        …
        
        Print the second half of the second half of the string
        …
        …

After we introduce recursion into this logic, we get a code similar to the following. Again, the code can be made further complicated. Just remember, there is no limit to obfuscation.

#include <stdio.h>
char *str = "He that falls in love with himself will have no rivals.";
void print(int start, int end) {
    if (end-start==1) {
        printf("%c", *(str+start));        
    }
    else {
        print(start, (end-start)/2+start);
        print((end-start)/2+start, end);
    }
}
int main() {
    print(0, 55);
    return 0;
}

Next, we remove the 2 in the else part of the code using #define. I use __LINE__ in this example. :). So now the code looks like this.

#include <stdio.h>
t=__LINE__;
char *str = "He that falls in love with himself will have no rivals.";
void print(int start, int end) {
    if (end-start==1) {
        printf("%c", *(str+start));        
    }
    else {
        print(start, (end-start)/t+start);
        print((end-start)/t+start, end);
    }
}
int main() {
    print(0, 55);
    return 0;
}

Next, we remove the if else in the print statement and have a single conditional statement. That changes the whole picture in the print function, as shown below.

#include <stdio.h>
t=__LINE__;
char *str = "He that falls in love with himself will have no rivals.";
void print(int start, int end) { (end-start==1?printf("%c", *(str+start)):(print(start, (end-start)/t+start),print((end-start)/t+start, end))); }
int main() {
    print(0, 55);
    return 0;
}

Finally we change all the variable names so that they have only _’s and remove unnecessary space. Also you can change the value 55 to as 32+16+8-1 (all of these generated only using the previously used 2). So this is the final code. 🙂

T
=__LINE__;
#define FF T*T*T*T*T+T*T*T+T*T*T*T-T/T
char *___ = "He that falls in love with himself will have no rivals.";_$_(int _,int __){(__-_==1?printf("%c", *(___+_)):(_$_(_,(__-_)/T+_),_$_((__-_)/T+_, __)));}main(){_$_(T-T, FF);}

Preprocessor Statements
The second technique I find interesting is using #ifdef, #else, #endif and #include. You can make any code obfuscated without much programming.

Again, let us take the same simple code to explain the usage of preprocessor statements. Though it is much more effective when you apply this to an obfuscated code.

So the sample code with which we start will be

#include <stdio.h>
int main() {
    printf("He that falls in love with himself will have no rivals.");
    return 0;
}

The resultant obfuscated code will be


#ifdef T1
    #ifdef T2
        #ifdef T3
            #ifdef T4
                }    //Line 5
            #else
                #define T4
                return 0;    //Line 4
                #include __FILE__
            #endif        
        #else
            #define T3
            printf("He that falls in love with himself will have no rivals.");    //Line 3
            #include __FILE__
        #endif
    #else
        #define T2
            int main() { //Line 2
        #include __FILE__
                
    #endif
#else
    #define T1
        #include <stdio.h> //Line 1
    #include __FILE__
#endif

I guess I can’t explain much on this. This is one simple concept in which preprocessor complies selected lines and then the same file is included again. 🙂 If the above code is confusing, read this article on preprocessing and then try some simple code using this idea. Then this code will be a cakewalk.

You can download all the C files as a single zip file here.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.