mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Some Obfuscated C code

Tue Apr 21, 2015 7:17 pm

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);}


Last edited by mrpi64 on Wed Apr 22, 2015 12:22 pm, edited 3 times in total.

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6356
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Some odd C code

Tue Apr 21, 2015 7:35 pm

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.

mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Some odd C code

Tue Apr 21, 2015 7:37 pm

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 ;) )

User avatar
DougieLawson
Posts: 41962
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Some odd C code

Tue Apr 21, 2015 7:44 pm

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.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Some odd C code

Tue Apr 21, 2015 7:47 pm

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.

mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Some odd C code

Tue Apr 21, 2015 7:48 pm

(and you really didn't need to take that many precautions ;) )

User avatar
DougieLawson
Posts: 41962
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Some odd C code

Tue Apr 21, 2015 7:52 pm

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.
Languages using left-hand whitespace for syntax are ridiculous

DMs sent on Twitter/LinkedIn will be answered next month.
Fake doctors - are all on my foes list.

The use of crystal balls and mind reading is prohibited.

mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Some odd C code

Tue Apr 21, 2015 7:53 pm

Well, have a partridge in a pear tree for compensation

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6356
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

Re: Some odd C code

Tue Apr 21, 2015 8:01 pm

mrpi64 wrote:and no, it's not a trojan or malicious program of any kind ;)
I think you missed the point.

mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Some odd C code

Tue Apr 21, 2015 8:06 pm

Nah. I guess posting something up and saying "run this for me" is a bit suspicious.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30436
Joined: Sat Jul 30, 2011 7:41 pm

Re: Some odd C code

Wed Apr 22, 2015 9:14 am

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.
Principal Software Engineer at Raspberry Pi Ltd.
Working in the Applications Team.

mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Some odd C code

Wed Apr 22, 2015 11:21 am

Well, the only function it uses is one to output a character.

dave j
Posts: 133
Joined: Mon Mar 05, 2012 2:19 pm

Re: Some odd C code

Wed Apr 22, 2015 12:17 pm

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.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 30436
Joined: Sat Jul 30, 2011 7:41 pm

Re: Some odd C code

Wed Apr 22, 2015 12:32 pm

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.
Principal Software Engineer at Raspberry Pi Ltd.
Working in the Applications Team.

mrpi64
Posts: 931
Joined: Sat Feb 16, 2013 5:13 pm

Re: Some Obfuscated C code

Wed Apr 22, 2015 8:27 pm

That's true, but with just stdio, I'm not sure. Can that still work?

Return to “Off topic discussion”