Posts‎ > ‎

Obfuscated Code

posted 7 Jul 2011, 12:18 by Rodrigo Caetano Rocha   [ updated 7 Jul 2011, 14:32 ]
Obfuscated code is a source code that has been modified to be difficult to be understood. It is a code whose logic is intentionally difficult to follow and whose syntax is intentionally unclear. There are several reasons for producing obfuscated code. These include intellectual property protection, program security or recreational challenge.

The following obfuscated code is a simple obfuscation based on macro definitions.
This source code, written in C, prints "Hello world!" on the screen.

#include "stdio.h"
#define _i int
#define _v void
#define _c char
#define _f float
#define _d double
#define _p *
#define o(_C_) putchar((char)_C_);
#define pf(_S_,_E_) printf(_S_,_E_);
#define ps(_S_) puts(_S_);
#define s(_S_) _S_;
#define m(_S_) int main() { _S_ return 0; }
#define f(_T_,_N_,_P_,_S_) _T_ _N_ _P_ {_S_}
#define c(_E_,_1_,_2_) if(_E_){_1_}else{_2_}
#define w(_E_,_S_) while(_E_){_S_}
#define v(_T_,_N_) _T_ _N_;
#define a(_N_,_E_) _N_=_E_;
#define b(_0_,_1_,_2_) ((_1_)_0_(_2_))
#define u(_0_,_1_) (_0_(_1_))
f(_i,S_,(_i _X_,_i _Y_),s(return b(+,_X_,_Y_)))f(_v,H_,(),o(0x48)o(0x65)o(0x6C)o(0x6C)o(0x6F)o(0x20)o(0x57)o(0x6F)o(0x72)o(0x6C)o(0x64)o(0x21))m(s(H_())o(S_(0x5,0x5)))

Obfuscation based on macro definitions can be reversed by executing the C preprocessor. After executing the preprocessor you will have a more understandable code, usually it won't be a fully understandable code but it will be easier to read.

Taking out the macro definitions and organising the code above we will get the following source code.

#include "stdio.h"

int S_ (int _X_,int _Y_) {
  return ((_X_)+(_Y_));
}

void H_(){
  putchar('H');
  putchar('e');
  putchar('l');
  putchar('l');
  putchar('o');
  putchar(' ');
  putchar('w');
  putchar('o');
  putchar('r');
  putchar('l');
  putchar('d');
  putchar('!');
}

int main() {
  H_();
  putchar( (char)S_(5,5) );
  return 0;
}

Another technique to obfuscate your code is to use meaningless and confusing names besides using a confusing and unreadable syntax.
The following obfuscated code makes use of this technique.
This source code, written in C, prints "Hello world!" on the screen.

#include "stdio.h"

void
__(
const char *
___,int _,int
O){
if(_ < O){putchar(*(___+_++));__(
___,_,O);}}main(){
const char _[
]={
'\x48'
,'\x65'
,
'\x6C','\x6C',
'\x6F','\x20'
,'\x57','\x6F','\x72','\x6C',
'\x64','\x21',
'\x0A'};
__(_,
0,13
);}

If we write the obfuscated code above using meaningful names and an organised syntax we get the following source code.

#include "stdio.h"

void myprint(const char *array, int a, int b){
  if(a < b) {
    putchar( *(array+a++) );
    myprint(array, a, b);
  }
}

main()
{
  const char array[] = { 'H', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!', '\n' };
   myprint(array, 0, 13);
}

For a more obfuscated code we can use both obfuscation techniques and some other approaches.

by rcor
Comments