Skip to content

Commit

Permalink
[IDS] Add support for slider captcha (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pairman authored Mar 6, 2024
1 parent 6eb0ac4 commit 099ca87
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
40 changes: 30 additions & 10 deletions libxduauth/sites/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from ..AuthSession import AuthSession
from ..utils.page import parse_form_hidden_inputs
from ..utils.vcode import _process_vcode
from ..utils.aes import encrypt


Expand All @@ -24,19 +23,40 @@ def __init__(
self.cookies.clear()
page = self.get(
'http://ids.xidian.edu.cn/authserver/login',
params={'service': target, 'type': 'userNameLogin'}
params={'service': target}
).text
is_need_captcha, vcode = self.get(
is_need_captcha = self.get(
'https://ids.xidian.edu.cn/authserver/checkNeedCaptcha.htl',
params={'username': username, '_': int(time.time() * 1000)}
).json()['isNeed'], None
params={'username': username, '_': str(int(time.time() * 1000))}
).json()['isNeed']
if is_need_captcha:
captcha = self.get(
'http://ids.xidian.edu.cn/authserver/getCaptcha.htl',
params={str(int(time.time() * 1000)): ''}
'https://ids.xidian.edu.cn/authserver/common/openSliderCaptcha.htl',
params={'_': str(int(time.time() * 1000))}
)
_process_vcode(Image.open(BytesIO(captcha.content))).show()
vcode = input('验证码:')
# 返回: {
# 'bigImage': ..., # 背景图(base64)
# 'smallImage': ..., # 滑块图(base64)
# 'tagWidth": 93, # 无用, 恒93
# 'yHeight': 0 # 无用, 恒0
# }
img = Image.open(BytesIO(captcha.json()['bigImage']))
img.show()
# move_len: 背景图左侧到滑块目标位置左侧的宽度
move_len = input('滑块位移:')
# canvasLength: canvas宽度, 硬编码280
# moveLength: 按比例缩放后的滑块位移, 有容错
verify = self.post(
'https://ids.xidian.edu.cn/authserver/common/verifySliderCaptcha.htl',
data={
'canvasLength': '280',
'moveLength': str(move_len * 280 // img.width)
}
)
# 返回: {
# 'errorCode': ..., # 验证通过时为1
# 'errorMsg': ... # 验证通过时为'success'
# }
page = BeautifulSoup(page, "lxml")
form = page.findChild(attrs={'id': 'pwdFromId'})
params = parse_form_hidden_inputs(form)
Expand All @@ -47,7 +67,7 @@ def __init__(
data=dict(params, **{
'username': username,
'password': encrypt(password.encode(), enc.encode()),
'captcha': vcode,
'captcha': '',
'rememberMe': 'true'
})
)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setuptools.setup(
name="libxduauth",
version="1.7.6",
version="1.8.0",
author="Frank",
author_email="[email protected]",
description="login utilities for XDU",
Expand Down

0 comments on commit 099ca87

Please sign in to comment.