Page 1 of 1

Some Obfuscated C code

Posted: Tue Apr 21, 2015 7:17 pm
by mrpi64
I found this code, and I have no idea where it comes from. Copy and paste it as it is, and see the result (it's not a malicious program, just in case you were wondering).

EDIT: This was an entry for the Obfuscated Code Contest of 1988. The judges commented that it looked like someone had hit some random keys on the keyboard. See http://www.ioccc.org/years.html#1988

Code: Select all

#include <stdio.h>
main(t,_,a)
char
*
a;
{
	return!

0<t?
t<3?

main(-79,-13,a+
main(-87,1-_,
main(-86, 0, a+1 )


+a)):

1,
t<_?
main(t+1, _, a )
:3,

main ( -94, -27+t, a )
&&t == 2 ?_
<13 ?

main ( 2, _+1, "%s %d %d\n" )

:9:16:
t<0?
t<-72?
main( _, t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+,/+#n+,/#;\
#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/+k#;\
q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw' i;# ){nl]!/n{n#'; \
r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c ;;\
{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")
:
t<-50?
_==*a ?
putchar(31[a]):

main(-65,_,a+1)
:
main((*a == '/') + t, _, a + 1 )
:

0<t?

main ( 2, 2 , "%s")
:*a=='/'||

main(0,

main(-61,*a, "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry")

,a+1);}



Re: Some odd C code

Posted: Tue Apr 21, 2015 7:35 pm
by ShiftPlusOne
I've removed the duplicate topic. Topics go in the most relevant section, not all that may be applicable.
mrpi64 wrote:I found this code, and I have no idea where it comes from. Copy and paste it as it is, and see the result.
"Hey, I have this file called trjn.exe, I don't know where it came from, but I'm going to distribute it and ask people to run it."
That's probably a very good example of what NOT to do. Yes, I get the point is to show an example of code obfuscation, but for all you know, it could be a malicious.

Re: Some odd C code

Posted: Tue Apr 21, 2015 7:37 pm
by mrpi64
It's something (I think) someone wrote at cambridge years ago. It's quite clever the way it works (and no, it's not a trojan or malicious program of any kind ;) )

Re: Some odd C code

Posted: Tue Apr 21, 2015 7:44 pm
by DougieLawson
It's mostly benign, but you've introduced some spelling errors probably by a failed cut'n'paste along the way.

I started a su nobody shell cd /tmp and compiled and ran it in there. I should have done that in a chroot to be extra cautious.

I've never seen the point in obfuscating code in that way apart from it showing how awful you can make things if you try very hard.

Re: Some odd C code

Posted: Tue Apr 21, 2015 7:47 pm
by mrpi64
Hey, I did nothing to it. I just found it, and to be honest, it interested me. It's still quite clever the way it kind of calls itself.

Re: Some odd C code

Posted: Tue Apr 21, 2015 7:48 pm
by mrpi64
(and you really didn't need to take that many precautions ;) )

Re: Some odd C code

Posted: Tue Apr 21, 2015 7:52 pm
by DougieLawson
mrpi64 wrote:(and you really didn't need to take that many precautions ;) )
Yes I did, because I don't like rebuilding SDCards from backups, because it's a painful waste of time. I had to do that already today when I messed up an rpi-update.

Re: Some odd C code

Posted: Tue Apr 21, 2015 7:53 pm
by mrpi64
Well, have a partridge in a pear tree for compensation

Re: Some odd C code

Posted: Tue Apr 21, 2015 8:01 pm
by ShiftPlusOne
mrpi64 wrote:and no, it's not a trojan or malicious program of any kind ;)
I think you missed the point.

Re: Some odd C code

Posted: Tue Apr 21, 2015 8:06 pm
by mrpi64
Nah. I guess posting something up and saying "run this for me" is a bit suspicious.

Re: Some odd C code

Posted: Wed Apr 22, 2015 9:14 am
by jamesh
mrpi64 wrote:Nah. I guess posting something up and saying "run this for me" is a bit suspicious.
Not a bit suspicious. A LOT suspicious, especially obfuscated code like this.

Re: Some odd C code

Posted: Wed Apr 22, 2015 11:21 am
by mrpi64
Well, the only function it uses is one to output a character.

Re: Some odd C code

Posted: Wed Apr 22, 2015 12:17 pm
by dave j
Searching for part of one of the strings reveals it was an entry to the International Obfuscated C Code Contest for 1988 (look for phillipps on this link). Posting a link to the code on that site (or mentioning the URL you'd got it from) would have allayed people's fears about what it might do.

If you want to know how it works, somebody has reverse engineered it.

Re: Some odd C code

Posted: Wed Apr 22, 2015 12:32 pm
by jamesh
mrpi64 wrote:Well, the only function it uses is one to output a character.
On the other hand, the obfuscated string in the middle might contain hidden code which calls rm -rf * from the root folder...it's not just the visible calls that are important.

Re: Some Obfuscated C code

Posted: Wed Apr 22, 2015 8:27 pm
by mrpi64
That's true, but with just stdio, I'm not sure. Can that still work?