Your SlideShare is downloading. ×
C Code and the Art of Obfuscation
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

C Code and the Art of Obfuscation

3,464
views

Published on

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 …

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

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,464
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
107
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 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. 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
  • 18. 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!
  • 19. 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!
  • 20. 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 */ }
  • 21. 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-
  • 22. 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;);}
  • 23. Why We Love C ● Consider the following... #include <stdio.h> int main() { printf( “hello world!n” ); }
  • 24. Why We Love C ● This is equivalent... #include <stdio.h> int main( int argc, char **argv ) { printf( “hello world!n” ); }
  • 25. Why We Love C ● And so is this... #include <stdio.h> int main(int argc,char**argv){printf(“hello world!n”);}
  • 26. 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” ); }
  • 27. 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); } }
  • 28. 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); } }
  • 29. 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); } }
  • 30. 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))
  • 31. 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))
  • 32. 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 ); } } }
  • 33. 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 ); } } }
  • 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, ... }; 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 { ... } }
  • 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) 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 ); }
  • 37. 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
  • 38. String Building ● Need to generate “hello world!”
  • 39. String Building ● Need to generate “hello world!” ● But we also need a rn at the end. ● So we need “hello world!rn”
  • 40. 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 );
  • 41. 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”
  • 42. 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
  • 43. 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 );
  • 44. 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: ... } } }
  • 45. 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
  • 46. 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 ); }
  • 47. 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));
  • 48. 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?
  • 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)); ● 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
  • 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? 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) )
  • 51. Case 1 We'll see it later
  • 52. 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 ); }
  • 53. 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 );
  • 54. 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 );
  • 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 };
  • 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 }; ● Make a new tab2... int ltab2[]={ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43 };
  • 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 }; 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 );
  • 58. Case 2 long tab1[]={ 989L, 5L, 26L, 0L, 88319L, 123L, 0L, 9367L }; ● And tab1? ● Anything to do here?
  • 59. 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
  • 60. 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 );
  • 61. 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
  • 62. 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 ); }
  • 63. 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
  • 64. 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
  • 65. 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); } }
  • 66. 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
  • 67. 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
  • 68. 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)

×