SlideShare una empresa de Scribd logo
1 de 71
Descargar para leer sin conexión
C Code and the Art of
    Obfuscation

       Lloyd Markle
               nd
     February 2 , 2007
A Little background
●   What's C?
    –   C is a programming language
    –   Designed at AT&T Bell Labs
    –   Available on every platform
    –   The most popular language of all time?
    –   Kerningham & Ritchie published the first good
        reference (fondly referred to as K&R C)
A Little C
●   Some C Syntax
        int x, y;
        x = 5;
        y = x;
        printf( “x = %d, y = %dn”, x, y );

●   Some more...
        if( x > y ) {
           printf( “x is bigger than yn” );
        }
        else {
           printf( “x is not bigger than yn” );
        }

        int i;
        for( i = 0; i < 10; i++ ) {
           printf( “%dn”, i );
        }
A Little C Program
●   This...
#include <stdio.h>

int main() {
   printf( “hello world!n” );
}


●   Outputs this...


hello world!
More Syntax

     Syntax               Result
++i              i is increased by 1
i++              i is evaluated, then increased
i += 1           i=i+1
i -= 1           i=i–1
i *= 1           i=i*1
i<j              1 if i is greater than j
i>j              1 if i is greater than j
!( i < j )       0 if i is greater than j
Exotic Syntax




Okay, we've some of the basics...
       now for some fun!
Exotic Syntax 101
This is easy...
    int x, y;
    x = 5;
    y = ( x = 5 );




But what about this?
    int x;
    x = ( 5 == 5 );
Exotic Syntax 101
Consider these...
   int y;
   y = 5 == 4;



   int x, y;
   x = 5;
   y = ( x = 5 )++;



   int x, y, i;
   for( i = 0, x = 0, y = 5; x++ < --y; i++ )
      printf( “%dn”, i );
   printf( “%dn”, i );
Exotic Syntax: Bitwise Operations
●   Bitwise OR
       10 | 4 = ?
Exotic Syntax: Bitwise Operations
●   Bitwise OR
        10 | 4 = ?



●   Think like this...

                                      1010
         10                 1010    | 0100
       | 4                | 0100   -------
      -----              -------      1110
Exotic Syntax: Bitwise Operations
●   Bitwise AND
       10 | 4 = ?
          & 6
Exotic Syntax: Bitwise Operations
●   Bitwise AND
        10 | 4 = ?
           & 6



●   Similarly...

                                  1010
         10             1010    & 0110
       & 6            & 0110   -------
      -----          -------      0010
Exotic Syntax: Bitwise Operations
●   Shift left
        1 | 2
        10<< 4 = ?
Exotic Syntax: Bitwise Operations
●   Shift left
        10<< 4 = ?
        1 | 2



●   Think like this...


          0001 << 2      0100
Exotic Syntax: Bitwise Operations
●   Shift left (again...)
        10 | 43==??
           <<
Exotic Syntax: Bitwise Operations
●   Shift left (again...)
        10 | 43==??
           <<



●   Think like this...


          0000 1010 << 3    0101 0000
Obfuscation
●   What is obfuscation?
    –   “Obfuscate: tr.v. -cated, -cating, -cates. 1. a) To
        render obscure. b) To darken. 2. To confuse: his
        emotions obfuscated his judgment.” -- ioccc.org
    –   “Obfuscation refers to the concept of concealing the
        meaning of communication by making it more
        confusing and harder to interpret” -- wikipedia.org
Why Obfuscate?
●   Mainly to prevent reverse engineering
    –   Java or C# programs are easy to decompile
    –   Obfuscated code is difficult to read
    –   Should have no value to unauthorized users
●   Fun!
    –   The International Obfuscated C Code Contest
        (IOCCC) http://www.ioccc.org/
    –   After all, coding is an art!
How to Obfuscate
●   Take an easy task, make it hard!
●   Meaningless variable/function names
●   For the IOCCC you can try:
    –   Interesting code format
    –   Complex C syntax
●   In general be creative!
IOCCC Examples (omoikane 2006)
                     /*                                                 ,*/
                    #include                                         <time.h>
                     #include/*                              _ ,o*/ <stdlib.h>
                     #define c(C)/*            -        . */return           ( C); /*      2004*/
                       #include       <stdio.h>/*.      Moekan                quot;'    `b-'       */
                          typedef/* */char        p;p* u                             ,w          [9
                             ][128] ,*v;typedef int _;_           R,i,N,I,A                     ,m,o,e
                     [9], a[256],k          [9], n[                      256];FILE*f           ;_ x     (_ K,_ r
               ,_ q){;       for(;                                                    r<       q      ; K            =((
            0xffffff)       &(K>>8))^                                                    n[255        &            ( K
         ^u[0           +                                                r ++         ]       )]);c               (K
        )}          _ E                                   (p*r,       p*q ){         c(             f              =
                 fopen                            (r ,q))}_        B(_ q){c(       fseek            (f,          0
               ,q))}_ D(){c( fclose(f ))}_ C( p                  *q){c( 0-        puts(q       )    )}_/*      /
             */main(_ t,p**z){if(t<4)c(          C(quot;<inquot;           quot;file>quot;       quot;40<lquot;      quot;aquot; quot;yout> quot;
            /*b9213272*/quot;<outfile>quot;         ) )u=0;i=I=(E(z[1],quot;rbquot;)) ?B(2)?0 :              (((o     =ftell
           (f))>=8)?(u          =(p*)malloc(o))?B(0)?0:!fread(u,o,1,f):0:0)?0: D():0                      ;if(
         !u)c(C(quot;           bad40input quot;));if(E(z[2],quot;rbquot; )){for(N=-1;256> i;n[i++] =-1                    )a[
         i]=0;          for(i=I=0;       i<o&&(R =fgetc(         f))>-1;i++)++a[R] ?(R==N)?( ++I>7)?(n[
        N]+1         )?0:(n [N        ]=i-7):0:    (N=R)        |(I=1):0;A =-1;N=o+1;for(i=33;i<127;i++
        )(          n[i      ]+ 1&&N>a[i])?        N= a         [A=i]        :0;B(i=I=0);if(A+1)for(N=n[A];
      I<         8&&      (R =fgetc(f ))>         -1&& i      <o           ;i++)(i<N||i>N+7)?(R==A)?((*w[I
      ]                 =u [i])?1:(*w[I]=        46))?(a                   [I++]=i):0:0:0;D();}if(I<1)c(C(
                       quot; bad40laquot; quot;yout quot;))for(i                         =0;256>(R= i);n[i++]=R)for(A=8;
                     A >0;A --)         R = ( (R&1)==0)                  ?(unsigned int)R>>(01):((unsigned
                    /*kero Q'              ,KSS */)R>>                1)^           0xedb88320;m=a[I-1];a[I
                    ]=(m                <N)?(m=    N+8):             ++          m;for(i=00;i<I;e[i++]=0){
                  v=w               [i]+1;for(R                              =33;127 >R;R++)if(R-47&&R-92
                  &&            R-(_)* w[i])*(                           v++)=      (p)R;*v=0;}for(sprintf
                             /*'_ G*/ (*w+1,                       quot;%0quot;        quot;8xquot;,x(R=time(i=0),m,o)^~
                          0)     ;i<        8;++                i)u         [N+ i]=*(*w+i+1);for(*k=x(~
                             0,i=0       ,*a);i>-            1;         ){for (A=i;A<I;A++){u[+a [ A]
                          ]=w[A          ][e[A]] ;         k        [A+1]=x (k[A],a[A],a[A+1]
                       );}if         (R==k[I])        c(         (E(z[3 ],quot;wb+quot;))?fwrite(
                     /* */       u,o,1,f)?D              ()|C(quot; n          OK.quot;):0      :C(
                    quot; n WriteErrorquot;                      )) for (i =+I-
                  1 ;i >-1?!w[i][++                             e[+ i]]:0;
                     ) for( A=+i--;                          A<I;e[A++]
                     =0); (i <I-4                         )?putchar
                     ((_       ) 46)                       | fflush
                    /*'          ,*/                      ( stdout
                    ):           0&                        0;}c(C
                  (quot;            n                       failquot;)
                  )          /*                         dP' /
                            dP                        pd '
                          '                           zc
                                                     */
                                                   }
IOCCC Examples (westley 1990)

char*lie;
   double time, me= !0XFACE,
   not; int rested,   get, out;
   main(ly, die) char ly, **die ;{
       signed char lotte,

dear; (char)lotte--;
   for(get= !me;; not){
   1 - out & out ;lie;{
   char lotte, my= dear,
   **let= !!me *!not+ ++die;
       (char*)(lie=

quot;The gloves are OFF this time, I detest you, snotn0sed GEEK!quot;);
   do {not= *lie++ & 0xF00L* !me;
   #define love (char*)lie -
   love 1s *!(not= atoi(let
   [get -me?
       (char)lotte-
IOCCC Examples (tomx 2000)
#include <stdio.h>
#define true

true /*:all

CC=cc
PROG=tomx

false :
    make -f $0 $1
    exit 0

all: $(PROG)

%:%.c
    $(CC) $< -o $@

clean:
    rm $(PROG)

.PHONY: /* true clean */
    int main() {return!printf(quot;Hello, worldnquot;);}
Why We Love C
●   Consider the following...

#include <stdio.h>

int main() {
   printf( “hello world!n” );
}
Why We Love C
●   This is equivalent...

#include <stdio.h>

int main( int argc, char **argv ) {
   printf( “hello world!n” );
}
Why We Love C
●   And so is this...

#include <stdio.h>
int main(int argc,char**argv){printf(“hello world!n”);}
Why We Love C
●   And so is this...
#include <stdio.h>
#define WHY_I_LOVE_C “hello world!”

int main( int argc, char **argv ) {
   char *x = WHY_I_LOVE_C;
   int i;

     for( i = 0; i < 13; i++ )
        printf( “%c”, x[i] );
     printf( “n” );
}
Why We Love C
●   And so is this...
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))
long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L };
int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 };
main(m1,s) char *s; {
      int a,b,c,d,o[k],n=(int)s;
     if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); }
     else switch(m1-=h){
           case f:
                 a=(b=(c=(d=g)<<g)<<g)<<g;
                 return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
           case h:
                 for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a);
           case g:
                 if(n<h)return(g);
                 if(n<j){n-=g;c='D';o[f]=h;o[g]=f;}
                 else{c='r'-'b';n-=j-g;o[f]=o[g]=g;}
                 if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
                 return(o[b-g]%n+k-h);
           default:
                 if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f;
                 for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1);
     }
}
Why We Love C
●   And so is this... but how?
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))
long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L };
int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 };
main(m1,s) char *s; {
      int a,b,c,d,o[k],n=(int)s;
     if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); }
     else switch(m1-=h){
           case f:
                 a=(b=(c=(d=g)<<g)<<g)<<g;
                 return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
           case h:
                 for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a);
           case g:
                 if(n<h)return(g);
                 if(n<j){n-=g;c='D';o[f]=h;o[g]=f;}
                 else{c='r'-'b';n-=j-g;o[f]=o[g]=g;}
                 if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
                 return(o[b-g]%n+k-h);
           default:
                 if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f;
                 for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1);
     }
}
IOCCC Examples (holloway 1986)
●   Let's make it a little easier...
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))
long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L };
int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 };
main(m1,s) char *s; {
      int a,b,c,d,o[k],n=(int)s;
     if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); }
     else switch(m1-=h){
           case f:
                 a=(b=(c=(d=g)<<g)<<g)<<g;
                 return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
           case h:
                 for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a);
           case g:
                 if(n<h)return(g);
                 if(n<j){n-=g;c='D';o[f]=h;o[g]=f;}
                 else{c='r'-'b';n-=j-g;o[f]=o[g]=g;}
                 if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
                 return(o[b-g]%n+k-h);
           default:
                 if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f;
                 for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1);
     }
}
IOCCC Examples (holloway 1986)


/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))
IOCCC Examples (holloway 1986)


/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;
#define e 3
#define g 1
#define h 2
#define f 0
#define j 8
#define k 6
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))
IOCCC Examples (holloway 1986)
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;

#define l( x ) tab2[ x ] / 2
#define m( n, a ) ( ( n & (a) ) == (a) )

long tab1[] = { 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };

main( m1, s ) char *s; {
     int a, b, c, d, o[6], n = (int)s;

     if( m1 == 1 ) { char b[ 15 ]; main( l( 5 ) + 5, b ); printf( b );}
     else {
          switch ( m1 -= 2 ) {
                case 0:
                     a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1;
                     return ( m( n, a | c ) | m( n, b ) | m( n, a | d ) | m( n, c | d ) );

                case 2:
                     for( a = 0; a < 8; ++a )
                          if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a );

                case 1:
                     if( n < 2 ) return ( 1 );
                     if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; }
                     else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; }
                     if( ( b = n ) >= 3 )
                          for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;
                     return ( o[ b - 1] % n + 4 );

                default:
                     if( m1 -= 3 ) main( m1 + 4, s + 1 );
                     else *( s + 1 ) = 0;
                     for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );
          }
     }
}
IOCCC Examples (holloway 1986)
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;

#define l( x ) tab2[ x ] / 2
#define m( n, a ) ( ( n & (a) ) == (a) )

long tab1[] = { 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };

main( m1, s ) char *s; {
     int a, b, c, d, o[6], n = (int)s;

     if( m1 == 1 ) { char b[ 15 ]; main( l( 5 ) + 5, b ); printf( b );}
     else {
          switch ( m1 -= 2 ) {
                case 0:
                     a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1;
                     return ( m( n, a | c ) | m( n, b ) | m( n, a | d ) | m( n, c | d ) );

                case 2:
                     for( a = 0; a < 8; ++a )
                          if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a );

                case 1:
                     if( n < 2 ) return ( 1 );
                     if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; }
                     else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; }
                     if( ( b = n ) >= 3 )
                          for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;
                     return ( o[ b - 1] % n + 4 );

                default:
                     if( m1 -= 3 ) main( m1 + 4, s + 1 );
                     else *( s + 1 ) = 0;
                     for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );
          }
     }
}
IOCCC Examples (holloway 1986)
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;

#define l( x ) tab2[ x ] / 2
#define m( n, a ) ( ( n & (a) ) == (a) )

long tab1[] = { 989L, 5L, 26L, 0L, 88319L, ... };
int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, ... };

main( m1, s ) char *s; {
   int a, b, c, d, o[6], n = (int)s;

    if( m1 == 1 ) {
       char b[ 15 ];
       main( l( 5 ) + 5, b );
       printf( b );
    }
    else {
       ...
    }
}
IOCCC Examples (holloway 1986)
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;

#define l( x ) tab2[ x ] / 2
#define m( n, a ) ( ( n & (a) ) == (a) )

long tab1[] = { 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };

main( m1, s ) char *s; {
     int a, b, c, d, o[6], n = (int)s;

     if( m1 == 1 ) { char b[ 15 ]; main( l( 5 ) + 5, b ); printf( b );}
     else {
          switch ( m1 -= 2 ) {
                case 0:
                     a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1;
                     return ( m( n, a | c ) | m( n, b ) | m( n, a | d ) | m( n, c | d ) );

                case 2:
                     for( a = 0; a < 8; ++a )
                          if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a );

                case 1:
                     if( n < 2 ) return ( 1 );
                     if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; }
                     else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; }
                     if( ( b = n ) >= 3 )
                          for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;
                     return ( o[ b - 1] % n + 4 );

                default:
                     if( m1 -= 3 ) main( m1 + 4, s + 1 );
                     else *( s + 1 ) = 0;
                     for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );
          }
     }
}
IOCCC Examples (holloway 1986)
switch ( m1 -= 2 ) {
    case 0:
        a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1;
        return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) );

    case 2:
        for( a = 0; a < 8; ++a )
            if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) )
                return ( a );

    case 1:
        if( n < 2 ) return ( 1 );
        if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; }
        else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; }
        if( ( b = n ) >= 3 )
        for( b = 1 << 1; b < n; ++b )
            o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;
        return ( o[ b - 1] % n + 4 );

    default:
        if( m1 -= 3 )   main( m1 + 4, s + 1 );
        else *( s + 1   ) = 0;
        for( *s = a =   0; a < 3; )
            *s = ( *s   << 3 ) | main( 2 + a++, (char *)m1 );
}
IOCCC Examples (holloway 1986)
     default:
        if( m1 -= 3 )
           main( m1 + 4, s + 1 );
        else
           *( s + 1 ) = 0;

       for( *s = a = 0; a < 3; )
          *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );

●   Build string recursively
●   Each character built in three stages
String Building
●   Need to generate “hello world!”
String Building
●   Need to generate “hello world!”
●   But we also need a rn at the end.
●   So we need “hello world!rn”
String Building
●   Need to generate “hello world!”
●   But we also need a rn at the end.
●   So we need “hello world!rn”
●   But we're doing this recursively...
    default:
       if( m1 -= 3 )
          main( m1 + 4, s + 1 );
       else
          *( s + 1 ) = 0;

       for( *s = a = 0; a < 3; )
          *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );
String Building
●   Need to generate “hello world!”
●   But we also need a rn at the end.
●   So we need “hello world!rn”
●   But we're doing this recursively...
    default:
       if( m1 -= 3 )
          main( m1 + 4, s + 1 );
       else
          *( s + 1 ) = 0;

       for( *s = a = 0; a < 3; )
          *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );

●   We need “nr!dlrow olleh”
String Building
  n   ASCII   int      bin
  0     n     10   0001010
  1     r     13   0001101
  2      !     33   0100001
  3      d    100   1100100
  4      l    108   1101100
  5      r    114   1110010
  6      o    111   1101111
  7     w     119   1110111
  8            32   0100000
  9     o     111   1101111
 10     l     108   1101100
 11     l     108   1101100
 12     e     101   1100101
 13     h     104   1101000
Generating Binary Numbers
default:
   if( m1 -= 3 )
      main( m1 + 4, s + 1 );
   else
      *( s + 1 ) = 0;

  for( *s = a = 0; a < 3; )
     *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );
Generating Binary Numbers
   default:
      if( m1 -= 3 )
         main( m1 + 4, s + 1 );
      else
         *( s + 1 ) = 0;

      for( *s = a = 0; a < 3; )
         *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );


main( m1, s ) char *s; {
   ...
   if( m1 == 1 ) { ... }
   else {
       switch ( m1 -= 2 ) {
          case 0: ...
          case 1: ...
          case 2: ...
          default: ...
       }
   }
}
String Building
 n   ASCII   int      bin    Case 0 Case 1 Case 2
 0     n     10   0001010    000    001    010
 1    r      13   0001101    000    001    101
 2      !     33   0100001    000    100    001
 3      d    100   1100100    001    100    100
 4      l    108   1101100    001    101    100
 5      r    114   1110010    001    110    010
 6      o    111   1101111    001    101    111
 7     w     119   1110111    001    110    111
 8            32   0100000    000    100    000
 9     o     111   1101111    001    101    111
10     l     108   1101100    001    101    100
11     l     108   1101100    001    101    100
12     e     101   1100101    001    100    101
13     h     104   1101000    001    101    000
IOCCC Examples (holloway 1986)
switch ( m1 -= 2 ) {
    case 0:
        a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1;
        return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) );

    case 2:
        for( a = 0; a < 8; ++a )
            if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) )
                return ( a );

    case 1:
        if( n < 2 ) return ( 1 );
        if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; }
        else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; }
        if( ( b = n ) >= 3 )
        for( b = 1 << 1; b < n; ++b )
            o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;
        return ( o[ b - 1] % n + 4 );

    default:
        if( m1 -= 3 )   main( m1 + 4, s + 1 );
        else *( s + 1   ) = 0;
        for( *s = a =   0; a < 3; )
            *s = ( *s   << 3 ) | main( 2 + a++, (char *)m1 );
}
Case 0
case 0:
   a=(b=(c=(d=1)<<1)<<1)<<1;
   return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
Case 0
case 0:
   a=(b=(c=(d=1)<<1)<<1)<<1;
   return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));

●   What are a, b, c, and d?
Case 0
        case 0:
           a=(b=(c=(d=1)<<1)<<1)<<1;
           return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));

    ●    What are a, b, c, and d?
          a = 8, b = 4, c = 2, d = 1
    ●    Why all the bitwise ORs?
  n        0   1   2    3   4    5     6   7   8    9    10   11   12   13
 (n)2 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101
Case 0 0     0    0    1    1    1    1   1     0    1    1    1    1    1
Case 0
        case 0:
           a=(b=(c=(d=1)<<1)<<1)<<1;
           return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));

    ●    What are a, b, c, and d?
          a = 8, b = 4, c = 2, d = 1
    ●    Why all the bitwise ORs?
  n        0   1   2    3   4    5     6   7    8   9    10   11   12   13
 (n)2 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101
Case 0 0     0    0    1    1    1    1   1     0    1    1    1    1    1

    ●    What is m(x,y)?
        #define m(n,a) ( ( n & (a) ) == (a) )
Case 1




We'll see it later
IOCCC Examples (holloway 1986)
switch ( m1 -= 2 ) {
    case 0:
        a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1;
        return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) );

    case 2:
        for( a = 0; a < 8; ++a )
            if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) )
                return ( a );

    case 1:
        if( n < 2 ) return ( 1 );
        if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; }
        else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; }
        if( ( b = n ) >= 3 )
        for( b = 1 << 1; b < n; ++b )
            o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;
        return ( o[ b - 1] % n + 4 );

    default:
        if( m1 -= 3 )   main( m1 + 4, s + 1 );
        else *( s + 1   ) = 0;
        for( *s = a =   0; a < 3; )
            *s = ( *s   << 3 ) | main( 2 + a++, (char *)m1 );
}
Case 2
#define l(x) tab2[x] / 2
long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };

case 2:
   for( a = 0; a < 8; ++a )
       if( tab1[a] && !( tab1[a] % ((long)l( n ))) )
          return ( a );
Case 2
#define l(x) tab2[x] / 2
long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };

case 2:
   for( a = 0; a < 8; ++a )
       if( tab1[a] && !( tab1[a] % ((long)l( n ))) )
          return ( a );
Case 2
#define l(x) tab2[x] / 2

long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };
Case 2
#define l(x) tab2[x] / 2

long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };


●   Make a new tab2...

int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };
Case 2
#define l(x) tab2[x] / 2
long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };
int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };
int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };

case 2:
   for( a = 0; a < 8; ++a )
       if( tab1[a] && !( tab1[a] % ((long)ltab2[n])) )
          return ( a );
Case 2
long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };


●   And tab1?
●   Anything to do here?
Case 2
long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L };


●   And tab1?
●   Anything to do here?
    –   Notice:
         ●   23*43 = 989
         ●   2*13 = 26
         ●   7*11*31*37 = 88319
         ●   3*41 = 123
         ●   17*19*29 = 9367
Case 2
long tab1[]={ 23*43, 5, 2*13, 0, 7*11*31*37, 3*41, 0, 17*19*29 };
int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };

case 2:
   for( a = 0; a < 8; ++a )
       if( tab1[a] && !( tab1[a] % ((long)ltab2[n])) )
          return ( a );
Case 2
long tab1[]={ 23*43, 5, 2*13, 0, 7*11*31*37, 3*41, 0, 17*19*29 };
int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };

case 2:
   for( a = 0; a < 8; ++a )
       if( tab1[a] && !( tab1[a] % ((long)ltab2[n])) )
          return ( a );


●   Careful inspection and we see we get the
    result:
      n     0   1   2   3   4   5   6   7   8   9 10 11 12 13
    Case 2 010 101 001 100 100 010 111 111 000 111 100 100 101 000
    Case 2 2    5   1   4   4   2   7   7   0   7   4   4   5   0
IOCCC Examples (holloway 1986)
switch ( m1 -= 2 ) {
    case 0:
        a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1;
        return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) );

    case 2:
        for( a = 0; a < 8; ++a )
            if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) )
                return ( a );

    case 1:
        if( n < 2 ) return ( 1 );
        if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; }
        else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; }
        if( ( b = n ) >= 3 )
        for( b = 1 << 1; b < n; ++b )
            o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;
        return ( o[ b - 1] % n + 4 );

    default:
        if( m1 -= 3 )   main( m1 + 4, s + 1 );
        else *( s + 1   ) = 0;
        for( *s = a =   0; a < 3; )
            *s = ( *s   << 3 ) | main( 2 + a++, (char *)m1 );
}
Case 1
●   Is too hard...
case 1:                                              n   Case 1 Case 1
   if( n < 2 ) return ( 1 );                         0    001     1
     if( n < 8 )   {                                 1    001     1
        n -= 1;                                      2    100     4
        c = 'D';                                     3    100     4
        o[ 0 ] =   2;
        o[ 1 ] =   0;                                4    101     5
     }                                               5    110     6
     else {                                          6    101     5
        c = 'r'   - 'b';
        n -= 7;                                      7    110     6
        o[ 0 ] =   o[ 1 ] = 1;                       8    100     4
     }                                               9    101     5
     if( ( b = n ) >= 3 )                           10    101     5
        for( b = 1 << 1; b < n; ++b )               11    101     5
            o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;   12    100     4
     return ( o[ b - 1] % n + 4 );
                                                    13    101     5
Case 1
●   Is too hard...
case 1:                                              n   Case 1 Case 1
   if( n < 2 ) return ( 1 );                         0    001     1
     if( n < 8 )   {                                 1    001     1
        n -= 1;                                      2    100     4
        c = 'D';                                     3    100     4
        o[ 0 ] =   2;
        o[ 1 ] =   0;                                4    101     5
     }                                               5    110     6
     else {                                          6    101     5
        c = 'r'   - 'b';
        n -= 7;                                      7    110     6
        o[ 0 ] =   o[ 1 ] = 1;                       8    100     4
     }                                               9    101     5
     if( ( b = n ) >= 3 )                           10    101     5
        for( b = 1 << 1; b < n; ++b )               11    101     5
            o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c;   12    100     4
     return ( o[ b - 1] % n + 4 );
                                                    13    101     5
A Masterpiece: One Last Look
/* Program by Bruce Holloway, Digital Research */
#include quot;stdio.hquot;
#define e 3
#define g (e/e)
#define h ((g+e)/2)
#define f (e-g-h)
#define j (e*e-g)
#define k (j-h)
#define l(x) tab2[x]/h
#define m(n,a) ((n&(a))==(a))

long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L };
int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 };

main(m1,s) char *s; {
     int a,b,c,d,o[k],n=(int)s;

    if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); }
    else switch(m1-=h){
         case f:
              a=(b=(c=(d=g)<<g)<<g)<<g;
              return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
         case h:
              for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a);
         case g:
              if(n<h)return(g);
              if(n<j){n-=g;c='D';o[f]=h;o[g]=f;}
              else{c='r'-'b';n-=j-g;o[f]=o[g]=g;}
              if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c;
              return(o[b-g]%n+k-h);
         default:
              if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f;
              for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1);
    }
}
C Code and the Art of Obfuscation
●   It should remain an art
●   Do not use in practice
●   Please try it at home!
●   IOCCC submission deadline:
     February 28th, 2007
     Details at: www.ioccc.org
Credits and Acknowledgements
●   Art Contributions on slides 17, 20, 24, 49-52,
    53, 55-63, 68, and 69 thanks to Asbjorn Lonvig,
     (used without permission)
●   Special thanks to Bruce Holloway and the
    IOCCC
Credits and Acknowledgements
●   Image on slides 39-47 from www.websitehelper.com/ (used without
    permission)
●   Image on slide 1 from www.indeutsch.com (used without permission)
●   Image on slide 2 from www.onlineprintworks.com (used without permission)
●   Image on slide 3 from www.yourhome123.com (used without permission)
●   Image on slide 4 from www.purrfection.com (used without permission)
●   Image on slide 6 from www.spectris.com (used without permission)
●   Image on slides 7-16 from www.germes-online.com (used without
    permission)
●   Image on slide 18 from www.smartboxat.com (used without permission)

Más contenido relacionado

La actualidad más candente

Dart ( 1 )
Dart ( 1 )Dart ( 1 )
Dart ( 1 )
Simplife EG
 
C and its errors
C and its errorsC and its errors
C and its errors
Junaid Raja
 
Code obfuscation
Code obfuscationCode obfuscation
Code obfuscation
bijondesai
 

La actualidad más candente (9)

C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Dart ( 1 )
Dart ( 1 )Dart ( 1 )
Dart ( 1 )
 
Regular Expression (Regex) Fundamentals
Regular Expression (Regex) FundamentalsRegular Expression (Regex) Fundamentals
Regular Expression (Regex) Fundamentals
 
C and its errors
C and its errorsC and its errors
C and its errors
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
こんなRedmineは(個人的に)イヤだ! #RedmineOsaka
こんなRedmineは(個人的に)イヤだ! #RedmineOsakaこんなRedmineは(個人的に)イヤだ! #RedmineOsaka
こんなRedmineは(個人的に)イヤだ! #RedmineOsaka
 
Elements of programming
Elements of programmingElements of programming
Elements of programming
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Code obfuscation
Code obfuscationCode obfuscation
Code obfuscation
 

Destacado

JavaScript From Hell - CONFidence 2.0 2009
JavaScript From Hell - CONFidence 2.0 2009JavaScript From Hell - CONFidence 2.0 2009
JavaScript From Hell - CONFidence 2.0 2009
Mario Heiderich
 

Destacado (8)

JavaScript From Hell - CONFidence 2.0 2009
JavaScript From Hell - CONFidence 2.0 2009JavaScript From Hell - CONFidence 2.0 2009
JavaScript From Hell - CONFidence 2.0 2009
 
Modern türkçe dilbilgisi yüksel göknel 1974
Modern türkçe dilbilgisi yüksel göknel 1974Modern türkçe dilbilgisi yüksel göknel 1974
Modern türkçe dilbilgisi yüksel göknel 1974
 
Tersine Mühendislik 101
Tersine Mühendislik 101Tersine Mühendislik 101
Tersine Mühendislik 101
 
Test Mühendisliğine Giriş Eğitimi - Bölüm 1
Test Mühendisliğine Giriş Eğitimi - Bölüm 1Test Mühendisliğine Giriş Eğitimi - Bölüm 1
Test Mühendisliğine Giriş Eğitimi - Bölüm 1
 
03 Jo P Mar 07
03 Jo P Mar 0703 Jo P Mar 07
03 Jo P Mar 07
 
The project of meridian golf talya
The project of meridian golf talyaThe project of meridian golf talya
The project of meridian golf talya
 
PIC Programlama, Assembly ve Komutlar
PIC Programlama, Assembly ve KomutlarPIC Programlama, Assembly ve Komutlar
PIC Programlama, Assembly ve Komutlar
 
Talk Like TED: 3 Unbreakable Laws of Communication
Talk Like TED: 3 Unbreakable Laws of Communication Talk Like TED: 3 Unbreakable Laws of Communication
Talk Like TED: 3 Unbreakable Laws of Communication
 

Similar a C Code and the Art of Obfuscation

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
DOSONKA Group
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
aztack
 

Similar a C Code and the Art of Obfuscation (20)

TRICK
TRICKTRICK
TRICK
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
 
Vcs16
Vcs16Vcs16
Vcs16
 
Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語Haskellで学ぶ関数型言語
Haskellで学ぶ関数型言語
 
C program
C programC program
C program
 
Python 1 liners
Python 1 linersPython 1 liners
Python 1 liners
 
Prelude to halide_public
Prelude to halide_publicPrelude to halide_public
Prelude to halide_public
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
Abstracting Vector Architectures in Library Generators: Case Study Convolutio...
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
C programs
C programsC programs
C programs
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
 
Arduino coding class part ii
Arduino coding class part iiArduino coding class part ii
Arduino coding class part ii
 

Más de guest9006ab

Más de guest9006ab (14)

Proportional-Derivative-Integral (PID) Control
Proportional-Derivative-Integral (PID) ControlProportional-Derivative-Integral (PID) Control
Proportional-Derivative-Integral (PID) Control
 
A Brief History of Cryptography
A Brief History of CryptographyA Brief History of Cryptography
A Brief History of Cryptography
 
Eigenvalues in a Nutshell
Eigenvalues in a NutshellEigenvalues in a Nutshell
Eigenvalues in a Nutshell
 
SMART Goals and Feedback (Project Management)
SMART Goals and Feedback (Project Management)SMART Goals and Feedback (Project Management)
SMART Goals and Feedback (Project Management)
 
OS Mélange
OS MélangeOS Mélange
OS Mélange
 
Top Super-models of Computer Graphics
Top Super-models of Computer GraphicsTop Super-models of Computer Graphics
Top Super-models of Computer Graphics
 
Human Nature and its Consequences
Human Nature and its ConsequencesHuman Nature and its Consequences
Human Nature and its Consequences
 
Slipping the Surly Bonds
Slipping the Surly BondsSlipping the Surly Bonds
Slipping the Surly Bonds
 
Communication with Extraterrestrial Intelligence
Communication with Extraterrestrial IntelligenceCommunication with Extraterrestrial Intelligence
Communication with Extraterrestrial Intelligence
 
Canada vs. Germany: Une réflection
Canada vs. Germany: Une réflectionCanada vs. Germany: Une réflection
Canada vs. Germany: Une réflection
 
Signal Compression and JPEG
Signal Compression and JPEGSignal Compression and JPEG
Signal Compression and JPEG
 
Clouds
CloudsClouds
Clouds
 
Brain-Computer Interfaces
Brain-Computer InterfacesBrain-Computer Interfaces
Brain-Computer Interfaces
 
The ABC's of the Bible
The ABC's of the BibleThe ABC's of the Bible
The ABC's of the Bible
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

C Code and the Art of Obfuscation

  • 1. C Code and the Art of Obfuscation Lloyd Markle nd February 2 , 2007
  • 2. A Little background ● What's C? – C is a programming language – Designed at AT&T Bell Labs – Available on every platform – The most popular language of all time? – Kerningham & Ritchie published the first good reference (fondly referred to as K&R C)
  • 3. A Little C ● Some C Syntax int x, y; x = 5; y = x; printf( “x = %d, y = %dn”, x, y ); ● Some more... if( x > y ) { printf( “x is bigger than yn” ); } else { printf( “x is not bigger than yn” ); } int i; for( i = 0; i < 10; i++ ) { printf( “%dn”, i ); }
  • 4. A Little C Program ● This... #include <stdio.h> int main() { printf( “hello world!n” ); } ● Outputs this... hello world!
  • 5. More Syntax Syntax Result ++i i is increased by 1 i++ i is evaluated, then increased i += 1 i=i+1 i -= 1 i=i–1 i *= 1 i=i*1 i<j 1 if i is greater than j i>j 1 if i is greater than j !( i < j ) 0 if i is greater than j
  • 6. Exotic Syntax Okay, we've some of the basics... now for some fun!
  • 7. Exotic Syntax 101 This is easy... int x, y; x = 5; y = ( x = 5 ); But what about this? int x; x = ( 5 == 5 );
  • 8. Exotic Syntax 101 Consider these... int y; y = 5 == 4; int x, y; x = 5; y = ( x = 5 )++; int x, y, i; for( i = 0, x = 0, y = 5; x++ < --y; i++ ) printf( “%dn”, i ); printf( “%dn”, i );
  • 9. Exotic Syntax: Bitwise Operations ● Bitwise OR 10 | 4 = ?
  • 10. Exotic Syntax: Bitwise Operations ● Bitwise OR 10 | 4 = ? ● Think like this... 1010 10 1010 | 0100 | 4 | 0100 ------- ----- ------- 1110
  • 11. Exotic Syntax: Bitwise Operations ● Bitwise AND 10 | 4 = ? & 6
  • 12. Exotic Syntax: Bitwise Operations ● Bitwise AND 10 | 4 = ? & 6 ● Similarly... 1010 10 1010 & 0110 & 6 & 0110 ------- ----- ------- 0010
  • 13. Exotic Syntax: Bitwise Operations ● Shift left 1 | 2 10<< 4 = ?
  • 14. Exotic Syntax: Bitwise Operations ● Shift left 10<< 4 = ? 1 | 2 ● Think like this... 0001 << 2 0100
  • 15. Exotic Syntax: Bitwise Operations ● Shift left (again...) 10 | 43==?? <<
  • 16. Exotic Syntax: Bitwise Operations ● Shift left (again...) 10 | 43==?? << ● Think like this... 0000 1010 << 3 0101 0000
  • 17.
  • 18. Obfuscation ● What is obfuscation? – “Obfuscate: tr.v. -cated, -cating, -cates. 1. a) To render obscure. b) To darken. 2. To confuse: his emotions obfuscated his judgment.” -- ioccc.org – “Obfuscation refers to the concept of concealing the meaning of communication by making it more confusing and harder to interpret” -- wikipedia.org
  • 19. Why Obfuscate? ● Mainly to prevent reverse engineering – Java or C# programs are easy to decompile – Obfuscated code is difficult to read – Should have no value to unauthorized users ● Fun! – The International Obfuscated C Code Contest (IOCCC) http://www.ioccc.org/ – After all, coding is an art!
  • 20. How to Obfuscate ● Take an easy task, make it hard! ● Meaningless variable/function names ● For the IOCCC you can try: – Interesting code format – Complex C syntax ● In general be creative!
  • 21. IOCCC Examples (omoikane 2006) /* ,*/ #include <time.h> #include/* _ ,o*/ <stdlib.h> #define c(C)/* - . */return ( C); /* 2004*/ #include <stdio.h>/*. Moekan quot;' `b-' */ typedef/* */char p;p* u ,w [9 ][128] ,*v;typedef int _;_ R,i,N,I,A ,m,o,e [9], a[256],k [9], n[ 256];FILE*f ;_ x (_ K,_ r ,_ q){; for(; r< q ; K =(( 0xffffff) &(K>>8))^ n[255 & ( K ^u[0 + r ++ ] )]);c (K )} _ E (p*r, p*q ){ c( f = fopen (r ,q))}_ B(_ q){c( fseek (f, 0 ,q))}_ D(){c( fclose(f ))}_ C( p *q){c( 0- puts(q ) )}_/* / */main(_ t,p**z){if(t<4)c( C(quot;<inquot; quot;file>quot; quot;40<lquot; quot;aquot; quot;yout> quot; /*b9213272*/quot;<outfile>quot; ) )u=0;i=I=(E(z[1],quot;rbquot;)) ?B(2)?0 : (((o =ftell (f))>=8)?(u =(p*)malloc(o))?B(0)?0:!fread(u,o,1,f):0:0)?0: D():0 ;if( !u)c(C(quot; bad40input quot;));if(E(z[2],quot;rbquot; )){for(N=-1;256> i;n[i++] =-1 )a[ i]=0; for(i=I=0; i<o&&(R =fgetc( f))>-1;i++)++a[R] ?(R==N)?( ++I>7)?(n[ N]+1 )?0:(n [N ]=i-7):0: (N=R) |(I=1):0;A =-1;N=o+1;for(i=33;i<127;i++ )( n[i ]+ 1&&N>a[i])? N= a [A=i] :0;B(i=I=0);if(A+1)for(N=n[A]; I< 8&& (R =fgetc(f ))> -1&& i <o ;i++)(i<N||i>N+7)?(R==A)?((*w[I ] =u [i])?1:(*w[I]= 46))?(a [I++]=i):0:0:0;D();}if(I<1)c(C( quot; bad40laquot; quot;yout quot;))for(i =0;256>(R= i);n[i++]=R)for(A=8; A >0;A --) R = ( (R&1)==0) ?(unsigned int)R>>(01):((unsigned /*kero Q' ,KSS */)R>> 1)^ 0xedb88320;m=a[I-1];a[I ]=(m <N)?(m= N+8): ++ m;for(i=00;i<I;e[i++]=0){ v=w [i]+1;for(R =33;127 >R;R++)if(R-47&&R-92 && R-(_)* w[i])*( v++)= (p)R;*v=0;}for(sprintf /*'_ G*/ (*w+1, quot;%0quot; quot;8xquot;,x(R=time(i=0),m,o)^~ 0) ;i< 8;++ i)u [N+ i]=*(*w+i+1);for(*k=x(~ 0,i=0 ,*a);i>- 1; ){for (A=i;A<I;A++){u[+a [ A] ]=w[A ][e[A]] ; k [A+1]=x (k[A],a[A],a[A+1] );}if (R==k[I]) c( (E(z[3 ],quot;wb+quot;))?fwrite( /* */ u,o,1,f)?D ()|C(quot; n OK.quot;):0 :C( quot; n WriteErrorquot; )) for (i =+I- 1 ;i >-1?!w[i][++ e[+ i]]:0; ) for( A=+i--; A<I;e[A++] =0); (i <I-4 )?putchar ((_ ) 46) | fflush /*' ,*/ ( stdout ): 0& 0;}c(C (quot; n failquot;) ) /* dP' / dP pd ' ' zc */ }
  • 22. IOCCC Examples (westley 1990) char*lie; double time, me= !0XFACE, not; int rested, get, out; main(ly, die) char ly, **die ;{ signed char lotte, dear; (char)lotte--; for(get= !me;; not){ 1 - out & out ;lie;{ char lotte, my= dear, **let= !!me *!not+ ++die; (char*)(lie= quot;The gloves are OFF this time, I detest you, snotn0sed GEEK!quot;); do {not= *lie++ & 0xF00L* !me; #define love (char*)lie - love 1s *!(not= atoi(let [get -me? (char)lotte-
  • 23. IOCCC Examples (tomx 2000) #include <stdio.h> #define true true /*:all CC=cc PROG=tomx false : make -f $0 $1 exit 0 all: $(PROG) %:%.c $(CC) $< -o $@ clean: rm $(PROG) .PHONY: /* true clean */ int main() {return!printf(quot;Hello, worldnquot;);}
  • 24.
  • 25. Why We Love C ● Consider the following... #include <stdio.h> int main() { printf( “hello world!n” ); }
  • 26. Why We Love C ● This is equivalent... #include <stdio.h> int main( int argc, char **argv ) { printf( “hello world!n” ); }
  • 27. Why We Love C ● And so is this... #include <stdio.h> int main(int argc,char**argv){printf(“hello world!n”);}
  • 28. Why We Love C ● And so is this... #include <stdio.h> #define WHY_I_LOVE_C “hello world!” int main( int argc, char **argv ) { char *x = WHY_I_LOVE_C; int i; for( i = 0; i < 13; i++ ) printf( “%c”, x[i] ); printf( “n” ); }
  • 29. Why We Love C ● And so is this... /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define e 3 #define g (e/e) #define h ((g+e)/2) #define f (e-g-h) #define j (e*e-g) #define k (j-h) #define l(x) tab2[x]/h #define m(n,a) ((n&(a))==(a)) long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L }; int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 }; main(m1,s) char *s; { int a,b,c,d,o[k],n=(int)s; if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); } else switch(m1-=h){ case f: a=(b=(c=(d=g)<<g)<<g)<<g; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d)); case h: for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a); case g: if(n<h)return(g); if(n<j){n-=g;c='D';o[f]=h;o[g]=f;} else{c='r'-'b';n-=j-g;o[f]=o[g]=g;} if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c; return(o[b-g]%n+k-h); default: if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f; for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1); } }
  • 30. Why We Love C ● And so is this... but how? /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define e 3 #define g (e/e) #define h ((g+e)/2) #define f (e-g-h) #define j (e*e-g) #define k (j-h) #define l(x) tab2[x]/h #define m(n,a) ((n&(a))==(a)) long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L }; int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 }; main(m1,s) char *s; { int a,b,c,d,o[k],n=(int)s; if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); } else switch(m1-=h){ case f: a=(b=(c=(d=g)<<g)<<g)<<g; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d)); case h: for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a); case g: if(n<h)return(g); if(n<j){n-=g;c='D';o[f]=h;o[g]=f;} else{c='r'-'b';n-=j-g;o[f]=o[g]=g;} if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c; return(o[b-g]%n+k-h); default: if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f; for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1); } }
  • 31. IOCCC Examples (holloway 1986) ● Let's make it a little easier... /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define e 3 #define g (e/e) #define h ((g+e)/2) #define f (e-g-h) #define j (e*e-g) #define k (j-h) #define l(x) tab2[x]/h #define m(n,a) ((n&(a))==(a)) long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L }; int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 }; main(m1,s) char *s; { int a,b,c,d,o[k],n=(int)s; if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); } else switch(m1-=h){ case f: a=(b=(c=(d=g)<<g)<<g)<<g; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d)); case h: for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a); case g: if(n<h)return(g); if(n<j){n-=g;c='D';o[f]=h;o[g]=f;} else{c='r'-'b';n-=j-g;o[f]=o[g]=g;} if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c; return(o[b-g]%n+k-h); default: if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f; for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1); } }
  • 32. IOCCC Examples (holloway 1986) /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define e 3 #define g (e/e) #define h ((g+e)/2) #define f (e-g-h) #define j (e*e-g) #define k (j-h) #define l(x) tab2[x]/h #define m(n,a) ((n&(a))==(a))
  • 33. IOCCC Examples (holloway 1986) /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define e 3 #define g 1 #define h 2 #define f 0 #define j 8 #define k 6 #define l(x) tab2[x]/h #define m(n,a) ((n&(a))==(a))
  • 34. IOCCC Examples (holloway 1986) /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define l( x ) tab2[ x ] / 2 #define m( n, a ) ( ( n & (a) ) == (a) ) long tab1[] = { 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 }; main( m1, s ) char *s; { int a, b, c, d, o[6], n = (int)s; if( m1 == 1 ) { char b[ 15 ]; main( l( 5 ) + 5, b ); printf( b );} else { switch ( m1 -= 2 ) { case 0: a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1; return ( m( n, a | c ) | m( n, b ) | m( n, a | d ) | m( n, c | d ) ); case 2: for( a = 0; a < 8; ++a ) if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a ); case 1: if( n < 2 ) return ( 1 ); if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; } else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; } if( ( b = n ) >= 3 ) for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; return ( o[ b - 1] % n + 4 ); default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); } } }
  • 35. IOCCC Examples (holloway 1986) /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define l( x ) tab2[ x ] / 2 #define m( n, a ) ( ( n & (a) ) == (a) ) long tab1[] = { 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 }; main( m1, s ) char *s; { int a, b, c, d, o[6], n = (int)s; if( m1 == 1 ) { char b[ 15 ]; main( l( 5 ) + 5, b ); printf( b );} else { switch ( m1 -= 2 ) { case 0: a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1; return ( m( n, a | c ) | m( n, b ) | m( n, a | d ) | m( n, c | d ) ); case 2: for( a = 0; a < 8; ++a ) if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a ); case 1: if( n < 2 ) return ( 1 ); if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; } else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; } if( ( b = n ) >= 3 ) for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; return ( o[ b - 1] % n + 4 ); default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); } } }
  • 36. IOCCC Examples (holloway 1986) /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define l( x ) tab2[ x ] / 2 #define m( n, a ) ( ( n & (a) ) == (a) ) long tab1[] = { 989L, 5L, 26L, 0L, 88319L, ... }; int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, ... }; main( m1, s ) char *s; { int a, b, c, d, o[6], n = (int)s; if( m1 == 1 ) { char b[ 15 ]; main( l( 5 ) + 5, b ); printf( b ); } else { ... } }
  • 37. IOCCC Examples (holloway 1986) /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define l( x ) tab2[ x ] / 2 #define m( n, a ) ( ( n & (a) ) == (a) ) long tab1[] = { 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[] = { 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 }; main( m1, s ) char *s; { int a, b, c, d, o[6], n = (int)s; if( m1 == 1 ) { char b[ 15 ]; main( l( 5 ) + 5, b ); printf( b );} else { switch ( m1 -= 2 ) { case 0: a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1; return ( m( n, a | c ) | m( n, b ) | m( n, a | d ) | m( n, c | d ) ); case 2: for( a = 0; a < 8; ++a ) if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a ); case 1: if( n < 2 ) return ( 1 ); if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; } else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; } if( ( b = n ) >= 3 ) for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; return ( o[ b - 1] % n + 4 ); default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); } } }
  • 38. IOCCC Examples (holloway 1986) switch ( m1 -= 2 ) { case 0: a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1; return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) ); case 2: for( a = 0; a < 8; ++a ) if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a ); case 1: if( n < 2 ) return ( 1 ); if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; } else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; } if( ( b = n ) >= 3 ) for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; return ( o[ b - 1] % n + 4 ); default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); }
  • 39. IOCCC Examples (holloway 1986) default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); ● Build string recursively ● Each character built in three stages
  • 40. String Building ● Need to generate “hello world!”
  • 41. String Building ● Need to generate “hello world!” ● But we also need a rn at the end. ● So we need “hello world!rn”
  • 42. String Building ● Need to generate “hello world!” ● But we also need a rn at the end. ● So we need “hello world!rn” ● But we're doing this recursively... default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );
  • 43. String Building ● Need to generate “hello world!” ● But we also need a rn at the end. ● So we need “hello world!rn” ● But we're doing this recursively... default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); ● We need “nr!dlrow olleh”
  • 44. String Building n ASCII int bin 0 n 10 0001010 1 r 13 0001101 2 ! 33 0100001 3 d 100 1100100 4 l 108 1101100 5 r 114 1110010 6 o 111 1101111 7 w 119 1110111 8 32 0100000 9 o 111 1101111 10 l 108 1101100 11 l 108 1101100 12 e 101 1100101 13 h 104 1101000
  • 45. Generating Binary Numbers default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 );
  • 46. Generating Binary Numbers default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); main( m1, s ) char *s; { ... if( m1 == 1 ) { ... } else { switch ( m1 -= 2 ) { case 0: ... case 1: ... case 2: ... default: ... } } }
  • 47. String Building n ASCII int bin Case 0 Case 1 Case 2 0 n 10 0001010 000 001 010 1 r 13 0001101 000 001 101 2 ! 33 0100001 000 100 001 3 d 100 1100100 001 100 100 4 l 108 1101100 001 101 100 5 r 114 1110010 001 110 010 6 o 111 1101111 001 101 111 7 w 119 1110111 001 110 111 8 32 0100000 000 100 000 9 o 111 1101111 001 101 111 10 l 108 1101100 001 101 100 11 l 108 1101100 001 101 100 12 e 101 1100101 001 100 101 13 h 104 1101000 001 101 000
  • 48. IOCCC Examples (holloway 1986) switch ( m1 -= 2 ) { case 0: a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1; return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) ); case 2: for( a = 0; a < 8; ++a ) if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a ); case 1: if( n < 2 ) return ( 1 ); if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; } else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; } if( ( b = n ) >= 3 ) for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; return ( o[ b - 1] % n + 4 ); default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); }
  • 49. Case 0 case 0: a=(b=(c=(d=1)<<1)<<1)<<1; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d));
  • 50. Case 0 case 0: a=(b=(c=(d=1)<<1)<<1)<<1; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d)); ● What are a, b, c, and d?
  • 51. Case 0 case 0: a=(b=(c=(d=1)<<1)<<1)<<1; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d)); ● What are a, b, c, and d? a = 8, b = 4, c = 2, d = 1 ● Why all the bitwise ORs? n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 (n)2 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 Case 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1
  • 52. Case 0 case 0: a=(b=(c=(d=1)<<1)<<1)<<1; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d)); ● What are a, b, c, and d? a = 8, b = 4, c = 2, d = 1 ● Why all the bitwise ORs? n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 (n)2 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 Case 0 0 0 0 1 1 1 1 1 0 1 1 1 1 1 ● What is m(x,y)? #define m(n,a) ( ( n & (a) ) == (a) )
  • 53. Case 1 We'll see it later
  • 54. IOCCC Examples (holloway 1986) switch ( m1 -= 2 ) { case 0: a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1; return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) ); case 2: for( a = 0; a < 8; ++a ) if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a ); case 1: if( n < 2 ) return ( 1 ); if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; } else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; } if( ( b = n ) >= 3 ) for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; return ( o[ b - 1] % n + 4 ); default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); }
  • 55. Case 2 #define l(x) tab2[x] / 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 }; case 2: for( a = 0; a < 8; ++a ) if( tab1[a] && !( tab1[a] % ((long)l( n ))) ) return ( a );
  • 56. Case 2 #define l(x) tab2[x] / 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 }; case 2: for( a = 0; a < 8; ++a ) if( tab1[a] && !( tab1[a] % ((long)l( n ))) ) return ( a );
  • 57. Case 2 #define l(x) tab2[x] / 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 };
  • 58. Case 2 #define l(x) tab2[x] / 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 }; ● Make a new tab2... int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };
  • 59. Case 2 #define l(x) tab2[x] / 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; int tab2[]={ 4, 6, 10, 14, 22, 26, 34, 38, 46, 58, 62, 74, 82, 86 }; int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 }; case 2: for( a = 0; a < 8; ++a ) if( tab1[a] && !( tab1[a] % ((long)ltab2[n])) ) return ( a );
  • 60. Case 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; ● And tab1? ● Anything to do here?
  • 61. Case 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; ● And tab1? ● Anything to do here? – Notice: ● 23*43 = 989 ● 2*13 = 26 ● 7*11*31*37 = 88319 ● 3*41 = 123 ● 17*19*29 = 9367
  • 62. Case 2 long tab1[]={ 23*43, 5, 2*13, 0, 7*11*31*37, 3*41, 0, 17*19*29 }; int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 }; case 2: for( a = 0; a < 8; ++a ) if( tab1[a] && !( tab1[a] % ((long)ltab2[n])) ) return ( a );
  • 63. Case 2 long tab1[]={ 23*43, 5, 2*13, 0, 7*11*31*37, 3*41, 0, 17*19*29 }; int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 }; case 2: for( a = 0; a < 8; ++a ) if( tab1[a] && !( tab1[a] % ((long)ltab2[n])) ) return ( a ); ● Careful inspection and we see we get the result: n 0 1 2 3 4 5 6 7 8 9 10 11 12 13 Case 2 010 101 001 100 100 010 111 111 000 111 100 100 101 000 Case 2 2 5 1 4 4 2 7 7 0 7 4 4 5 0
  • 64. IOCCC Examples (holloway 1986) switch ( m1 -= 2 ) { case 0: a = ( b = ( c = ( d = 1 ) << 1 ) << 1 ) << 1; return ( m(n,a|c) | m(n,b) | m(n,a|d) | m(n,c|d) ); case 2: for( a = 0; a < 8; ++a ) if( tab1[ a ] && !( tab1[ a ] % ( (long)l( n ) ) ) ) return ( a ); case 1: if( n < 2 ) return ( 1 ); if( n < 8 ) { n -= 1; c = 'D'; o[ 0 ] = 2; o[ 1 ] = 0; } else { c = 'r' - 'b'; n -= 7; o[ 0 ] = o[ 1 ] = 1; } if( ( b = n ) >= 3 ) for( b = 1 << 1; b < n; ++b ) o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; return ( o[ b - 1] % n + 4 ); default: if( m1 -= 3 ) main( m1 + 4, s + 1 ); else *( s + 1 ) = 0; for( *s = a = 0; a < 3; ) *s = ( *s << 3 ) | main( 2 + a++, (char *)m1 ); }
  • 65. Case 1 ● Is too hard... case 1: n Case 1 Case 1 if( n < 2 ) return ( 1 ); 0 001 1 if( n < 8 ) { 1 001 1 n -= 1; 2 100 4 c = 'D'; 3 100 4 o[ 0 ] = 2; o[ 1 ] = 0; 4 101 5 } 5 110 6 else { 6 101 5 c = 'r' - 'b'; n -= 7; 7 110 6 o[ 0 ] = o[ 1 ] = 1; 8 100 4 } 9 101 5 if( ( b = n ) >= 3 ) 10 101 5 for( b = 1 << 1; b < n; ++b ) 11 101 5 o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; 12 100 4 return ( o[ b - 1] % n + 4 ); 13 101 5
  • 66. Case 1 ● Is too hard... case 1: n Case 1 Case 1 if( n < 2 ) return ( 1 ); 0 001 1 if( n < 8 ) { 1 001 1 n -= 1; 2 100 4 c = 'D'; 3 100 4 o[ 0 ] = 2; o[ 1 ] = 0; 4 101 5 } 5 110 6 else { 6 101 5 c = 'r' - 'b'; n -= 7; 7 110 6 o[ 0 ] = o[ 1 ] = 1; 8 100 4 } 9 101 5 if( ( b = n ) >= 3 ) 10 101 5 for( b = 1 << 1; b < n; ++b ) 11 101 5 o[ b ] = o[ b - 2 ] + o[ b - 1 ] + c; 12 100 4 return ( o[ b - 1] % n + 4 ); 13 101 5
  • 67. A Masterpiece: One Last Look /* Program by Bruce Holloway, Digital Research */ #include quot;stdio.hquot; #define e 3 #define g (e/e) #define h ((g+e)/2) #define f (e-g-h) #define j (e*e-g) #define k (j-h) #define l(x) tab2[x]/h #define m(n,a) ((n&(a))==(a)) long tab1[]={ 989L,5L,26L,0L,88319L,123L,0L,9367L }; int tab2[]={ 4,6,10,14,22,26,34,38,46,58,62,74,82,86 }; main(m1,s) char *s; { int a,b,c,d,o[k],n=(int)s; if(m1==1){ char b[2*j+f-g]; main(l(h+e)+h+e,b); printf(b); } else switch(m1-=h){ case f: a=(b=(c=(d=g)<<g)<<g)<<g; return(m(n,a|c)|m(n,b)|m(n,a|d)|m(n,c|d)); case h: for(a=f;a<j;++a)if(tab1[a]&&!(tab1[a]%((long)l(n))))return(a); case g: if(n<h)return(g); if(n<j){n-=g;c='D';o[f]=h;o[g]=f;} else{c='r'-'b';n-=j-g;o[f]=o[g]=g;} if((b=n)>=e)for(b=g<<g;b<n;++b)o[b]=o[b-h]+o[b-g]+c; return(o[b-g]%n+k-h); default: if(m1-=e) main(m1-g+e+h,s+g); else *(s+g)=f; for(*s=a=f;a<e;) *s=(*s<<e)|main(h+a++,(char *)m1); } }
  • 68.
  • 69. C Code and the Art of Obfuscation ● It should remain an art ● Do not use in practice ● Please try it at home! ● IOCCC submission deadline: February 28th, 2007 Details at: www.ioccc.org
  • 70. Credits and Acknowledgements ● Art Contributions on slides 17, 20, 24, 49-52, 53, 55-63, 68, and 69 thanks to Asbjorn Lonvig, (used without permission) ● Special thanks to Bruce Holloway and the IOCCC
  • 71. Credits and Acknowledgements ● Image on slides 39-47 from www.websitehelper.com/ (used without permission) ● Image on slide 1 from www.indeutsch.com (used without permission) ● Image on slide 2 from www.onlineprintworks.com (used without permission) ● Image on slide 3 from www.yourhome123.com (used without permission) ● Image on slide 4 from www.purrfection.com (used without permission) ● Image on slide 6 from www.spectris.com (used without permission) ● Image on slides 7-16 from www.germes-online.com (used without permission) ● Image on slide 18 from www.smartboxat.com (used without permission)