The Daily WTF: Curious Perversions in Information Technology
Welcome to TDWTF Forums Sign in | Join | Help
in Search

Speed Challenge 32: Obfuscated Hello World

Last post 08-09-2008 4:05 PM by curtmack. 31 replies.
Page 1 of 1 (32 items)
Sort Posts: Previous Next
  • 07-21-2007 11:56 AM

    Speed Challenge 32: Obfuscated Hello World

    Write the most obfuscated program who's sole output is "Hello, World!" (with or without punctuation) to standard output.

     It came to me when looking through my old assembly programs, and I came across this:

     

    .text
    main:
        sub $sp, $sp, 14
        move $a0, $sp
        li $t0, 72       
        li $t1, 101
        sb $t0, 0($sp)
        sub $t0, $t0, 2
        div $t0, $t0, 10
        sb $t1, 1($sp)
        add $t1, $t1, $t0
        sll $t2, $t1, 8
        sb $t1, 10($sp)
        add $sp, $sp, 2
        or $t1, $t1, $t2
        sh $t1, 0($sp)
        li $v0, 4
        li $t2, 10
        sub $t2, $t2, $t0
        add $t1, $t2, $t1
        sb $t1, 2($sp)
        sb $t1, 6($sp)
        add $sp, $sp, 1
        li $t1, 44
        sb $t1, 2($sp)
        add $sp, $sp, 3
        li $t2, 100
        sb $t2, 5($sp)
        li $t1, 22304
        sh $t1, 0($sp)
        sub $t1, $t1, 22271
        sb $t1, 6($sp)
        mul $t0, $t0, 2
        add $t0, $t0, $t2
        sb $t0, 3($sp)
        sb $0, 7($sp)
        syscall
        li $v0, 10
        syscall
     

    Please, no "extra" code that doesn't affect the output, either.  That's just pointless.  Every line of code has to affect the final output (unless you're programming in BeFunge, in which case every character has to affect the output.  I think you know what I mean.)

    --Edward Dassmesser
  • 07-21-2007 2:41 PM In reply to

    • tster
    • Top 10 Contributor
    • Joined on 04-11-2006
    • Worcester, MA
    • Posts 1,180

    Re: Speed Challenge 32: Obfuscated Hello World

    someone create a default "Hello World" .NET program inside the newest visual studio and paste it verbatim.
    The pig go. Go is to the fountain. The pig put foot. Grunt. Foot in what? ketchup. The dove fly. Fly is in sky. The dove drop something. The something on the pig. The pig disgusting... see bio for the earth shattering ending.
  • 07-21-2007 4:21 PM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    rpar PROTON all
  • 07-22-2007 7:25 AM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    I'd like to take a part in this one, but I am affraid I can't do it in next hours. I'll try to submit something before 24h deadline.
  • 07-22-2007 5:53 PM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    I see how they could be similar, but this can still be all in one language (as was my example), and the previous example wasn't necessarily for a "Hello, World!" output.
    --Edward Dassmesser
  • 07-22-2007 6:25 PM In reply to

    Obfuscation by no obfuscation

    I have to admit that I never liked idea of writing obfuscated code, as in my opinion mostly this is quite useless stuff, not showing anythng about knowledge of the language, etc. So I choose to submit totally not obfuscated entry. I think that it's main strength is that this is totally legal, standard C++, with no things like defines, trigraphs, and that kind of sh*it. However, I decided to leave Polish names for functions, variables, etc. to make things more fun ;)

    It's weak because it is not obfuscated, uses quite not big number of concepts (i.e. no inheritance and virtual calls), but I think that Hello, World should be simple, so here it is:

     

    #include <string>

    #include <algorithm>

    #include <iostream>

    class Napis

    {

    std::basic_string<char, std::char_traits<char>, std::allocator<char> > napis;

    public:

    Napis& dodaj(std::basic_string<char, std::char_traits<char>, std::allocator<char> >::value_type znak)

    {

    napis.insert(napis.end(), napis.end() == napis.begin() ? znak : *(napis.end() - 1) + znak);

    return *this;

    }

    std::basic_string<char, std::char_traits<char>, std::allocator<char> >& operator_napis()

    {

    static std::basic_string<char, std::char_traits<char>, std::allocator<char> > nowy(napis.c_str(), -std::distance(napis.end(), napis.begin()));

    return nowy;

    }

    };

     

    int main(int argc, char* argcv[])

    {

    Napis napis;

    unsigned char tempval = *(std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> >((reinterpret_cast<unsigned char**>(argcv))[0]).end() - 1) * 9 % 100;

    tempval = tempval / 10 + tempval % 10;

    napis.dodaj(tempval + 077).dodaj(29).dodaj(0x7).dodaj(-0).dodaj(3).dodaj(-0103).dodaj(-0x000C).dodaj(067)

    .dodaj(24).dodaj(3).dodaj(-6).dodaj(-010).dodaj(-0103);

    std::copy(napis.operator_napis().begin(), napis.operator_napis().end(), std::ostream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >::value_type>(std::cout, sizeof(char) - 1));

    return 0;

    }

     

     I hope that forum software will display my code correctly.

     

    Cheers,

    Hob

    Filed under: ,
  • 07-22-2007 6:44 PM In reply to

    Re: Obfuscation by no obfuscation

    Please, replace second line of main function with following:

    unsigned char tempval = *(std::basic_string<unsigned char, std::char_traits<unsigned char>, std::allocator<unsigned char> >((reinterpret_cast<unsigned char**>(argcv))[0]).end() - 1) %10 * 9;

     without this, entry submitted by me works only with Windows :)

     

  • 07-22-2007 7:33 PM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    In that case, I'll proudly present "obfuscation by Windows". Well okay, so Dev-C++ did most of the work for me. Anyway:
     
     
    #include 
    
    #define BUFSZ 32
    
    LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
    
    char szClassName[ ] = "WindowsApp";
    
    int WINAPI WinMain (HINSTANCE hThisInstance,
                        HINSTANCE hPrevInstance,
                        LPSTR lpszArgument,
                        int nShow)
    {
        HWND hwnd;
        MSG messages;
        WNDCLASSEX wincl;
    
        wincl.hInstance = hThisInstance;
        wincl.lpszClassName = szClassName;
        wincl.lpfnWndProc = WindowProcedure;
        wincl.style = CS_DBLCLKS;
        wincl.cbSize = sizeof (WNDCLASSEX);
    
        wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
        wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
        wincl.lpszMenuName = NULL;
        wincl.cbClsExtra = 0;
        wincl.cbWndExtra = 0;
        wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    
        if (!RegisterClassEx (&wincl))
            return 0;
    
        hwnd = CreateWindowEx (
               0,                   /* Extended possibilites for variation */
               szClassName,         /* Classname */
               "Hello World",       /* Title Text */
               WS_OVERLAPPEDWINDOW, /* default window */
               CW_USEDEFAULT,       /* Windows decides the position */
               CW_USEDEFAULT,       /* where the window ends up on the screen */
               300,                 /* The programs width */
               200,                 /* and height in pixels */
               HWND_DESKTOP,        /* The window is a child-window to desktop */
               NULL,                /* No menu */
               hThisInstance,       /* Program Instance handler */
               NULL                 /* No Window Creation data */
               );
    
        ShowWindow (hwnd, nShow);
        while (GetMessage (&messages, NULL, 0, 0))
        {
            TranslateMessage(&messages);
            DispatchMessage(&messages);
        }
        return messages.wParam;
    }
    
    
    LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	union {	LPTSTR title, msg; };
        switch (message)
        {
            case WM_CREATE:
                msg = new TCHAR[BUFSZ];
                GetWindowText(hwnd, msg, BUFSZ);
                MessageBox(hwnd, title, msg, MB_OK);
                delete[] msg;
            case WM_DESTROY:
                PostQuitMessage(0);
                break;
            default:
                return DefWindowProc (hwnd, message, wParam, lParam);
        }
        return 0;
    }
     
     
    rpar PROTON all
  • 07-23-2007 4:18 AM In reply to

    • Daid
    • Top 100 Contributor
    • Joined on 01-30-2007
    • Posts 266

    Re: Speed Challenge 32: Obfuscated Hello World

    #include <stdio.h>

    int main(int argc, char** argv)
    {
        char H[4] = "Hell";
        int I;
        int J;
        int K = 0x0a782500;

        I = 111 << 24;
        J = K - 1292430;
        I+= 2800 * 2800;
        I-= 32913;

        printf(H);
        return 0;
    }

     

    Sorry, only had 10 minutes here ;)

  • 07-23-2007 9:34 AM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    While I am rather partial to the templated approach, overflowing an array to create a simple hello world program is so deliciously wrong (Imagine a CS 101 teacher getting this as a submission) that I think Daid must be declared the winner here. (Unfortunately, the second submission, as far as I could tell, was not outputting to the console. I know, nitpicky, but since I couldn't get it to run, I have to go by my interpretation of the code, rather than some compiler's.)
    --Edward Dassmesser
  • 07-23-2007 9:59 AM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    Yeah, noone likes templates, that's why we still have tons of C++ code full of char* and arrays created with new and never deleted...
    j/k

    CONGRATULATIONS TO THE WINNER!

  • 07-23-2007 11:32 AM In reply to

    • Daid
    • Top 100 Contributor
    • Joined on 01-30-2007
    • Posts 266

    Re: Speed Challenge 32: Obfuscated Hello World

    *blink* *blink*... I won?

    Guess I'll have to think up the next one... 

  • 07-23-2007 12:13 PM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    Unicode obfuscation in C#!

    using System;
    class \u0048\u0065\u006c\u006c\u006f\u0057\u006f\u0072\u006c\u0064
    {
      static void \u004d\u0061\u0069\u006e(string[] args)
      {
        \u0043\u006f\u006e\u0073\u006f\u006c\u0065
          .\u0057\u0072\u0069\u0074\u0065\u004c\u0069\u006e\u0065
          (
            typeof
            (
              \u0048\u0065\u006c\u006c\u006f\u0057\u006f\u0072\u006c\u0064
            )
            .\u0046\u0075\u006c\u006c\u004e\u0061\u006d\u0065
          );
      }
    }

     

  • 07-23-2007 3:22 PM In reply to

    • Daid
    • Top 100 Contributor
    • Joined on 01-30-2007
    • Posts 266

    Re: Speed Challenge 32: Obfuscated Hello World

    And nr 33 is here: http://forums.worsethanfailure.com/forums/126487/ShowThread.aspx#126487
  • 08-21-2007 11:10 PM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    import Char; import List; import Maybe
    main = putStrLn $ map (chr . fromInteger) $ unfoldr foo 653552968926683012802491080
    foo n = listToMaybe $ genericTake n $ [(n `mod` 128, n `div` 128)]
  • 08-27-2007 11:35 PM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    This isn't the same thing, but reminds me of something I saw on QDB. Perl script:

    length q bless glob and print chr oct ord q mkdir m and print chr ord q xor x and print chr ord q q q and print chr ord uc q map m and print chr ord q qw q and print chr ord q each le and print chr ord q my alarm and print chr oct oct ord uc qw q for q

    Prints 'Go Away.'
  • 02-11-2008 9:19 AM In reply to

    • Daniel15
    • Top 150 Contributor
    • Joined on 01-27-2007
    • Melbourne, Australia
    • Posts 213

    Re: Speed Challenge 32: Obfuscated Hello World

    Daid:

    #include <stdio.h>

    int main(int argc, char** argv)
    {
        char H[4] = "Hell";
        int I;
        int J;
        int K = 0x0a782500;

        I = 111 << 24;
        J = K - 1292430;
        I+= 2800 * 2800;
        I-= 32913;

        printf(H);
        return 0;
    }

     

    Sorry, only had 10 minutes here ;)

    I know this is old, but could you explain how this works / what it's doing?

    (I've never taken any programming courses, and the only languages I know at the moment are high-level languages like Visual Basic and PHP. I understand some simple C code, but I've never programmed in it. I'm probably going to learn C at University, I'm starting Software Development at Uni this year)

  • 02-11-2008 11:04 AM In reply to

    • olm
    • Not Ranked
    • Joined on 08-06-2007
    • Posts 18

    Re: Speed Challenge 32: Obfuscated Hello World

    Daniel15:

    I know this is old, but could you explain how this works / what it's doing?

    (I've never taken any programming courses, and the only languages I know at the moment are high-level languages like Visual Basic and PHP. I understand some simple C code, but I've never programmed in it. I'm probably going to learn C at University, I'm starting Software Development at Uni this year)

    It doesn't necessarily work. With my gcc compiler I had to reverse the declaration of the variables due to the order they are placed on the stack. (can't remember how compiler- or standards-specific this is)

    Basic explanation of the algorithm:
    Characters are represented as their ASCII value, e.g. 'H' is 0x48.
    This means that the 4 characters "Hell" are internally known as 0x48, 0x65, 0x6C, 0x6C.
    Using the endianness (is that a word?) of the x86 cpu, this is internally stored as an 32 bit integer (word) with the value 0x6C6C6548. ('lleH')
    The whole sentence "Hello world\n" is represented as 0x6C6C6548, 0x6F77206F, 0x0A646C72 ('lleH', 'ow o', '\ndlr' - '\n' is the newline character)

    The printf() function takes a pointer to the first character in a string. It then prints that and all following characters untill it encounters the character with the ASCII value 0.
    (Which is incidentally the first character in K = 0x0a782500 :-)
    So it starts by printing the characters in H, followed by the characters in I and J and then on.

    This means that to make the program print "Hello world\n", we just have to make I = 0x6F77206F and J = 0x0A646C72, since the printing stops at K.
    This is done obfuscated in the four lines in the middle.

     

    This will probably all be more clear after you have learned about computer architecture, pointers, stack, compilers etc. etc. at the university :-)

  • 02-12-2008 6:36 AM In reply to

    • Daniel15
    • Top 150 Contributor
    • Joined on 01-27-2007
    • Melbourne, Australia
    • Posts 213

    Re: Speed Challenge 32: Obfuscated Hello World

    Thanks for that, it makes sense now :D

    As for my Uni course, there's a "Computer and Logic Essentials" unit, I guess we might learn about this stuff in that (we do some Assembler, so I suppose stuff like this should be covered?) :)

    It doesn't necessarily work. With my gcc compiler I had to reverse the declaration of the variables due to the order they are placed on the stack.
    It worked for me on Ubuntu (gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2))
  • 02-14-2008 4:54 AM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    Daniel15:
    It worked for me on Ubuntu (gcc (GCC) 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2))

    Now try compiling with "-O3".

    rpar PROTON all
  • 03-25-2008 6:17 PM In reply to

    • NaN
    • Not Ranked
    • Joined on 03-18-2008
    • Posts 8

    Re: Speed Challenge 32: Obfuscated Hello World

     It's better late to the party than never. The lines with "delay" in them can be removed, but it's better keeping them so you can watch it in it's magnificence. As specified, it prints to the consol.

    Java:


    import java.awt.Robot;
    import java.awt.event.KeyEvent;

    public class Hello {

        public static void main(String[ args) {
        Robot r = null;    
           
            try               
            {
            r = new Robot();
            }
            catch(Exception e)
            {
           
            }
           
            r.delay(250);
           
            r.keyPress(KeyEvent.VK_WINDOWS);
            r.keyPress(KeyEvent.VK_R);
            r.keyRelease(KeyEvent.VK_WINDOWS);
            r.keyRelease(KeyEvent.VK_R);

            r.delay(250);
                   
            r.keyPress(KeyEvent.VK_BACK_SPACE);
            r.keyRelease(KeyEvent.VK_BACK_SPACE);

            r.delay(250);

            r.keyPress(KeyEvent.VK_C);
            r.keyRelease(KeyEvent.VK_C);

            r.delay(250);

            r.keyPress(KeyEvent.VK_M);
            r.keyRelease(KeyEvent.VK_M);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_D);
            r.keyRelease(KeyEvent.VK_D);
           
            r.delay(250);
           
            r.keyPress(KeyEvent.VK_ENTER);
            r.keyRelease(KeyEvent.VK_ENTER);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_C);
            r.keyRelease(KeyEvent.VK_C);

            r.delay(250);

            r.keyPress(KeyEvent.VK_H);
            r.keyRelease(KeyEvent.VK_H);

            r.delay(250);

            r.keyPress(KeyEvent.VK_O);
            r.keyRelease(KeyEvent.VK_O);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_SPACE);
            r.keyRelease(KeyEvent.VK_SPACE);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_H);
            r.keyRelease(KeyEvent.VK_H);

            r.delay(250);

            r.keyPress(KeyEvent.VK_E);
            r.keyRelease(KeyEvent.VK_E);


            r.delay(250);
           
            r.keyPress(KeyEvent.VK_L);
            r.keyRelease(KeyEvent.VK_L);

           
            r.delay(250);
           
            r.keyPress(KeyEvent.VK_L);
            r.keyRelease(KeyEvent.VK_L);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_O);
            r.keyRelease(KeyEvent.VK_O);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_SPACE);
            r.keyRelease(KeyEvent.VK_SPACE);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_W);
            r.keyRelease(KeyEvent.VK_W);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_O);
            r.keyRelease(KeyEvent.VK_O);
     
            r.delay(250);
           
            r.keyPress(KeyEvent.VK_R);
            r.keyRelease(KeyEvent.VK_R);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_L);
            r.keyRelease(KeyEvent.VK_L);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_D);
            r.keyRelease(KeyEvent.VK_D);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_SHIFT);
            r.keyPress(KeyEvent.VK_1);

            r.delay(250);

            r.keyRelease(KeyEvent.VK_SHIFT);
            r.keyRelease(KeyEvent.VK_1);

            r.delay(250);
           
            r.keyPress(KeyEvent.VK_ENTER);
            r.keyRelease(KeyEvent.VK_ENTER);
           
        }

    }


  • 03-26-2008 12:52 PM In reply to

    • deibyz
    • Not Ranked
    • Joined on 03-26-2008
    • Posts 3

    Re: Speed Challenge 32: Obfuscated Hello World

     Mine (a bit late):

     

    #!/usr/bin/perl
    use strict;use warnings;
    $SIG{__WARN__}=sub{($_)=@_;/^\w+\s\w+(\w)(?{print$1})(?<=[\s])/};open OdlroWolleH,$0;print OdlroWolleH "Just Another Perl Hack";
     

  • 04-10-2008 9:00 AM In reply to

    Re: Speed Challenge 32: Obfuscated Hello World

    Faxmachinen:

    Daniel15:
    It worked for me on U