Writeup 2026_0828

Let’s analyze system calls - AlpacaHack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdio.h>
#include <stdlib.h>

int main() {
unsigned long result_1, result_2, sum, input;

asm("syscall"
: /* No outputs. */
: "a"(318), "D"(&result_1), "S"(sizeof(result_1)), "d"(0)
: "rcx", "r11", "memory");

asm("syscall"
: "=a"(result_2)
: "a"(102)
: "rcx", "r11");

sum = result_1 + result_2;

printf("result_1: %lu\n", result_1);
printf("What will be the sum? ");
fflush(stdout);
scanf("%lu", &input);

if (input == sum) {
const char *env_flag = getenv("FLAG");
printf("Correct! FLAG: %s\n", env_flag ? env_flag : "Alpaca{DUMMY}");
}
else {
puts("Incorrect...");
}
}

result1とresult2の合計を当てるクイズで、実行時にresult1は教えてもらえる。
syscallを直接呼び出しているらしいので318と102について調べてみる。
Linux Syscall Tableによると、それぞれgetrandomとgetuidらしい。
uidがどこかに書かれてると思うのでDockerfileを覗くと

1
2
3
4
5
6
7
8
9
10
11
# NOTE: This hash is pinned to avoid version and environment differences.
FROM ubuntu:26.04@sha256:7b202b0e2e0028c6250f5fcf41d04df492d145a1654c6995a6553f0c1f6f1960
RUN apt-get update && apt-get install -yq socat

WORKDIR /app
COPY chal ./

# Sets UID and GID
USER 404:404

CMD ["socat", "-T30", "tcp-listen:1337,fork,reuseaddr", "exec:'./chal',stderr"]

UID/GIDともに404で実行されているのでgetuidは404になる。
result1に404を足した値を入力したらフラグが出る。

a fact of CTF - AlpacaHack

1
2
3
4
5
6
7
8
9
# all prime numbers less than 300
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293]
assert len(flag) <= len(primes)

ct = 1
for i, c in enumerate(flag):
ct *= primes[i] ** (ord(c))

print(hex(ct))

指数が分かればよいので、outputを素因数分解した。

super-tomato - AlpacaHack

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
p = getPrime(2048)

print(f"I think 🍅 equals to prime.")
print(f"here is my 🍅: {p}")
print("I need ONE 🍅!!!")
choice = int(input("what is your 🍅> "))

if choice <= 0:
print("I need a POSITIVE 🍅!!!")
exit()

a = getPrime(1024)

if pow(a, choice, p) == 1:
print(f"here is the flag: {flag}")
else:
print("NO NO 🍅")

フェルマーの小定理で解いた。

Author

kiku

Posted on

2026-08-29

Updated on

2026-08-29

Licensed under