Live data from Hacker News

Main is usually a function – when is it not?

jroweboy.github.io

11–20 of 62 posts

Re: Main is usually a function – when is it not?

#11
post #3

I wonder if one could write a header file containing definitions that would make the following possible. char main[] = { movl(1, eax), movl(1, ebx), movl(message, esi), movl(13, edx), syscall(), movl(60, eax), xorl(ebx, ebx), syscall(), }; Obviously there are some technical difficulties like handling literal values and code sections, but it could be a fun hack, and I've love to see what someone could come up with.

It's possible, but once you're not obfuscating the code - and already depending on OS details when you assume that global data can be executable, whether const or not - I think you may as well just use the inline assembler feature of your compiler. Well, if there is one... MSVC bizarrely doesn't support on x86-64 what was a perfectly good feature on x86.

Re: Main is usually a function – when is it not?

#12
post #2

Next challenge... make it a real string! https://0xcd80.wordpress.com/2011/04/29/linux-x86-shellcodin... https://code.google.com/p/alpha3/

  const char main[] =
    "UH\x89\xe5\xb8\1\0\0\0\xbb\1\0\0\0g\x8d"
    "5\x10\0\0\0\xba\r\0\0\0\xf\5\xb8\x3c\0\0\0"
    "1\xdb\xf\5Hello World!\n]\xc3";

Re: Main is usually a function – when is it not?

#13
post #8
post #7

Reminds me of this 'Hello World': http://stackoverflow.com/questions/15182496/why-does-this-co... I bet these two ideas would go well together.

The seed for the random number generator is not executed though.

I was thinking more along the lines of making the main function (array) call some other function which does this in such a way that it looks like you implemented a random number generator but it actually prints "Hello World". And you could hide the fact that 'main' was a piece of code by making it the initial seed for for the random number generator and using macros in a separate header file to rename it to something like 'SEED'.

Re: Main is usually a function – when is it not?

#15
post #6
post #4

I recall from somewhere: int main = 0; This code compiles (cleanly!), but segfaults.

I think that sets the address of main to NULL, so it segfaults as soon as `_start` jumps to `main`. It's known for being one of the smallest compilable C programs. EDIT: Your original post contained int main=0;

Not quite - what matters (normally) is the address of the symbol, not the bytes located there, since in the case of a real function those bytes would be the instructions. So this will either execute the bytes at &main as instructions (4 zero bytes, and whatever follows), or, more likely, crash due to memory protections, as described in the article.

Re: Main is usually a function – when is it not?

#16
post #11
post #3

I wonder if one could write a header file containing definitions that would make the following possible. char main[] = { movl(1, eax), movl(1, ebx), movl(message, esi), movl(13, edx), syscall(), movl(60, eax), xorl(ebx, ebx), syscall(), }; Obviously there are some technical difficulties like handling literal values and code sections, but it could be a fun hack, and I've love to see what someone could come up with.

It's possible, but once you're not obfuscating the code - and already depending on OS details when you assume that global data can be executable, whether const or not - I think you may as well just use the inline assembler feature of your compiler. Well, if there is one... MSVC bizarrely doesn't support on x86-64 what was a perfectly good feature on x86.

It might be an easier way to produce obfuscated code by reading the preprocessed output. But I see your point---it's reinventing the wheel in a different way. That's the fun of it of course.

Re: Main is usually a function – when is it not?

#18
post #17

Wouldn't this be considered academic fraud? His coworker turning in code that he didn't write?

Probably not, since this isn't a case where my co worker can just copy paste my code and hope it works for two reasons. The first is I am not entirely sure their testing environment is using 64 bit linux, it could be 32 bit linux since I'm not in the class, I just assumed its 64bit so I could test it on my machine. The next reason is they don't actually have a Hello World assignment at all! The reason I wrote this article is so I could share something I learned, and my co worker (who has already completed the first half of the semester's assignments) could use this as a starting point for how to write his own version for an assignment.

Re: Main is usually a function – when is it not?

#19
post #12
post #2

Next challenge... make it a real string! https://0xcd80.wordpress.com/2011/04/29/linux-x86-shellcodin... https://code.google.com/p/alpha3/

const char main[] = "UH\x89\xe5\xb8\1\0\0\0\xbb\1\0\0\0g\x8d" "5\x10\0\0\0\xba\r\0\0\0\xf\5\xb8\x3c\0\0\0" "1\xdb\xf\5Hello World!\n]\xc3";

I should have been more clear -- something more alphanumeric than ASCII.
Post reply on HN