From e286d885a535bb11aaf5ee1edbba3c76feadf4ed Mon Sep 17 00:00:00 2001 From: glacialcascade <11969863+glacialcascade@users.noreply.github.com> Date: Wed, 15 May 2024 15:31:22 -0400 Subject: [PATCH 1/3] challenge which i am too lazy to solve --- idk/chall.yaml | 15 +++++++++++ idk/idk | Bin 0 -> 17928 bytes idk/idk.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ idk/solve.py | 4 +++ 4 files changed, 87 insertions(+) create mode 100644 idk/chall.yaml create mode 100755 idk/idk create mode 100644 idk/idk.c create mode 100644 idk/solve.py diff --git a/idk/chall.yaml b/idk/chall.yaml new file mode 100644 index 0000000..dc817da --- /dev/null +++ b/idk/chall.yaml @@ -0,0 +1,15 @@ +name: idk +categories: + - rev +value: 100 +flag: bcactf{H1gH_QUAlITy_coDE_ccec60e091ba2} +description: |- + i threw some random stuff at a wall +hints: + - most of these operations are well known +files: + - src: ./idk +authors: + - Marvin +visible: true +# TODO: deployment diff --git a/idk/idk b/idk/idk new file mode 100755 index 0000000000000000000000000000000000000000..a1b9ef9b7b9b6a425a3abe6f40e87775021d83a1 GIT binary patch literal 17928 zcmeHP4{%(?dEa-ZwN93$lO;>YHlQ;|M#ShOS@Iv*7CvLkoa#RqV+VVN_I$cKNmolJ zaXNt^f;tx0qRLKADWoKI`IG($lahwgG-Vv1l3_TTp^2EZiQDNkE)}Ufc8QtL;E)vc z_wDX?K0Tx~lXg0j&aLL&_y5~(zuouV?t5?bTWveHJ1k3Zaf(|7QT-*F<1+$Rt3d#K zV!bHBv0B_H7J$xEJT3QW0I4qL9J8Uz@ST8US4qem@LJ6l3^_uAWH(>&=W7W=VcJ1< zl0=5K!?pr#$B^Zj`aD`LZ15$+W|(b9xp}%yD?DxT$1r8`@`QnGMzzdSm#J@$v8(hbH^L69H5+3~ z7q0J4*il}7(Zb!l*VH#(KO8doWJvu@rM@THv9`IsCtTMPjSU{DJF!YS@CO&){osZV z*DUko*OCskgM5+>72?s43(5G$yk7(!hf?VriV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Qv3&!C%*c%O@);{>c!K*6J&d>$I z8|v%rjo>2d4M#c#yI>cLb>U^DA)~R*KE{I`9r4J0Oko3OgmGk#QDUQC<9sIk&mgUn zamr-&v{gE_KZe)Z+>>s0q3bMNn;nL^-qZ_^>%An;E^J zMH7Y0_WZnoA=^Vm6?^{g1((+HY(H++?+odEHWiMa^%y=389f_dnx79a+@q{@A7#gH z*y02b$|>9P^9hFIMv?2cxBvHz{cT2%pLZ}klx2^V#K-dY40se5`_J6Z%zcfY0~t4& z`!v%hZT22x$B^64`bM0o=F2vF{{I6E)7)U=fbH4eS8euuzh_v)iU!)p?_U~w&cE;& zMFYzX$-r*^O$aD|Y+vconhdKonK|0+&q9)I@AGI?hFG4?Dz@W`Pe%X%oYv!^4lremT2evV= z+c(Z*-_oQF{+TP9$A0rX_SZCPgCqY<{X2}ky`7(>8zTxoRnnBsHR0TjPtIe%o1TE6 z?AIW3WV>u=3sLDlYwRC2!W+P)^MuC(-@gjLpq?U literal 0 HcmV?d00001 diff --git a/idk/idk.c b/idk/idk.c new file mode 100644 index 0000000..5fb60fc --- /dev/null +++ b/idk/idk.c @@ -0,0 +1,68 @@ +#include + +int main() { + // read input in as char array + char flag[40]; + printf("what: "); + scanf("%s", flag); + unsigned int cd[20]; + // Interleave + for (int i = 0; i < 20; i++) { + cd[i] = ((flag[i] * 0x0101010101010101ULL & 0x8040201008040201ULL) * + 0x0102040810204081ULL >> + 49) & + 0x5555 | + ((flag[i + 1] * 0x0101010101010101ULL & 0x8040201008040201ULL) * + 0x0102040810204081ULL >> + 48) & + 0xAAAA; + } + // XOR Swap + int ind1[20] = {11, 19, 14, 1, 3, 5, 18, 13, 0, 17, 6, 7, 8, 16, 12, 10, 4, 9, 15, 2}; + for (int i = 0; i < 19; i += 2) { + cd[ind1[i]] = cd[ind1[i]] ^ cd[ind1[i + 1]]; + cd[ind1[i + 1]] = cd[ind1[i + 1]] ^ cd[ind1[i]]; + cd[ind1[i]] = cd[ind1[i]] ^ cd[ind1[i + 1]]; + } + // Max and min + int ind2[20] = {18, 6, 17, 4, 13, 12, 10, 5, 0, 14, 8, 11, 16, 7, 15, 1, 2, 19, 9, 3}; + unsigned int check = 0; + for (int i = 0; i < 19; i += 2) { + unsigned int c = (cd[ind2[i]] < cd[ind2[i + 1]]); + check += c << i; + unsigned int temp = cd[ind2[i + 1]] ^ ((cd[ind2[i]] ^ cd[ind2[i + 1]]) & -c); + cd[ind2[i]] ^= ((cd[ind2[i]] ^ cd[ind2[i + 1]]) & -c); + cd[ind2[i + 1]] = temp; + } + // Get bit count and remove last bit + for (int i = 0; i < 20; i++) { + unsigned int t = cd[i]; + t = t - ((t >> 1) & 0x55555555); + t = (t & 0x33333333) + ((t >> 2) & 0x33333333); + t = (((t + (t >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; + cd[i] >> 1; + cd[i] += t << 16; + } + // Advance bit permutations + for (int i = 0; i < 20; i++) { + for (int j = 0; j < i; j++) { + unsigned int temp = cd[i] | (cd[i]-1); + cd[i] = (temp + 1) | (((~temp & -~temp) - 1) >> (__builtin_ctz(cd[i]) + 1)); + } + } + + unsigned int goal_check = 267584; + unsigned int goal[20] = {474119, 407714, 472280, 539974, 472220, 471852, 334466, 535866, 670135, 671418, 540229, 406161, 537387, 735727, 539663, 474161, 538139, 474179, 405830, 603047}; + if (check == goal_check) { + for (int i = 0; i < 20; i++) { + if (cd[i] != goal[i]) { + printf("no\n"); + return 0; + } + } + printf("yes\n"); + } else { + printf("no\n"); + } + return 0; +} diff --git a/idk/solve.py b/idk/solve.py new file mode 100644 index 0000000..afac498 --- /dev/null +++ b/idk/solve.py @@ -0,0 +1,4 @@ +goal_check = 267584 +goal = [474119, 407714, 472280, 539974, 472220, 471852, 334466, 535866, 670135, 671418, 540229, 406161, 537387, 735727, 539663, 474161, 538139, 474179, 405830, 603047] + + From 52d73281ab9fd784220ce3e632c5c8a7dfa9af71 Mon Sep 17 00:00:00 2001 From: glacialcascade <11969863+glacialcascade@users.noreply.github.com> Date: Wed, 15 May 2024 15:34:25 -0400 Subject: [PATCH 2/3] better hint --- idk/chall.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idk/chall.yaml b/idk/chall.yaml index dc817da..586f4d3 100644 --- a/idk/chall.yaml +++ b/idk/chall.yaml @@ -6,7 +6,7 @@ flag: bcactf{H1gH_QUAlITy_coDE_ccec60e091ba2} description: |- i threw some random stuff at a wall hints: - - most of these operations are well known + - i didn't invent any of these files: - src: ./idk authors: From 32fa28738d1bd74a64d98f130d273de838a2c652 Mon Sep 17 00:00:00 2001 From: glacialcascade <11969863+glacialcascade@users.noreply.github.com> Date: Tue, 28 May 2024 12:21:57 -0400 Subject: [PATCH 3/3] solved and fixed this chall --- idk/Dockerfile | 3 +++ idk/chall.yaml | 9 ++++++-- idk/idk | Bin 17928 -> 0 bytes idk/idk.c | 12 +++++----- idk/solve.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 idk/Dockerfile delete mode 100755 idk/idk diff --git a/idk/Dockerfile b/idk/Dockerfile new file mode 100644 index 0000000..8beb209 --- /dev/null +++ b/idk/Dockerfile @@ -0,0 +1,3 @@ +FROM gcc:4.9 +COPY idk.c . +RUN gcc -o chall idk.c \ No newline at end of file diff --git a/idk/chall.yaml b/idk/chall.yaml index 586f4d3..adec3f9 100644 --- a/idk/chall.yaml +++ b/idk/chall.yaml @@ -8,8 +8,13 @@ description: |- hints: - i didn't invent any of these files: - - src: ./idk + - src: /chall + dest: idk + container: static +deploy: + static: + build: . authors: - Marvin visible: true -# TODO: deployment +# TODO: verify deployment diff --git a/idk/idk b/idk/idk deleted file mode 100755 index a1b9ef9b7b9b6a425a3abe6f40e87775021d83a1..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17928 zcmeHP4{%(?dEa-ZwN93$lO;>YHlQ;|M#ShOS@Iv*7CvLkoa#RqV+VVN_I$cKNmolJ zaXNt^f;tx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iKr(~XZPrVkmYt6;i^Pk0;eOGKl^EwN&E&DuyPH_vfD4uVz zehU!#+Q(jYf>DCmTZ3=5E%P5IJ`-8+9mOxs?0;NoWG^UwmZCxbmiP|Ls%`V7?|@J7 z*z@px@F_p`JX})zrDDAgNLG0dc!^m0F+cwa{#@}FV?fNsuLa+e<(JWI^YHJQhcEOf z4g_#U2HO0A?R&TEY76+c?G=G-2X}AT)xK2(Zr^#wZCiE*?%2NluC{%FeOqqZ*%mOT zj*mAq(cYy%Bpgfxu?_1EY@!NBV}ZeeNZ7WCYLr#VcNol%~sKm@5IVj zVYifh8&^gK5sVX1z^<@B+wN_&T`WK$={;SDFGk^X3=9|sZ41kos}{`K5tb2Qv3&!C%*c%O@);{>c!K*6J&d>$I z8|v%rjo>2d4M#c#yI>cLb>U^DA)~R*KE{I`9r4J0Oko3OgmGk#QDUQC<9sIk&mgUn zamr-&v{gE_KZe)Z+>>s0q3bMNn;nL^-qZ_^>%An;E^J zMH7Y0_WZnoA=^Vm6?^{g1((+HY(H++?+odEHWiMa^%y=389f_dnx79a+@q{@A7#gH z*y02b$|>9P^9hFIMv?2cxBvHz{cT2%pLZ}klx2^V#K-dY40se5`_J6Z%zcfY0~t4& z`!v%hZT22x$B^64`bM0o=F2vF{{I6E)7)U=fbH4eS8euuzh_v)iU!)p?_U~w&cE;& zMFYzX$-r*^O$aD|Y+vconhdKonK|0+&q9)I@AGI?hFG4?Dz@W`Pe%X%oYv!^4lremT2evV= z+c(Z*-_oQF{+TP9$A0rX_SZCPgCqY<{X2}ky`7(>8zTxoRnnBsHR0TjPtIe%o1TE6 z?AIW3WV>u=3sLDlYwRC2!W+P)^MuC(-@gjLpq?U diff --git a/idk/idk.c b/idk/idk.c index 5fb60fc..f12c24f 100644 --- a/idk/idk.c +++ b/idk/idk.c @@ -7,12 +7,12 @@ int main() { scanf("%s", flag); unsigned int cd[20]; // Interleave - for (int i = 0; i < 20; i++) { - cd[i] = ((flag[i] * 0x0101010101010101ULL & 0x8040201008040201ULL) * + for (int i = 0; i < 20; i ++) { + cd[i] = ((flag[2*i] * 0x0101010101010101ULL & 0x8040201008040201ULL) * 0x0102040810204081ULL >> 49) & 0x5555 | - ((flag[i + 1] * 0x0101010101010101ULL & 0x8040201008040201ULL) * + ((flag[2*i + 1] * 0x0101010101010101ULL & 0x8040201008040201ULL) * 0x0102040810204081ULL >> 48) & 0xAAAA; @@ -40,7 +40,7 @@ int main() { t = t - ((t >> 1) & 0x55555555); t = (t & 0x33333333) + ((t >> 2) & 0x33333333); t = (((t + (t >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24; - cd[i] >> 1; + cd[i] >>= 1; cd[i] += t << 16; } // Advance bit permutations @@ -51,8 +51,8 @@ int main() { } } - unsigned int goal_check = 267584; - unsigned int goal[20] = {474119, 407714, 472280, 539974, 472220, 471852, 334466, 535866, 670135, 671418, 540229, 406161, 537387, 735727, 539663, 474161, 538139, 474179, 405830, 603047}; + unsigned int goal_check = 333072; + unsigned int goal[20] = {466437, 528153, 333852, 530074, 728060, 531211, 400528, 399745, 396035, 530846, 662759, 395326, 397355, 663164, 399371, 532170, 465419, 466482, 532038, 399114}; if (check == goal_check) { for (int i = 0; i < 20; i++) { if (cd[i] != goal[i]) { diff --git a/idk/solve.py b/idk/solve.py index afac498..4a13045 100644 --- a/idk/solve.py +++ b/idk/solve.py @@ -1,4 +1,59 @@ -goal_check = 267584 -goal = [474119, 407714, 472280, 539974, 472220, 471852, 334466, 535866, 670135, 671418, 540229, 406161, 537387, 735727, 539663, 474161, 538139, 474179, 405830, 603047] +# most things stolen from https://graphics.stanford.edu/~seander/bithacks.html +goal_check = 333072 +goal = [466437, 528153, 333852, 530074, 728060, 531211, 400528, 399745, 396035, 530846, 662759, 395326, 397355, 663164, 399371, 532170, 465419, 466482, 532038, 399114] +def bits(n): + c = 0 + while n: + c += 1 + n &= n - 1 + return c + + +# previous bit permutation +for i in range(20): + for j in range(i): + bit_count = bits(goal[i]) + goal[i] -= 1 + while bits(goal[i]) != bit_count: + goal[i] -= 1 + +print(goal) + +# reverse this thing "Get bit count and remove last bit" +for i in range(20): + bit_count = goal[i] >> 16 + goal[i] = goal[i] & ((1 << 16) - 1) + goal[i] <<= 1 + t = bits(goal[i]) + if (t == bit_count): continue + if (t == bit_count - 1): goal[i] += 1; continue + raise Exception("???", t, bit_count) +print(goal) +# reverse Max and min with help of check +ind2 = [18, 6, 17, 4, 13, 12, 10, 5, 0, 14, 8, 11, 16, 7, 15, 1, 2, 19, 9, 3] +for i in range(0, 20, 2): + if not (goal_check & (1 << i)): # if 1st > 2nd in orig + # already correct order (this part sets the 2nd one to the min) + continue + goal[ind2[i]], goal[ind2[i + 1]] = goal[ind2[i + 1]], goal[ind2[i]] +print(goal) +# reverse XOR swap +ind = [11, 19, 14, 1, 3, 5, 18, 13, 0, 17, 6, 7, 8, 16, 12, 10, 4, 9, 15, 2] +for i in range(0, 20, 2): + goal[ind[i]], goal[ind[i + 1]] = goal[ind[i + 1]], goal[ind[i]] + +flag = [] +print(goal) +# uninterleave +for i in range(20): + temp_x = 0 + temp_y = 0 + for j in range(0, 16, 2): + temp_x |= ((goal[i] >> j) & 1) << (j//2) + temp_y |= ((goal[i] >> (j+1)) & 1) << (j//2) + flag.append(temp_x) + flag.append(temp_y) + +print("".join([chr(x) for x in flag])) \ No newline at end of file