The Joy ofProgrammingHow ‘C’mart Are You?                                                                                 ...
Upcoming SlideShare
Loading in …5

03 Jo P Mar 07


Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

03 Jo P Mar 07

  1. 1. The Joy ofProgrammingHow ‘C’mart Are You? S.G. GANESHObfuscating C CodeWikipedia defines ‘obfuscation’ as the concept of concealing the meaning of communicationby making it more confusing and harder to interpret. For programmers, C is a language ofchoice for writing obfuscated code for fun (mostly because of its curt syntax). This columncovers one such very simple C program and its obfuscated version to help beginners andnovices understand the process of writing and interpreting such programs.C ompile this program and run it by giving your name scanf, let the user give the name from the command line: as the argument. Now find out how the program works: const char * str = “Happy birthday to youn”; main(int argc, char *argv[]){ main(int i, char*a[]){char b[]={0x48,0x61,0x70,0x70,0x79,0x20, int i; 0x62,0x69,0x72,0x74,0x68,0x64,0x61,0x79,0x20,0x74,0x6f,0x20,0x79,0x6f, for(i=0; i<4; i++) printf((i==2) ? “%.15sdear %s n” : 0x75,0x0a};while(i+2) printf((!i—)?”%.15sdear %sn”:”%s”,b,1[a]);} “%s”, str, argv[1]); } Given your name, say, Bala, it wishes you happy birthday: GUEST COLUMN Happy birthday to you This is readable; now how can we make it little Happy birthday to you illegible? Happy birthday, dear Bala The string “Happy birthday to youn” is Happy birthday to you readable, so convert it into hexadecimal (in UNIX, That’s nice, isn’t it! you can probably use the octal dump tool, od, Instead of starting by dissecting the given obfuscated with the argument –x to print the hexa value).program, let me start from the original program and The expression a[1] is equivalent to 1[a], andexplain how one can arrive at the obfuscated code move i++ to the comparison expression i++<4(because that’s the way almost all obfuscated programs are and change the condition in printf as (i == 3).written). Proper white spaces and new-lines are evil Here is the original program: for unreadable code; so, don’t even leave a single space. For loop is somewhat predictable, const char * str = “Happy birthday to youn”; so replace it by while loop making use of the int main(){ fact that the argc value is 2 when passed with const char name[50]; an argument. Remove the return type of gets (name); main... printf(“ %s %s %.15s%s %s”, str, str, str, name, str); It’s possible to obfuscate more, but for the } purpose of explaining the process, this will do. Now, read the code given in the beginning of this The idea is to print the string “Happy birthday to article and you can understand how it works!youn” four times in the printf function. However, for thethird string, after printing “Happy birthday”, the name thatis given by the user has to be printed. For that, the format S.G. Ganesh is an engineer in Hewlett-Packard’s C++string %.15s prints the first 15 characters of the string str compiler team. He has authored a book “Deep C” (ISBN 81- 7656-501-6). He is also a member of the ANSI/ISO C++and with the following %s, the given name is printed. Standardization Committee (JTC1/SC22/WG21). You can Now, let’s make the program a little compact by moving reach him at comparison inside the printf function. Also, to avoid106 MARCH 2007 | LINUX FOR YOU | CMYK