DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 31st March 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default Obfuscated Code

This is my first exercise in code obfuscation. This is a legal C program and will compile (tested gcc and pcc) -- I'll let you guess what it does before I tell you (don't cheat by running it).

Code:
_(__,___,____,_____){(__)?printf("%d\n",____),_(__-_____,____,___+____,_____):0;}main(){_(25,0,1,1);}
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
  #2   (View Single Post)  
Old 31st March 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

I don't know what's more evil, that I can actually read that fine or that you actually compiled it.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #3   (View Single Post)  
Old 31st March 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

It's valid C code.

Here's the de-obfuscated code:

Code:
/*
        a recursive fibonacci generator to print upto:
            (arg1+arg4)/arg4 fib's if (arg4 > 1 && arg4 < arg1)
            arg1 fib's if (arg4==1 && arg4 <= arg1)
        starting with the numbers arg2 and arg3 (assuming arg3 == arg2+1 || arg2 == arg3 == 1 && arg2 >= 0)
*/
fib_gen(arg1,arg2,arg3,arg4)
{
        (arg1) ?
        /* printf(...); (void) fib_gen(...); */
        printf("%d\n", arg3), fib_gen(arg1-arg4, arg3, arg2+arg3, arg4)
        :
        0;
}

main()
{
        fib_gen(25,0,1,1);
}
So, what prize am I getting?

Last edited by ephemera; 31st March 2009 at 04:21 PM.
Reply With Quote
  #4   (View Single Post)  
Old 1st April 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Quote:
Originally Posted by ephemera View Post
It's valid C code.

Here's the de-obfuscated code:

Code:
/*
        a recursive fibonacci generator to print upto:
            (arg1+arg4)/arg4 fib's if (arg4 > 1 && arg4 < arg1)
            arg1 fib's if (arg4==1 && arg4 <= arg1)
        starting with the numbers arg2 and arg3 (assuming arg3 == arg2+1 || arg2 == arg3 == 1 && arg2 >= 0)
*/
fib_gen(arg1,arg2,arg3,arg4)
{
        (arg1) ?
        /* printf(...); (void) fib_gen(...); */
        printf("%d\n", arg3), fib_gen(arg1-arg4, arg3, arg2+arg3, arg4)
        :
        0;
}

main()
{
        fib_gen(25,0,1,1);
}
So, what prize am I getting?
I'm impressed -- you even came close to the original function name fib().

Three cheers for ephemera!
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
  #5   (View Single Post)  
Old 1st April 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

So, how did I do on my first attempt at obfuscation? Was it hard to understand? easy to decipher? etc.?
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
  #6   (View Single Post)  
Old 1st April 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

Interesting, no, and yes, lol.

All I had to do, was count the underscores to find which identifiers were which, as soon as I spotted the recursive call (~3 seconds later) it was obvious that displays a sequence of 25 numbers. The only 'hard' part was double checking if the sequence were fibonacci numbers, if you call that hard. (I stopped thinking after mentally compiling '5').


Maybe try writing a java virtual machine in brainfuck next time? hehe.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
  #7   (View Single Post)  
Old 1st April 2009
Carpetsmoker's Avatar
Carpetsmoker Carpetsmoker is offline
Real Name: Martin
Old man from scene 24
 
Join Date: Apr 2008
Location: Eindhoven, Netherlands
Posts: 2,068
Thanked 198 Times in 156 Posts
Default

Quote:
Maybe try writing a java virtual machine in brainfuck next time? hehe.
I think anyone trying to do that will soon have an obfuscated brain.
__________________
UNIX was not designed to stop you from doing stupid things, because that would also stop you from doing clever things.
Reply With Quote
  #8   (View Single Post)  
Old 1st April 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

Quote:
Originally Posted by JMJ_coder View Post
So, how did I do on my first attempt at obfuscation? Was it hard to understand? easy to decipher? etc.?
TBH, I didn't find it too hard to decipher. But, did you make this on your own?
If so, it's a good effort - in particular I liked your use of recursion!

Last edited by ephemera; 1st April 2009 at 06:10 AM.
Reply With Quote
  #9   (View Single Post)  
Old 2nd April 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Quote:
Originally Posted by ephemera View Post
TBH, I didn't find it too hard to decipher. But, did you make this on your own?
If so, it's a good effort - in particular I liked your use of recursion!
I followed some tips from Wikipedia (e.g., use recursion instead of iteration, ?: instead of if-else, etc.), but I wrote the program and converted to obfuscated code myself.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)
Reply With Quote
Old 2nd April 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

Cool.

Somehow obfuscation never caught my fancy.
Anyway, any code of some size and complexity without good comments is obfuscated enough for me.

You might have probably seen this one, its cool: http://www.cs.uleth.ca/~holzmann/C/C/cselfprint.html

Last edited by ephemera; 2nd April 2009 at 02:46 PM.
Reply With Quote
Old 2nd April 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

if you want to use the ?: trinary/ternary operator to obfuscate things, be sure to nest them in strange and deranged ways, and slip in comma's and garbage values anywhere you can.


In my personal opinion, good code should be fairly obvious even without comments, and the comments should be free to provide 'commentary' and annotations instead of explaining away physical laziness. e.g.

Code:
/* push element onto int stack */ istk_t istkp(iskt_t *s, iskt_t *e);  

vs 

istack_t istack_push( istack_t *stk, istack_t *elem);
where the latter speaks for itself, documenting all but the 'i' part. And a proper comment in the istack module might explain what the heck the it is, and hopefully it's purpose and use should be known in a few minutes of code browsing even without it. You could say, I hate anything that I need to read more any furhter then the header file in order to use it....



Those bastardo 2000+ line procedures some people write on the other hand, just need to be drawn and quartered ;-)
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Old 30th October 2009
s2scott's Avatar
s2scott s2scott is offline
Package Pilot
 
Join Date: May 2008
Location: Toronto, Ontario Canada
Posts: 198
Thanked 9 Times in 8 Posts
Default

I'm sensing you guys would have liked APL.

/S
__________________
Never argue with an idiot. They will bring you down to their level and beat you with experience.
Reply With Quote
Old 30th October 2009
jggimi's Avatar
jggimi jggimi is offline
More noise than signal
 
Join Date: May 2008
Location: USA
Posts: 3,674
Thanked 214 Times in 189 Posts
Default

I used to be an APL programmer, back in the late '70s. And through until around 1981, or so, when I moved to systems programming (today, that would be systems administration, but back then, we modified our OSes to meet requirements).

APL had some interesting capabilities, helpful for financial analysis applications (I was working for a commodities trading firm). Special keyboards and terminals were needed, however.

I did end up learning how to pronounce and spell most of the Greek alphabet, of course, as any APL programmer would. But along with Ro and Pho and Theta, we also used words to describe symbolic functions that were not Greek. The left and right arrows, for example, were called the "guzinta" and "guzouta".

One could write a line of code that would perform an operation on a large vector of numbers, that might contain, for example, twenty thousand integers, all at once. But, if the programmer misplaced a comma, rather than making a single arithmetic operation on twenty thousand numbers at once, one would instead execute twenty thousand individual operations. You'd get the same results, but thousands of times more slowly. Just from putting the comma in the wrong place.

I moved from APL to S/370 Assembler. The latter was so much clearer and easier to understand.
Reply With Quote
Old 31st October 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Thanked 112 Times in 104 Posts
Default

The number of people who have received :wq! and kkkkk messages from me, are living prove that I could never program in APL or any language needing a special keyboard, even if I think leaving near-ASCII behind is a Good Thing.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.
Reply With Quote
Old 5th November 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

No one pointed it out; there is a possible integer overflow if the no. of fibs is > 48.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programming QL-500 (ASCII Code) PetitConcept FreeBSD General 1 29th June 2009 10:08 PM
Comparison of Code Quality JMJ_coder Other BSD and UNIX/UNIX-like 13 11th April 2009 03:40 AM
Source code for ed? matt FreeBSD Ports and Packages 1 21st October 2008 08:18 PM
Compiling OpenBSD code WeakSauceIII OpenBSD General 4 19th May 2008 12:59 AM


All times are GMT. The time now is 12:59 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick