picoCTF Practice Writeup 3

picoCTF Practice Writeup 3
[picoGym Practice Challenges page=3](https://play.picoctf.org/practice?page=3) の11問を勉強した記録

このページの難問は，
626 solves の New Caesar （Cryptographyというよりpython力を問う問題）
362 solves の ARMssembly 1　（ARM Assembly）

特に
126 solves の Cache Me Outside　はお手上げ。なので11問だけ掲載。

# Warmed Up
Category: General Skills
Description:
What is 0x3D (base 16) in decimal (base 10)?
Hints:
1. Submit your answer in our flag format. For example, if your answer was '22', you would submit 'picoCTF{22}' as the flag.

Solution:
計算するだけ



# The Numbers
Category: Cryptography
Description:
The numbers... what do they mean?
Hints:
1. The flag is in the format PICOCTF{}
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/eadb15a4-4f6e-e416-a398-66e1fb63269f.png)

Solution:

```
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
1 2 3 4 5 6 7 8 9 1011121314151617181920212223242526
PICOCTF{THENUMBERSMASON}
```



# 2Warm
Category: General Skills
Description:
Can you convert the number 42 (base 10) to binary (base 2)?
Hints:
Submit your answer in our competition's flag format. For example, if your answer was '11111', you would submit 'picoCTF{11111}' as the flag.

Solution:
計算するだけ



# Wireshark doo dooo do doo...
Category: Forensics
Description:
Can you find the flag? shark1.pcapng.
Hints:
(None)

Solution:
プロトコル階層
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/451b0474-6cb6-830b-3f6c-bf61a29cd38a.png)
Line-based text data が目立つ
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/50d4867c-1ec0-a43e-e6ff-ce6770d8fd16.png)
ROT13ビンゴ



# speeds and feeds
Category: Reverse Engineering
Description:
There is something on my shop network running at nc mercury.picoctf.net 59953, but I can't tell what it is. Can you?
Hints:
What language does a CNC machine use?

Solution:
工作機械の言語なんて初めて見ました。

https://www.kousakukikai.tech/cnc-machine-tools/

https://www.kousakukikai.tech/nc-gcode/

https://ncviewer.com/

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/849be0f4-2791-ed82-71ae-c0952089880d.png)
CNCにはGコードだけでなくMコード，Sコード，Tコード，Fコードとかもあるみたいです。



# Shop
Category: Reverse Engineering
Description:
Best Stuff - Cheap Stuff, Buy Buy Buy... Store Instance: source. The shop is open for business at nc mercury.picoctf.net 42159.
Hints:
1. Always check edge cases when programming

```
$ nc mercury.picoctf.net 42159
Welcome to the market!
=====================
You have 40 coins
        Item            Price   Count
(0) Quiet Quiches       10      12
(1) Average Apple       15      8
(2) Fruitful Flag       100     1
(3) Sell an Item
(4) Exit
Choose an option:
```

Solution:
Fruitful Flagは100円だが40円しか持っていない。
Quiet Quichesを-100個買うと，お金が1000円増える。
残高 = 残高 - ( 10 * -100 ) 
Fruitful Flagを買う

```
$ nc mercury.picoctf.net 42159
Welcome to the market!
=====================
You have 40 coins
        Item            Price   Count
(0) Quiet Quiches       10      12
(1) Average Apple       15      8
(2) Fruitful Flag       100     1
(3) Sell an Item
(4) Exit
Choose an option:
0
How many do you want to buy?
-100
You have 1040 coins
        Item            Price   Count
(0) Quiet Quiches       10      112
(1) Average Apple       15      8
(2) Fruitful Flag       100     1
(3) Sell an Item
(4) Exit
Choose an option:
2
How many do you want to buy?
1
Flag is:  [112 105 99 111 67 84 70 123 98 52 100 95 98 114 111 103 114 97 109 109 101 114 95 55 57 55 98 50 57 50 99 125]
```



# Scavenger Hunt
Category: Web Exploitation
Description:
There is some interesting information hidden around this site http://mercury.picoctf.net:39491/. Can you find it?
Hints:
You should have enough hints to find the files, don't run a brute forcer.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/2d9047b5-042b-0845-2d22-08c66ba774c7.png)

Solution:
ソース
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/12d8baf7-5c0f-2edd-9fdb-c304bbf9c385.png)

CSS
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/2b7bc713-219d-c04d-1761-e517fa77e403.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/84fa04bc-1b86-afb2-f95e-7f38c447228e.png)

js
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/fffa97c1-c82d-4cc2-206e-e9699380e799.png)
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/1abde4be-8c82-f577-54d4-759232212fe1.png)

ヒントだけ
How can I keep Google from indexing my website?

robots.txt
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/5f561a00-1183-9aac-3189-2e2cee90adef.png)

次のヒント
I think this is an apache server... can you Access the next flag?

apache独自といえば
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/e61e1818-021c-2ee6-c528-eb17ce157629.png)

次のヒント
I love making websites on my Mac, I can Store a lot of information there.

Macで編集した時に残るのは？　自動バックアップか
.DocumentRevisions-V100
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/31bb647d-4c45-1f8c-0130-7e875653d171.png)
だめ

.DS_Store
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/7d8c6e5e-407e-7585-908f-1ab0b0234e1f.png)
ビンゴ



# MacroHard WeakEdge
Category: Forensics
Description:
I've hidden a flag in this file. Can you find it? Forensics is fun.pptm
Hints
(None)

Solution:
pptm = PowerPoint マクロ有効プレゼンテーション

常とう手段，拡張子をzipに変える
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/a6c7e604-337b-54f3-1608-31cb3066f469.png)
hiddenなる怪しいファイル発見
ビンゴ




# New Caesar
良問です。
[picoCTF 2021 New Caesar Writeup](https://qiita.com/housu_jp/items/f52d3f93193ecded043d)



# ARMssembly 1
Category: Reverse Engineering
Description:
For what argument does this program print `win` with variables 79, 7 and 3? File: chall_1.S Flag format: picoCTF{XXXXXXXX} -> (hex, lowercase, no 0x, and 32 bits. ex. 5614267 would be picoCTF{0055aabb})
Hints:
Shifts

```armasm
	.arch armv8-a
	.file	"chall_1.c"
	.text
	.align	2
	.global	func
	.type	func, %function
func:
	sub	sp, sp, #32
	str	w0, [sp, 12]
	mov	w0, 79
	str	w0, [sp, 16]
	mov	w0, 7
	str	w0, [sp, 20]
	mov	w0, 3
	str	w0, [sp, 24]
	ldr	w0, [sp, 20]
	ldr	w1, [sp, 16]
	lsl	w0, w1, w0
	str	w0, [sp, 28]
	ldr	w1, [sp, 28]
	ldr	w0, [sp, 24]
	sdiv	w0, w1, w0
	str	w0, [sp, 28]
	ldr	w1, [sp, 28]
	ldr	w0, [sp, 12]
	sub	w0, w1, w0
	str	w0, [sp, 28]
	ldr	w0, [sp, 28]
	add	sp, sp, 32
	ret
	.size	func, .-func
	.section	.rodata
	.align	3
.LC0:
	.string	"You win!"
	.align	3
.LC1:
	.string	"You Lose :("
	.text
	.align	2
	.global	main
	.type	main, %function
main:
	stp	x29, x30, [sp, -48]!
	add	x29, sp, 0
	str	w0, [x29, 28]
	str	x1, [x29, 16]
	ldr	x0, [x29, 16]
	add	x0, x0, 8
	ldr	x0, [x0]
	bl	atoi
	str	w0, [x29, 44]
	ldr	w0, [x29, 44]
	bl	func
	cmp	w0, 0
	bne	.L4
	adrp	x0, .LC0
	add	x0, x0, :lo12:.LC0
	bl	puts
	b	.L6
.L4:
	adrp	x0, .LC1
	add	x0, x0, :lo12:.LC1
	bl	puts
.L6:
	nop
	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
```

Solution:
まず，問題から
第1引数 79
第2引数 7
第3引数 3
の情報が与えられている。

lsl と sdiv が目にとまる

https://www.keil.com/support/man/docs/armasm/armasm_dom1361289876185.htm

https://www.keil.com/support/man/docs/armasm/armasm_dom1361289894402.htm

重要な部分だけ解説すると

```armasm
	mov	w0, 79     
	str	w0, [sp, 16]  # sp+16に 79 が保存される

	mov	w0, 7
	str	w0, [sp, 20]  # sp+20に 7 が保存される

	mov	w0, 3
	str	w0, [sp, 24]  # sp+24に 3 が保存される

	ldr	w0, [sp, 20]
	ldr	w1, [sp, 16]
	lsl	w0, w1, w0    # wo(10112) = w1(79) << w0(7)
	str	w0, [sp, 28]  # sp+28に 10112 が保存される

	ldr	w1, [sp, 28]
	ldr	w0, [sp, 24]
	sdiv	w0, w1, w0    # w0 = w1(10112) / w0(3)
```

( 79 << 7 ) / 3 =  3370 = 0xd2a
picoCTF{00000d2a}




# Some Assembly Required 1
Category: Web Exploitation
Description:
http://mercury.picoctf.net:26318/index.html
Hints:
(None)

Solution:
ページのソース

```html
<html>
<head>
	<meta charset="UTF-8">
	<script src="G82XCw5CX3.js"></script>
</head>
<body>
	<h4>Enter flag:</h4>
	<input type="text" id="input"/>
	<button onclick="onButtonPress()">Submit</button>
	<p id="result"></p>
</body>
</html>
```


javascryptを見てみる

```javascript:G82XCw5CX3.js
const _0x402c = ['value', '2wfTpTR', 'instantiate', '275341bEPcme', 'innerHTML', '1195047NznhZg', '1qfevql', 'input', '1699808QuoWhA', 'Correct!', 'check_flag', 'Incorrect!', './JIFxzHyW8W', '23SMpAuA', '802698XOMSrr', 'charCodeAt', '474547vVoGDO', 'getElementById', 'instance', 'copy_char', '43591XxcWUl', '504454llVtzW', 'arrayBuffer', '2NIQmVj', 'result'];
const _0x4e0e = function (_0x553839, _0x53c021) {
  _0x553839 = _0x553839 - 0x1d6;
  let _0x402c6f = _0x402c[_0x553839];
  return _0x402c6f;
};
(function (_0x76dd13, _0x3dfcae) {
  const _0x371ac6 = _0x4e0e;
  while (!![]) {
    try {
      const _0x478583 = -parseInt(_0x371ac6(0x1eb)) + parseInt(_0x371ac6(0x1ed)) + -parseInt(_0x371ac6(0x1db)) * -parseInt(_0x371ac6(0x1d9)) + -parseInt(_0x371ac6(0x1e2)) * -parseInt(_0x371ac6(0x1e3)) + -parseInt(_0x371ac6(0x1de)) * parseInt(_0x371ac6(0x1e0)) + parseInt(_0x371ac6(0x1d8)) * parseInt(_0x371ac6(0x1ea)) + -parseInt(_0x371ac6(0x1e5));
      if (_0x478583 === _0x3dfcae) break;
      else _0x76dd13['push'](_0x76dd13['shift']());
    } catch (_0x41d31a) {
      _0x76dd13['push'](_0x76dd13['shift']());
    }
  }
}(_0x402c, 0x994c3));
let exports;
(async() => {
  const _0x48c3be = _0x4e0e;
  let _0x5f0229 = await fetch(_0x48c3be(0x1e9)),
    _0x1d99e9 = await WebAssembly[_0x48c3be(0x1df)](await _0x5f0229[_0x48c3be(0x1da)]()),
    _0x1f8628 = _0x1d99e9[_0x48c3be(0x1d6)];
  exports = _0x1f8628['exports'];
})();

function onButtonPress() {
  const _0xa80748 = _0x4e0e;
  let _0x3761f8 = document['getElementById'](_0xa80748(0x1e4))[_0xa80748(0x1dd)];
  for (let _0x16c626 = 0x0; _0x16c626 < _0x3761f8['length']; _0x16c626++) {
    exports[_0xa80748(0x1d7)](_0x3761f8[_0xa80748(0x1ec)](_0x16c626), _0x16c626);
  }
  exports['copy_char'](0x0, _0x3761f8['length']), exports[_0xa80748(0x1e7)]() == 0x1 ? document[_0xa80748(0x1ee)](_0xa80748(0x1dc))[_0xa80748(0x1e1)] = _0xa80748(0x1e6) : document[_0xa80748(0x1ee)](_0xa80748(0x1dc))[_0xa80748(0x1e1)] = _0xa80748(0x1e8);
}
```

WebAssemblyって何だ？

https://developer.mozilla.org/ja/docs/WebAssembly

「wasm ファイルを取得、ロード、実行するため先述した WebAssembly JavaScript API を呼び出すロジックも含んでいます。」とか書かれています。

デベロッパーツールで見ていると

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/b8a520a8-4828-fd35-f580-2bce320f067d.png)

wasmを見つけることができます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1163572/884a2983-ae5b-a4d1-c2e3-ea32236957ab.png)

よくわからないけどフラグ見えた。（ESP？）