0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

picoCTF 2021 writeup ARMssembly 0

Last updated at Posted at 2025-12-22

ARMssembly 0 (Reverse Engineering)

What integer does this program print with arguments 3854998744 and 915131509? File: chall.S Flag format: picoCTF{XXXXXXXX} -> (hex, lowercase, no 0x, and 32 bits. ex. 5614267 would be picoCTF{0055aabb})

添付ファイル
・chall.S

	.arch armv8-a
	.file	"chall.c"
	.text
	.align	2
	.global	func1
	.type	func1, %function
func1:
	sub	sp, sp, #16
	str	w0, [sp, 12]
	str	w1, [sp, 8]
	ldr	w1, [sp, 12]
	ldr	w0, [sp, 8]
	cmp	w1, w0
	bls	.L2
	ldr	w0, [sp, 12]
	b	.L3
.L2:
	ldr	w0, [sp, 8]
.L3:
	add	sp, sp, 16
	ret
	.size	func1, .-func1
	.section	.rodata
	.align	3
.LC0:
	.string	"Result: %ld\n"
	.text
	.align	2
	.global	main
	.type	main, %function
main:
	stp	x29, x30, [sp, -48]!
	add	x29, sp, 0
	str	x19, [sp, 16]
	str	w0, [x29, 44]
	str	x1, [x29, 32]
	ldr	x0, [x29, 32]
	add	x0, x0, 8
	ldr	x0, [x0]
	bl	atoi
	mov	w19, w0
	ldr	x0, [x29, 32]
	add	x0, x0, 16
	ldr	x0, [x0]
	bl	atoi
	mov	w1, w0
	mov	w0, w19
	bl	func1
	mov	w1, w0
	adrp	x0, .LC0
	add	x0, x0, :lo12:.LC0
	bl	printf
	mov	w0, 0
	ldr	x19, [sp, 16]
	ldp	x29, x30, [sp], 48
	ret
	.size	main, .-main
	.ident	"GCC: (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0"
	.section	.note.GNU-stack,"",@progbits

まず、main関数のプロローグがある。
str w0, [x29, 44]str x1, [x29, 32]で引数をスタックに保存している。
w0 = 3854998744 = x29から44bytesx1 = 915131509 = x29から32bytesとなる。

ldr命令で915131509のポインタをx0にロードし、次のaddとldrで915131509の8個目を取得してbl atoiで文字から数字に変換している。つまり9を数字に変換している。変換結果はw19に格納している。
次は915131509の16個目、つまり3854998744の7個目の7を数字に変換しw1に格納している。
bl func1でfunc1を呼び出している。
func1ではw0を[sp, 12]に、w1を[sp, 8]に保存している。
さらに、w0とw1の値を入れ替えて比較し、w1の方が小さいか等しいとき、w0の値を返す。そうでない場合はw1の値を返す。
mainでは返ってきた値をw1に格納しているので、w1 = 3854998744となる。
さらにw19の値をw0に格納して、再度func1を呼び出している。
返ってきた値をw1に格納しているので、w1 = 3854998744となる。
最後に、数値が大きい方の値を出力している。

以上より、渡した引数のうち、大きい値を出力するプログラムであると分かる。

フラグが得られた。

picoCTF{e5c69cd8}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?