SlideShare ist ein Scribd-Unternehmen logo
1 von 1
Downloaden Sie, um offline zu lesen
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 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], 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 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 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

Weitere ähnliche Inhalte

Ähnlich wie 03 Jo P Mar 07

Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
Ganesh Samarthyam
 

Ähnlich wie 03 Jo P Mar 07 (20)

2 data and c
2 data and c2 data and c
2 data and c
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
14 Jo P Feb 08
14 Jo P Feb 0814 Jo P Feb 08
14 Jo P Feb 08
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.Wade not in unknown waters. Part two.
Wade not in unknown waters. Part two.
 
C Programming
C ProgrammingC Programming
C Programming
 
Csharp_Chap06
Csharp_Chap06Csharp_Chap06
Csharp_Chap06
 
Fundamental of C Programming Language and Basic Input/Output Function
  Fundamental of C Programming Language and Basic Input/Output Function  Fundamental of C Programming Language and Basic Input/Output Function
Fundamental of C Programming Language and Basic Input/Output Function
 
Basic Input and Output
Basic Input and OutputBasic Input and Output
Basic Input and Output
 
Strings
StringsStrings
Strings
 
07 Jo P Jul 07
07 Jo P Jul 0707 Jo P Jul 07
07 Jo P Jul 07
 
3 character strings and formatted input output
3  character strings and formatted input output3  character strings and formatted input output
3 character strings and formatted input output
 
comp2
comp2comp2
comp2
 
C string
C stringC string
C string
 
Python
PythonPython
Python
 
14 strings
14 strings14 strings
14 strings
 
Writing Efficient Code Feb 08
Writing Efficient Code Feb 08Writing Efficient Code Feb 08
Writing Efficient Code Feb 08
 

Mehr von Ganesh Samarthyam

Mehr von Ganesh Samarthyam (20)

Wonders of the Sea
Wonders of the SeaWonders of the Sea
Wonders of the Sea
 
Animals - for kids
Animals - for kids Animals - for kids
Animals - for kids
 
Applying Refactoring Tools in Practice
Applying Refactoring Tools in PracticeApplying Refactoring Tools in Practice
Applying Refactoring Tools in Practice
 
CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”CFP - 1st Workshop on “AI Meets Blockchain”
CFP - 1st Workshop on “AI Meets Blockchain”
 
Great Coding Skills Aren't Enough
Great Coding Skills Aren't EnoughGreat Coding Skills Aren't Enough
Great Coding Skills Aren't Enough
 
College Project - Java Disassembler - Description
College Project - Java Disassembler - DescriptionCollege Project - Java Disassembler - Description
College Project - Java Disassembler - Description
 
Coding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean CodeCoding Guidelines - Crafting Clean Code
Coding Guidelines - Crafting Clean Code
 
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
 
Bangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief PresentationBangalore Container Conference 2017 - Brief Presentation
Bangalore Container Conference 2017 - Brief Presentation
 
Bangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - PosterBangalore Container Conference 2017 - Poster
Bangalore Container Conference 2017 - Poster
 
Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)Software Design in Practice (with Java examples)
Software Design in Practice (with Java examples)
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Bangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship DeckBangalore Container Conference 2017 - Sponsorship Deck
Bangalore Container Conference 2017 - Sponsorship Deck
 
Let's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming LanguageLet's Go: Introduction to Google's Go Programming Language
Let's Go: Introduction to Google's Go Programming Language
 
Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction Google's Go Programming Language - Introduction
Google's Go Programming Language - Introduction
 
Java Generics - Quiz Questions
Java Generics - Quiz QuestionsJava Generics - Quiz Questions
Java Generics - Quiz Questions
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Docker by Example - Quiz
Docker by Example - QuizDocker by Example - Quiz
Docker by Example - Quiz
 
Core Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quizCore Java: Best practices and bytecodes quiz
Core Java: Best practices and bytecodes quiz
 

Kürzlich hochgeladen

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Kürzlich hochgeladen (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 

03 Jo P Mar 07

  • 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 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], 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 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 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