From 57c46d46df9e2309dda8fc43a1b0fa9167b351c8 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sat, 20 May 2023 05:06:57 -0700 Subject: [PATCH 1/3] More original macro usage in 2001/anonymous --- 2001/anonymous/anonymous.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/2001/anonymous/anonymous.c b/2001/anonymous/anonymous.c index b846d06ce..007dda79f 100644 --- a/2001/anonymous/anonymous.c +++ b/2001/anonymous/anonymous.c @@ -41,8 +41,7 @@ c L(E(k [(l)V(17)]),0); } V(v){ c E(B[v]); } int pain(char *ck, char **k) { -c (z?(stat(M,&t)?P+=a+'{'?0:3:execv(M,k),a=G,i=P,y=G&255,sprintf(Q,y/'@'-3?"*L(V(%d+%d)+%d,0)":"R[%d]",(y&7),'\r',y/0100-1?0:G),(a+127&&a+'}'&&a+1?(a+61&&a+24&&a+025?(P=a+'H'&&a%061<=0?P:i,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:g:I:g,a>0?a&7:"& "[a%3%2+1],a+72?Q:A(*R),a>0|a<-99?g:I,0) ,(char* ) P):(P=i,i=a+61?a+21?E(I):G:0, D(a%' '+29+"P O,\0 U %d,P=%d,",P,P+i,0,0,0) , p(d,"A'",b)),z):(n=g-4?g%5-1?a+127?G:I:0:(Scan(), *( x86?processor :d)=0, (int)(ss+V(i))),g%5<2?d+=sprintf( d,g%5?" %s=E(~-E(%s)), \0 U %s,"+4*g:"%s=E(E(%s)%c%d),",Q,Q,"+ - "[g],n,0 ),(char* ) P:(a=G-'u'?'!':'=', D(g-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+G), p(d,"A'",b) ,z)))):(Run((z=(char*)&ck,stat(k[1],&t),B=(l)mmap(0,N=i=t.st_size,PROT_READ|PROT_WRITE,1,f=open(k[1],2),0))),(((B+13)[4]=E(B+i/4),run(),P=V(-~i),(B+13)[4]=E(V(17)-4),*(int*)V(021))),sprintf(M,A(.%s%d),k[1],P),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\"",__FILE__,"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\"",M,fflush(0) ),(char* ) P)); } -int -main (int cka, char **k) { char *ck = (char *)cka; +c (z?(stat(M,&t)?P+=a+'{'?0:3:execv(M,k),a=G,i=P,y=G&255,sprintf(Q,y/'@'-3?"*L(V(%d+%d)+%d,0)":"R[%d]",(y&7),'\r',y/0100-1?0:G),(a+127&&a+'}'&&a+1?(a+61&&a+24&&a+025?(P=a+'H'&&a%061<=0?P:i,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:g:I:g,a>0?a&7:"& "[a%3%2+1],a+72?Q:A(*R),a>0|a<-99?g:I,0) ,(char* ) P):(P=i,i=a+61?a+21?E(I):G:0, D(a%' '+29+"P O,\0 U %d,P=%d,",P,P+i,0,0,0) , p(d,"A'",b)),z):(n=g-4?g%5-1?a+127?G:I:0:(Scan(), *( x86?processor :d)=0, (int)(ss+V(i))),g%5<2?d+=sprintf( d,g%5?" %s=E(~-E(%s)), \0 U %s,"+4*g:"%s=E(E(%s)%c%d),",Q,Q,"+ - "[g],n,0 ),(char* ) P:(a=G-'u'?'!':'=', D(g-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+G), p(d,"A'",b) ,z)))):(Run((z=(char*)&ck,stat(k[1],&t),B=(l)mmap(0,N=i=t.st_size,PROT_READ|PROT_WRITE,1,f=open(k[1],2),0))), magic((R[4]=E(B+i/4),run(),P=V(-~i),R[4]=E(V(17)-4),*(int*)V(021))), sprintf(M,A(.%s%d),k[1],P), D("%s %s '-DX=A(%s)' -o %s '-Dmagic=",X,__FILE__,X,M,fflush(0)) ,(char* ) P)); } +int main (int cka, char **k) { char *ck = (char *)cka; (E((ck?pain((char*)cka,k):p(M,A(rm -f .%s),M),*R)));munmap(B,N);close(f);execv(k[1],k); } From 4bc03de321612869aebf855850c6500df95cb6ef Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sat, 20 May 2023 05:49:27 -0700 Subject: [PATCH 2/3] Make 2001/anonymous.c use original macros There is one exception that I'm aware of and that is the U macro (U is referenced only in strings) and this is because it segfaulted and it was a minor change so didn't seem worth investigating further, at least for now (if not for ever). This feels like a new pull request is appropriate here so although it's technically the same idea of the previous pull request this is what should be the final revision (again unless I figure out why U segfaulted). --- 2001/anonymous/README.md | 20 ++++++++++++-------- 2001/anonymous/anonymous.c | 3 ++- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/2001/anonymous/README.md b/2001/anonymous/README.md index 58b77571d..78b726829 100644 --- a/2001/anonymous/README.md +++ b/2001/anonymous/README.md @@ -22,12 +22,12 @@ author's remarks it should be executed). He managed to do this with linux but it will not work with macOS (see [bugs.md](/bugs.md) for why this is); this 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 -needed to be changed was: +Although it took a fair bit of debugging it turns out all that had to change +was: -- `#include ` for `mmap()`. -- have `main()` call another function which is the recursive function; this -prevented `main()` from entering an infinite loop. +- `#include ` for `mmap()` and `munmap()`. +- have `main()` call another function which is no longer a recursive function; +this prevented `main()` from entering an infinite recursive loop. 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 :-) @@ -41,9 +41,13 @@ macOS and linux). There was one other thing that had to be done though. Since the program used to recursively call (infinite recursion, seemingly) `main()` and since that cannot -be done what had to be done is first call `munmap()` and `close()` (else one -would get text file busy error) and then run `execv()` again like the function -`pain()` (was in `main()`) did but with a slight change. +be done what had to be done instead is after the new function (`pain()` since it +was a pain to fix :-) ) returns to first call `munmap()` and `close()` (else +one would get text file busy error) and then the program can call `execv()` again like the +function `pain()` (was in `main()`) did but with a slight change. See commit +`2159caec4677e0f25ad704a74e04c8196fd6c343` for details. Notice that +the location of the calls to `munmap()` and `close() followed by `execv()` do +matter! NOTE: there might be educational value to see the progress of this fix; if you wish to see, try: diff --git a/2001/anonymous/anonymous.c b/2001/anonymous/anonymous.c index 007dda79f..5f52678c2 100644 --- a/2001/anonymous/anonymous.c +++ b/2001/anonymous/anonymous.c @@ -41,7 +41,8 @@ c L(E(k [(l)V(17)]),0); } V(v){ c E(B[v]); } int pain(char *ck, char **k) { -c (z?(stat(M,&t)?P+=a+'{'?0:3:execv(M,k),a=G,i=P,y=G&255,sprintf(Q,y/'@'-3?"*L(V(%d+%d)+%d,0)":"R[%d]",(y&7),'\r',y/0100-1?0:G),(a+127&&a+'}'&&a+1?(a+61&&a+24&&a+025?(P=a+'H'&&a%061<=0?P:i,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:g:I:g,a>0?a&7:"& "[a%3%2+1],a+72?Q:A(*R),a>0|a<-99?g:I,0) ,(char* ) P):(P=i,i=a+61?a+21?E(I):G:0, D(a%' '+29+"P O,\0 U %d,P=%d,",P,P+i,0,0,0) , p(d,"A'",b)),z):(n=g-4?g%5-1?a+127?G:I:0:(Scan(), *( x86?processor :d)=0, (int)(ss+V(i))),g%5<2?d+=sprintf( d,g%5?" %s=E(~-E(%s)), \0 U %s,"+4*g:"%s=E(E(%s)%c%d),",Q,Q,"+ - "[g],n,0 ),(char* ) P:(a=G-'u'?'!':'=', D(g-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+G), p(d,"A'",b) ,z)))):(Run((z=(char*)&ck,stat(k[1],&t),B=(l)mmap(0,N=i=t.st_size,PROT_READ|PROT_WRITE,1,f=open(k[1],2),0))), magic((R[4]=E(B+i/4),run(),P=V(-~i),R[4]=E(V(17)-4),*(int*)V(021))), sprintf(M,A(.%s%d),k[1],P), D("%s %s '-DX=A(%s)' -o %s '-Dmagic=",X,__FILE__,X,M,fflush(0)) ,(char* ) P)); } +c (z?(stat(M,&t)?P+=a+'{'?0:3:execv(M,k),a=G,i=P,y=G&255,sprintf(Q,y/'@'-3?"*L(V(%d+%d)+%d,0)":"R[%d]",(y&7),'\r',y/0100-1?0:G),(a+127&&a+'}'&&a+1?(a+61&&a+24&&a+025?(P=a+'H'&&a%061<=0?P:i,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:g:I:g,a>0?a&7:"& "[a%3%2+1],a+72?Q:A(*R),a>0|a<-99?g:I,0) ,(char* ) P):(P=i,i=a+61?a+21?E(I):G:0, D(a%' '+29+"P O,\0 U %d,P=%d,",P,P+i,0,0,0) , p(d,"A'",b)),z):(n=g-4?g%5-1?a+127?G:I:0:(Scan(), *( x86?processor :d)=0, (int)(ss+V(i))),g%5<2?d+=sprintf( d,g%5?" %s=E(~-E(%s)), \0 U %s,"+4*g:"%s=E(E(%s)%c%d),",Q,Q,"+ - "[g],n,0 ),(char* ) P:(a=G-'u'?'!':'=', D(g-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+G), p(d,"A'",b) ,z)))):(Run((z=(char*)&ck,stat(k[1],&t),B=(l)mmap(0,N=i=t.st_size,PROT_READ|PROT_WRITE,1,f=open(k[1],2),0))), magic((R[4]=E(B+i/4),run(),P=V(-~i), R[4]=E(V(17)-4),*(int*)V(021))), sprintf(M,A(.%s%d),k[1],P), D("%s %s '-DX=A(%s)' -o %s '-Dmagic=",X,__FILE__,X,M,fflush(0)) ,(char* ) P)); +} int main (int cka, char **k) { char *ck = (char *)cka; (E((ck?pain((char*)cka,k):p(M,A(rm -f .%s),M),*R)));munmap(B,N);close(f);execv(k[1],k); } From ed49d1dbad9f241aceef23c3d115243d190b69d2 Mon Sep 17 00:00:00 2001 From: Cody Boone Ferguson <53008573+xexyl@users.noreply.github.com> Date: Sat, 20 May 2023 05:54:06 -0700 Subject: [PATCH 3/3] Final :-) updates to 2001/anonymous/README.md Added a couple git diff commands to try and moved the INABIAF and the warning from the author to below the judges' comments as already most people probably will want to skip past long before to the next section. Perhaps the fix should be moved to under the judges' comments as well but I don't know. --- 2001/anonymous/README.md | 44 ++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/2001/anonymous/README.md b/2001/anonymous/README.md index 78b726829..c53fc68e3 100644 --- a/2001/anonymous/README.md +++ b/2001/anonymous/README.md @@ -58,10 +58,36 @@ git diff 93aa8d79f208dcccc3c5a2370a727b5cf64e9c53..c48629017117379a52b1a512ef8f2 git diff c48629017117379a52b1a512ef8f2593ca9569c8..efdee208a2bc650256637b9357ddfd0de82d2f41 anonymous.c git diff efdee208a2bc650256637b9357ddfd0de82d2f41..e9a3f77ea3b209e63ac3f9c06bb84ad86e5ea706 anonymous.c git diff d2a42f42e8f477f29e9d5ed09ce2bb349eaf7397..2159caec4677e0f25ad704a74e04c8196fd6c343 anonymous.c +git diff 2159caec4677e0f25ad704a74e04c8196fd6c343..4bc03de321612869aebf855850c6500df95cb6ef anonymous.c +``` + +Finally to see from start to finish: + +```sh +git diff d2a42f42e8f477f29e9d5ed09ce2bb349eaf7397..4bc03de321612869aebf855850c6500df95cb6ef anonymous.c ``` Thank you Cody for your assistance! +## Try: + +```sh +./try.me.sh +``` + +What happens if the x86 program has already been modified by this program? The +judges' remarks below might give you a hint! + +What happens if you try it on another file like [anonymous.c](anonymous.c)? Can +you recompile it okay? What if you run it on `anonymous` itself? Can you run the +program successfully after it without recompiling? + + +## Judges' remarks: + +Is emulation the sincerest form of flattery? This small program does +quite a lot of bit twiddling. + ### INABIAF - it's not a bug it's a feature! :-) If you do not specify a 32-bit ELF binary as the arg of this program it will @@ -85,24 +111,6 @@ 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. -## Try: - -```sh -./try.me.sh -``` - -What happens if the x86 program has already been modified by this program? The -judges' remarks below might give you a hint! - -What happens if you try it on another file like [anonymous.c](anonymous.c)? Can -you recompile it okay? What if you run it on `anonymous` itself? Can you run the -program successfully after it without recompiling? - - -## Judges' remarks: - -Is emulation the sincerest form of flattery? This small program does -quite a lot of bit twiddling. ## Author's remarks: