Vous êtes sur la page 1sur 1

The Joy of

Programming
How ‘C’mart Are You?
S.G. GANESH

Obfuscating C Code
Wikipedia defines ‘obfuscation’ as the concept of concealing the meaning of communication
by making it more confusing and harder to interpret. For programmers, C is a language of
choice for writing obfuscated code for fun (mostly because of its curt syntax). This column
covers one such very simple C program and its obfuscated version to help beginners and
novices 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 you\n”;
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 %s\n”:”%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 you\n” 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], and
explain 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 you\n”; 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!
you\n” four times in the printf function. However, for the
third string, after printing “Happy birthday”, the name that
is 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 sgganesh@gmail.com.
the comparison inside the printf function. Also, to avoid

106 MARCH 2007 | LINUX FOR YOU | www.linuxforu.com

CMYK

Vous aimerez peut-être aussi