C Code and the Art of Obfuscation

50 %
50 %
Information about C Code and the Art of Obfuscation

Published on September 23, 2007

Author: guest9006ab

Source: slideshare.net

Description

In the early days of computer science coding was viewed as an art. In the modern world of software engineering we may have lost the art to make way for rules and best practices. The International Obfuscated C Code Contest offers a chance for the coder to think beyond the rules of software engineering and unleash their creative side. We'll explore some of the more interesting entries in the past, take a closer look at some exotic C syntax, and finish up by exploring Bruce Holloway's 1986 entry.

From the Un-Distinguished Lecture Series (http://ws.cs.ubc.ca/~udls/). The talk was given Feb. 2, 2007

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)

  • Add a comment

    Related presentations

    Related pages

    The International Obfuscated C Code Contest

    The International Obfuscated C Code Contest [ The judges | IOCCC home page | How to enter | FAQ | Mirrors | ... -obfuscation n. obfuscatory adj. The IOCCC:
    Read more

    C/C++ Source Code Obfuscation - SourceFormat

    C/C++ Source Code Obfuscation Examples: ... Download SourceFormatX C/C++ Code Formatter to reformat and obfuscate all C and C++ source code files of your ...
    Read more

    C# Source Code Obfuscation - C Sharp

    SourceFormatX C# code formatting tool provides professional C Sharp code formatting solution and C# code ... C# Source Code Obfuscation ...
    Read more

    Obfuscation (software) - Wikipedia, the free encyclopedia

    ... the International Obfuscated C Code Contest, ... some developers may employ code obfuscation for the purpose of reducing file size or increasing ...
    Read more

    Obfuscating C/C++ Code - Stack Overflow

    Obfuscating C/C++ Code [closed] ... Tutorials for Code Obfuscation in C. 3 How to obfuscate lua code? 5 Source code obfuscation. 3
    Read more

    International Obfuscated C Code Contest - Wikipedia, the ...

    The International Obfuscated C Code ... after the manner of ASCII art. Preprocessor redefinitions to make code ... In the effort to take obfuscation ...
    Read more

    Obfuscated C Code - Computer & Information Science ...

    Obfuscated C Code Unless otherwise ... from the International Obfuscated C code contest. Most files contain an explanatory text file appended to the ANSI C ...
    Read more

    The art of obfuscation - On a Maze program

    The art of obfuscation. ... by Carl Shapiro, won the Grand Prize for most well-rounded in confusion of the 1985 International Obfuscated C Code Contest:
    Read more

    Obfuskation – Wikipedia

    Obfuskation verändert den ausführbaren Code eines Programms, ... Diese Art der Umgehung kommt vor, ... der International Obfuscated C Code Contest ...
    Read more