Update 2001/anonymous - cpp, 32-bit ELF ten.c etc.

Add back cpp.c which is a C pre-processed version of anonymous.c. Added
comments and tried to format it some. Perhaps I should use a formatted /
de-obfuscator from some years ago in the IOCCC (by Dave Burton) but I
didn't think of that until much later on: I'll consider it another time.

The Makefile builds cpp by default. Works as well as anonymous.c: will
modify 32-bit ELF binaries in the right systems (it won't touch them in
macOS - I pre-compiled anonymous.ten.c as 32-bit ELF in linux and added
it to the repo as anonymous.ten.32 mostly for those who might be able to
run 32-bit ELF binaries but have no way to compile them though maybe no
system has that but it also allows for partly 'testing' the program in
macOS).

Change main target to compile to anonymous and not anonymous_tmp. This
is because otherwise running make would repeatedly try and build it. I
did try changing the target but that didn't want to work possibly due to
the cp updating the timestamp though that's a quick thought in the head
of someone who's been awake too long at the hour.

Instead anonymous.sh is the script to run to 'emulate' the running of
the target program (again assuming that's correct which does seem likely
but is still unconfirmed).

The anonymous.sh script now checks for compiling anonymous.ten.c but
uses anonymous.ten.32 as a backup if it exists.

Update bugs.md and README.md.

Anything else I'm afraid I've forgotten.
This commit is contained in:
Cody Boone Ferguson committed 2023-05-19 04:12:33 -07:00
1 parent 0571930501
commit 31fef7fa43
7 files changed
+464 -40

No files matched your search

+13 -4
View File
@@ -117,9 +117,9 @@ PROG= ${ENTRY}
# #
OBJ= ${PROG}.o OBJ= ${PROG}.o
DATA= DATA=
TARGET= ${PROG} ${PROG}.ten TARGET= ${PROG} cpp ${PROG}.ten
# #
ALT_OBJ= ALT_OBJ=
ALT_TARGET= ALT_TARGET=
@@ -139,8 +139,8 @@ ${PROG}: ${PROG}.c
X='-DA(X)=#X'; \ X='-DA(X)=#X'; \
warning='-Dprocessor'; \ warning='-Dprocessor'; \
$$magic $$warning -Dmagic= $$X "-DX=A($$magic \"$$X\")" \ $$magic $$warning -Dmagic= $$X "-DX=A($$magic \"$$X\")" \
$< -o $@_tmp ${LIBS} $< -o $@ ${LIBS}
${CP} -af anonymous.sh $@ ${CHMOD} -v +x anonymous.sh
${PROG}.ten: ${PROG}.ten.c ${PROG}.ten: ${PROG}.ten.c
@@ -157,6 +157,15 @@ ${PROG}.ten: ${PROG}.ten.c
@echo @echo
${CC} ${CFLAGS} -m32 $< -o $@ ${LIBS} ${CC} ${CFLAGS} -m32 $< -o $@ ${LIBS}
cpp: cpp.c
magic='${CC} ${CFLAGS}'; \
X='-DA(X)=#X'; \
warning='-Dprocessor'; \
$$magic $$warning -Dmagic= $$X "-DX=A($$magic \"$$X\")" \
$< -o $@ ${LIBS}
${CHMOD} -v +x anonymous.sh
# alternative executable # alternative executable
# #
alt: data ${ALT_TARGET} alt: data ${ALT_TARGET}
+12 -9
View File
@@ -16,10 +16,10 @@ make
``` ```
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed both the [Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed both the
supplementary program and the program itself, both of which segfaulted. He supplementary program and the program itself (but for the program itself see
managed to do this with linux but it will not work with macOS (see [bugs.md](/bugs.md), both of which segfaulted. He managed to do this with linux
[bugs.md](/bugs.md) for why this is); this is not a bug but a feature inherent but it will not work with macOS (see [bugs.md](/bugs.md) for why this is); this
in what it does. is not a bug but a feature inherent in what it does.
Although it took a fair bit of debugging it turns out all that Although it took a fair bit of debugging it turns out all that
needed to be changed was: needed to be changed was:
@@ -30,8 +30,9 @@ prevented `main()` from entering an infinite loop.
Without either of these it would crash and prevent modification of the 32-bit Without either of these it would crash and prevent modification of the 32-bit
[ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) (not elf :-) [ELF](https://en.wikipedia.org/wiki/Executable_and_Linkable_Format) (not elf :-)
) binary. The following change was also made to be more portable, in case the ) binary. But again see [bugs.md](/bugs.md) here. The following change was also
constants `PROT_READ` and/or `PROT_WRITE` are not standardised: made to be more portable, in case the constants `PROT_READ` and/or `PROT_WRITE`
are not standardised:
Change `3` in the call to `mmap()` to be `PROT_READ|PROT_WRITE`: just in case Change `3` in the call to `mmap()` to be `PROT_READ|PROT_WRITE`: just in case
`PROT_READ|PROT_WRITE` does not equal 3 (though it seems to be equal in both `PROT_READ|PROT_WRITE` does not equal 3 (though it seems to be equal in both
@@ -60,9 +61,11 @@ The author suggested that this will somewhat destroy the binaries this touches
but Cody did not observe this. It does indeed modify the files as the script but Cody did not observe this. It does indeed modify the files as the script
below will show you (though not all files are modified: can you figure out why below will show you (though not all files are modified: can you figure out why
that is?) but Cody did not notice any problems in using them. Perhaps it's that is?) but Cody did not notice any problems in using them. Perhaps it's
something he's not aware of. If you follow the try commands below you will something he's not aware of possibly including the fact that the modification
notice that although the binaries do differ it's not many differences. See the might or might not be complete. If you follow the try commands below you will
author's warning about this. notice that although the binaries do differ it's not many differences and the
output of the supplementary program both before and after is the same. See the
author's warning about this in their remarks.
### INABIAF - it's not a bug it's a feature! :-) ### INABIAF - it's not a bug it's a feature! :-)
+1 -1
View File
@@ -7,7 +7,7 @@ fi
make >/dev/null 2>&1 make >/dev/null 2>&1
if [[ -f "$1" ]]; then if [[ -f "$1" ]]; then
./anonymous_tmp "$1" ./anonymous "$1"
./"$1" ./"$1"
fi fi
BIN
View File
Binary file not shown.
+383
View File
@@ -0,0 +1,383 @@
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <strings.h>
#include <sys/mman.h>
#define l int*
#define F char
struct stat t;
#define c return
#define I (P+=4,*L(P-4,0))
#define G (signed F)E(*L(P++,0))
#define C(O,D) E (D[B+V(010)/4+O*10])
#define U R[4]=E(V(17)-4),*(l)V(021)=
char M [99],Q[99],b[15000],*ss,*d=b, *z;
#define O =(n=*(l)V(021),R[4]=E(V(17)+4),n)
#define p(a,b,c) system((sprintf(a,b,k[1]),c)),z
#define g (y/010&7)
#define R (B+13)
#define x86 (F*)index(ss+V(i ),0100)
#define D(y,n,a,m,i,c) d += \
sprintf(d, \
y,n,a,m,i,c ), \
(F* ) P
int *B,i,n,a,r,y,P ;
V(v){
return E(B[v]) /* end E() */;
}
int *Li(d,r)
{
return E (3[B+
V(010) /* end V() */
/4+r*10]
) &&
E (3 [B+
V(010) /* end V() in E() */
/4+r*10]) /* end E() */
<=d ?
(int*)(((int)B) + d +
E(4[B+
V (010) /* end V() in E() */
/4+r*10]) /* end E() */ -
E (3[B +
V(010) /* end V() in E() */
/4+r*10]) /* end E() */
) :
(((int)B)^d)>>24&&--r?
Li(d,r) /* end Li() */:
(int*)d;
}
int *L(d, r){
return r =
V(014 ) /* end V() */&
63,
Li(d,r) /* end Li() */
;
}
int *Runi(d,a)
{
return (ss = strcmp((char*)B+
E (4[B+
V(010)/* end V() *//4+
(V(12) /* end V() */
>>16)*10])+
E(0[B+
V(010) /* end V() */
/4+--r*10]), /* end E() */
".strtab") /* end strcmp() */ ?
ss:(char*)B+
E (4[B+
V(010) /* end V() in E() */
/4+r*10]) /* end E() */ ,
E (1[B+
V(010) /* end V() in E() */
/4+r*10]) /* end E() */ -
2) ?
Runi(d,r) :
0;
}
int *Run(d, a)
{
return r=
V(014 ) /* end V() */
&63,
Runi(d,a);
}
Sca()
{
i=((a-=16)+
E (4[B +
V (010) /* end V() in E() */
/4+r*10]) /* end E() */
)/4,
(V(-~i) /* end V() */
-P+2 ?
Sca() :
0
);
}
Scan()
{
a=
E(5[B+
V(010) /* end V() */
/4+r*10]) /* end E() */
, Sca();
}
ru()
{
i=((a-=16)+
E(4[B+
V(010) /* end V() */ /
4+r*10]) /* end E() */
)/4,
(strcmp(ss +
V(i),
"main") /* end strcmp() */ ?
ru() :
0)
;
}
run(){
a=
E(5[B +
V(010) /* end V() in E() */
/4+r*10]) /* end E() */,
ru();
}
E(DX)
{
return *z /* z at this point appeared to be '\002' if I (Cody) recalls correctly */ ?
DX : DX <<030 |
(DX&65280)<<010 |
DX>>8 & 65280 |
DX>>24&0377;
}
int* K(k)
{
return L(
E(
k [
(int*)V(17) /* end V() in E() in L() */
]) /* end E() in L() */ ,
0) /* end L() */;
}
int
pain(char *ck, char **k)
{
return (z ?
(stat(M,&t) ?
P+= a+'{' ?
0 : 3
: execv(M,k), /* note: M appears to be empty here but this used to be recursive main()
so M might have been modified on additional calls */
a=(signed char) E(*L(P++,0)),
i=P,
y=(signed char)E(*L(P++,0))
&255,
sprintf(Q,
y/'@'-3?"*L(V(%d+%d)+%d,0)":"R[%d]",
(y&7),'\r',
y/0100-1 ?
0
:
(signed char)E(*L(P++,0))
), /* ends sprintf() on line 175 */
(a+127&&a+'}' &&
a+1 ?
(a+61 &&
a+24 &&
a + 025 ?
(P = a+'H' &&
a%061
<=0 ?
P
:
i,
d+=
sprintf(d,
a>0?
" R[%d]=E(~-E(R[%d])),\0 U R[%d],\0 R[%d] O,\0 U %d,\0R[%d]=0," +
(a&'8')
*3
:a <- 'c'
?
" %d, %c%s = R[%d], \0 %d, *R=%c%s==R[%d], \0 R[%d]=(int)%c%s," -
a%'w'
%'j'
*5
:
"%d,%d,%s=%d,",
a>0 ?
a-'h'
?
a-49
?a&7
:
(y/010&7)
:
(P+=4,
*L(P-4,0)
)
:
(y/010&7),
a>0 ? a&7
:
"& "[a%3%2+1],
a+72 ?
Q:"*R",
a>0|a<-99?
(y/010&7)
:
(P+=4,
*L (P-4,0)
),
0 ) /* end sprintf on line 195 */
,(char* ) P)
:
(P=i,i=a+61 ?
a+21
?E((P+=4,*L(P-4,0)))
:
(signed char)E(*L(P++,0))
:0,
d+=sprintf( d,
a%' '+
29+
"P O,\0 U %d,P=%d,",
P,
P+i,
0,
0,
0 ), /* ends sprintf() on 242 */
(char* ) P,
system(
(sprintf(d,"A'",k[1]),
b) /* sprintf IN system() on line 252 (above line) */
) /* ends system on 252 */
,z)):
(n=
(y/010&7)
-4
?
(y/010&7)
%5-1
?
a+127
? (signed char)E(
*L(P++,0)
)
:(P+=4,
*L(P-4,0)
) :
0
:
(Scan(),
*( (char*)index(ss+
V(i ), /* V(i ) in index from line 274 */
0100)/*ends index() on line 274 */
?(char*)index(ss +
V(i ), /* V(i) in index() on line 277 */
0100)
:
d)
=0,
(int)(ss+
V(i)
)),
(y/010&7) %5
<2?
d+=sprintf( d,
(y/010&7)%5
?" %s=E(~-E(%s)), \0 U %s,"
+4*(y/010&7)
:
"%s=E(E(%s)%c%d),",
Q,
Q,
"+ - "[(y/010&7)],
n,
0 ) /* ends sprintf() on line 288 */
,(char* ) P
:
(a=(signed char) E(
*L(P++,0) /* *L() in E() from line 301 */
) /* ends E() on line * 301 */
-'u'
?
'!'
:'=',
d+=sprintf(d,
(y/010&7)
-4
?"P=%d%c=%s?%d:%d,"
: "*R=E(((int(*)(l,l,l))%s)(K(1),K(2),K(3))),P O,",
n,
a,
Q,
P,
P+(signed char)E (
*L(P++,0) /* *L() in E() in sprintf() from line 308 */
)
) /* ends sprintf() on line 308 */
,(char* ) P,
system(
(sprintf(d,"A'",k[1]), /* ends sprintf() in system() on line 323 */
b)) /* ends system() on line 323 but appears that b is what's system()d */
,z)
)
)
)
:
(Run(
(z=(char*)&ck,
stat(k[1],&t), /* stat() in Run() on line 331 */
B =
(l)mmap(0,i=t.st_size,PROT_READ|PROT_WRITE,1,
open(k[1],2),0) /* open() in mmap() (line 325) in Run() (line 331 */
)
), /* ends Run() on line 331 */
(((B+13)[4]
=
E(B+i/4),
run(),
P = V(-~i),
(B+13)[4] =
E(
V(17)- /* V() in E() on line 345 */
4),*(int*)V(021))), /* second V() in E() on line 345 */
sprintf(M,
".%s%d",k[1],P), /* ends sprintf() on line 348 */
d+=sprintf( d,
"%s %s '-DX=A(%s)' -o %s '-Dmagic=",
"cpp -std=gnu99 -Wall -Wextra -pedantic -Winfinite-recursion -Wno-bitwise-conditional-parentheses -Wno-error -Wno-implicit-function-declaration -Wno-implicit-int -Wno-int-to-pointer-cast -Wno-macro-redefined -Wno-pedantic -Wno-pointer-to-int-cast -Wno-return-type -Wno-string-plus-int -Wno-bitwise-op-parentheses -Wno-format -Wno-format-extra-args -Wno-int-conversion -Wno-unused-parameter -Wno-unsequenced -E \"-DA(X)=#X\"", /* end first string in sprintf() %s from line 350 */
"anonymous.c", /* second string in sprintf() on line 350.
note it appears to run the above cpp on anonymous.c ? */
"cpp -std=gnu99 -Wall -Wextra -pedantic -Winfinite-recursion -Wno-bitwise-conditional-parentheses -Wno-error -Wno-implicit-function-declaration -Wno-implicit-int -Wno-int-to-pointer-cast -Wno-macro-redefined -Wno-pedantic -Wno-pointer-to-int-cast -Wno-return-type -Wno-string-plus-int -Wno-bitwise-op-parentheses -Wno-format -Wno-format-extra-args -Wno-int-conversion -Wno-unused-parameter -Wno-unsequenced -E \"-DA(X)=#X\"",
/* above line is third string in sprintf() on line 350 */
M, /* fourth string in sprintf() on 350, possibly running cpp from above line on M */
fflush(0) ), /* end sprintf() on line 350 */
(char* ) P) /* ends Run() on line 331 */
);
}
int
main (int cka, char **k)
{
char *ck = (char *)cka;
/* main calls exit() which calls E() which has a call to pain() if
ck ((char *)cka - was originally a char * as first arg to main()) with
second arg being argv or k. Otherwise it calls system() with a convoluted sprintf() */
exit (E(
(ck?pain((char*)cka,k): /* if ck ((char *)argc which used to be a char *) run pain() which used to be main() */
system( /* otherwise if !ck call system() with ... */
(sprintf(M,"rm -f .%s*",k[1]),M) /* sprintf() writing to M a convoluted string including itself (which
iirc is undefined but perhaps it's my tiredness: I can't think of what
the '*' does in sprintf()) */
), /* ends system() from line 374 */
z,*(B+13))) /* end E() which calls either pain() or system() (which calls sprintf() on M) from line 372 from exit() on line 372 */
);
} /* end main() */
+22 -15
View File
@@ -2,30 +2,37 @@
make clobber all >/dev/null 2>&1 make clobber all >/dev/null 2>&1
if [[ -f "anonymous.ten" ]]; then
make anonymous 2>/dev/null TARGET="anonymous.ten"
echo "$ cp anonymous.ten anonymous.ten.bak" 1>&2
cp anonymous.ten anonymous.ten.bak if [[ ! -f "$TARGET" ]]; then
echo "$ ./anonymous anonymous.ten" 1>&2 TARGET="anonymous.ten.32"
./anonymous anonymous.ten echo "cannot compile anonymous.ten.c as 32-BIT, trying anonymous.ten.32 as backup"
echo "$ ./anonymous.ten" 1>&2 echo "if your system cannot run 32-bit ELF binaries this might fail"
./anonymous.ten fi
echo "$ diff -s anonymous.ten anonymous.ten.bak" 1>&2
diff -s anonymous.ten anonymous.ten.bak if [[ -f "$TARGET" ]]; then
echo "$ od -t x1 anonymous.ten > ten.od" 1>&2 echo "$ cp $TARGET anonymous.ten.bak" 1>&2
od -t x1 anonymous.ten > ten.od cp "$TARGET" anonymous.ten.bak
echo "$ ./anonymous $TARGET" 1>&2
./anonymous.sh "$TARGET"
echo "$ ./$TARGET" 1>&2
./"$TARGET"
echo "$ diff -s $TARGET anonymous.ten.bak" 1>&2
diff -s "$TARGET" anonymous.ten.bak
echo "$ od -t x1 $TARGET > ten.od" 1>&2
od -t x1 "$TARGET" > ten.od
echo "$ od -t x1 anonymous.ten.bak > ten.bak.od" 1>&2 echo "$ od -t x1 anonymous.ten.bak > ten.bak.od" 1>&2
od -t x1 anonymous.ten.bak > ten.bak.od od -t x1 anonymous.ten.bak > ten.bak.od
echo "$ diff ten.bak.od ten.od" 1>&2 echo "$ diff ten.bak.od ten.od" 1>&2
diff ten.bak.od ten.od diff ten.bak.od ten.od
echo "$ ./anonymous.ten > ten.txt" 1>&2 echo "$ ./$TARGET > ten.txt" 1>&2
./anonymous.ten > ten.txt ./"$TARGET" > ten.txt
echo "$ ./anonymous.ten.bak > ten.bak.txt" 1>&2 echo "$ ./anonymous.ten.bak > ten.bak.txt" 1>&2
./anonymous.ten.bak > ten.bak.txt ./anonymous.ten.bak > ten.bak.txt
echo "$ diff -s ten.txt ten.bak.txt" 1>&2 echo "$ diff -s ten.txt ten.bak.txt" 1>&2
diff -s ten.txt ten.bak.txt diff -s ten.txt ten.bak.txt
else else
echo "cannot compile anonymous.ten.c as 32-bit, sorry." echo "cannot compile anonymous.ten.c as 32-bit and backup file does not exist, sorry."
exit 1 exit 1
fi fi
+33 -11
View File
@@ -1114,24 +1114,46 @@ modifies the program, only works on 32-bit ELF binaries (by design) and the
binary still works but we do not know if it's supposed _additionally_ run it. binary still works but we do not know if it's supposed _additionally_ run it.
Can you confirm? Please let us know! Can you confirm? Please let us know!
## STATUS: INABIAF - please **DO NOT** fix ## STATUS: INABIAF - please **DO NOT** fix
[Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed this so that it [Cody Boone Ferguson](/winners.html#Cody_Boone_Ferguson) fixed this so that it
works (it segfaulted - but see above) in modern systems but he notes that the doesn't segfault and so that it at least _does_ modify the target executable (but see
program [anonymous.ten.c](2001/anonymous/anonymous.ten.c) **MUST** be compiled below) but he's not sure if it's _also_ supposed to run the program; upon a second
as a 32-bit ELF binary! This is because the program only works on 32-bit ELF look at the README.md file it _appears_ that it's supposed to but the binary at least
binaries. If you cannot use `-m32` on the files it acts on the program **will** is modified (hopefully in the correct way that the entry is supposed to do but
almost certainly crash but in no case will it work properly. this cannot be confirmed), at least if 32-bit ELF (that's the only file type
it's supposed to modify).
This means that for macOS it will not work. Why will this not work in macOS? The [anonymous.sh](2001/anonymous.sh) script, run by
Because Apple saw to it to make it very hard to compile 32-bit binaries (thanks [2001/anonymous/try.me.sh](2001/anonymous/try.me.sh), is for now a workaround
Apple for ruining some IOCCC entries! :-) :-( ). This appears to be even harder (until it can be ascertained - and addressed if necessary and possible - if the
with the Apple silicon chip. Thus this entry will not work with macOS unless you program is supposed to run the target executable or not); the script runs the
have an older, out of date version. program on the target executable and then runs the target executable after
modification which in effect emulates the original program (if indeed it did run
the executable as seems likely).
So again this might be only partially resolved but for now this is unknown.
Cody also fixed the [anonymous.ten.c](2001/anonymous/anonymous.ten.c) program
(it also segfaulted) but notes that it **MUST** be compiled as a **32-bit ELF
binary** so it will NOT work in macOS (for example); if you cannot use `-m32` or
even if you can but it cannot compile as an ELF binary (not elf binary :-) )
(but see below) then the `anonymous` program itself will very likely crash if
you run it on the compiled [anonymous.ten.c](2001/anonymous/anonymous.ten.c) but
if nothing else it will not modify the target executable (this part of the fix
at least should be correct).
In fact if you run the program on a 32-bit ELF binary (say compiled from another
system) under macOS it won't even touch it (it shouldn't crash however).
The file `2001/anonymous/anonymous.ten.32` is a pre-compiled
[2001/anonymous.ten.c](2001/anonymous.ten.c) as a 32-bit ELF binary in case you
can run ELF binaries but cannot compile 32-bit binaries.
Other BSD Unices were not tested. Other BSD Unices were not tested.
BTW: if something is _partially_ resolved is it impartial ? :-)
## [2001/bellard](2001/bellard/bellard.c) ([README.md](2001/bellard/README.md)) ## [2001/bellard](2001/bellard/bellard.c) ([README.md](2001/bellard/README.md))
## STATUS: doesn't work with some platforms - please help us fix ## STATUS: doesn't work with some platforms - please help us fix