質問するログイン新規登録

質問編集履歴

1

入力値と結果の追記、

2021/12/11 09:18

投稿

lkl191
lkl191

スコア14

title CHANGED
File without changes
body CHANGED
@@ -9,19 +9,52 @@
9
9
 
10
10
  ```
11
11
  const jwt = require("jsonwebtoken");
12
+
13
+ const fs = require("fs");
12
14
  const PUBLIC_KEY = fs.readFileSync("../publicKey.pem"); // 公開鍵
13
15
 
16
+ let token =
14
- // test_tokenは[{"alg": "none", "typ": "JWT"}.{"user", "admin"}.]をbase64でエンコードしたものです。
17
+ "eyJhbGciOiJSUzI1NiJ9.eyJ1c2VyIjogIueUsOS4rSJ9.Tvowrr98fHxyHjRtzHKe58cBP7vWUzYvRXRTS1e4zZNWUqDopnRf4fAZ9cbcx8ovvj9NZetqo1lz6eQK2hsF6Q";
15
18
 
16
- const test_token = "eyJhbGciOiAibm9uZSIsICJ0eXAiOiAiSldUIn0.eyJ1c2VyIjogImFkbWluIn0."
19
+ const secretsOrPrivateKey = PUBLIC_KEY;
17
20
 
18
- jwt.verify(testToken, PUBLIC_KEY, (err, decoded) => {
21
+ jwt.verify(token, secretsOrPrivateKey, (err, decoded) => {
19
- if (err) {
22
+ if (err) {
20
- res.status(500).send("jwtの解読に失敗しました");
23
+ console.log("jwtの解読に失敗しました");
21
- return;
22
- } else {
24
+ } else {
23
- res.json(decoded);
25
+ console.log(decoded);
24
- }
26
+ }
25
- });
27
+ });
26
28
  ```
27
- どなたかわかる方はいませんでしょうか?
29
+ どなたかわかる方はいませんでしょうか?
30
+
31
+ テスト用なのでAPI通信はしていません。
32
+ このコマンドから実行しています。
33
+ ```
34
+ node test.js
35
+ ```
36
+ jwtのヘッダとペイロードは
37
+ ```
38
+ header = {"alg": "RS256"}
39
+ payload = {"user": "田中"}
40
+ ```
41
+ になり、
42
+ この結果が返ってきます。
43
+ ```
44
+ { user: '田中' }
45
+ ```
46
+
47
+ ここで、jwtを以下に変更して、署名を削除し、再度base64でエンコードし
48
+ ```
49
+ header = {"alg": "none"}
50
+ payload = {"user": "admin"}
51
+ ```
52
+ 再度base64でエンコードしjwt
53
+ ```
54
+ token = "eyJhbGciOiAibm9uZSIsICJ0eXAiOiAiSldUIn0.eyJ1c2VyIjogImFkbWluIn0."
55
+ ```
56
+ でalg=noneの脆弱性のテストをすると、
57
+ ```
58
+ jwtの解読に失敗しました
59
+ ```
60
+ と出てしまいます。