感谢作者:kunyus
1. 通过检测浏览器状态来进行人机检测
使用selenium
打开浏览器
通过 selenium 打开的浏览器是出于测试状态的,和正常的浏览器不太一样。
- 通过
selenium
打开的浏览器哪怕人工手动拖动验证码也会被判断出来是机器操作并显示"出错了"。 - 通过查阅资料发现阿里人机交互会检查
window.navigator.webdriver
是否为空,通过代码处理后可以绕过。
- 正常打开
window.navigator.webdriver
值: -
> window.navigator.webdriver undefined
使用
selenium
打开window.navigator.webdriver
值: -
> window.navigator.webdriver true
绕过代码
-
# 通过浏览器的 devtool 将 .webdriver 属性改为"undefined" driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", { "source": """Object.defineProperty(navigator, 'webdriver', {get: () => undefined})""", })
修改chromedriver.exe驱动字符串
-
使用Notepad++ 打开chromedriver.exe,搜索$cdc_asdjflasutopfhvcZLmcfl_ (将asdjflasutopfhvcZLmcfl替换成任意的22个大小写字符串),保存关闭。
-
这一步主要是因为阿里在全局JS中设置了一段代码来验证是否为selenium,检测到字符串后就会触发滑块验证,所以我们把字符串修改掉以后就成功的进行了伪装。
-
$cdc_asdjflasutopfhvcZLmcfl_
-
2. 通过判断操作方式来进行人机检测
【被识别】使用selenium通过操作元素
当验证码检测到是通过找到拖动元素使用 ActionChains 模拟鼠标滚动后,可以检测出来是机器操作并显示"出错了"。
当检测出来一次后,后续哪怕人工手动操作也会显示"出错了"。
重新刷新网页后人工手动操作可以正常处理。 -
selenium
拖动代码
while True:
for i in range(1,10):
div = driver.find_element_by_id(("nc_%d_n1z"%i))
if div != None:
break
ActionChains(driver).click_and_hold(on_element=div).perform()
time.sleep(random.random()%0.1)
curroffset = random.randint(30,200)
ActionChains(driver).move_to_element_with_offset(to_element=div, xoffset=curroffset, yoffset=10).perform()
【欺骗成功】使用 win32api
模拟鼠标拖动
1.使用 win32api
模拟鼠标拖动方式验证码无法识别,会成功打开导入界面。
win32api
模拟代码
def mouse_down(posX, posY):#鼠标按下不放,从一个坐标点直线拖动到另外一个坐标点
print("down = %d,%d"%(posX, posY))
ctypes.windll.user32.SetCursorPos(posX, posY)#鼠标按下的坐标点
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, posX, posY,posX, posY)
def mouse_move(posX, posY):#鼠标按下不放,从一个坐标点直线拖动到另外一个坐标点
print("move = %d,%d"%(posX, posY))
win32api.mouse_event(win32con.MOUSEEVENTF_ABSOLUTE + win32con.MOUSEEVENTF_MOVE, posX, posY,0,0)
ctypes.windll.user32.SetCursorPos(posX, posY)#鼠标停在最后弹起的坐标点
def mouse_up(posX, posY):#鼠标按下不放,从一个坐标点直线拖动到另外一个坐标点
print("up = %d,%d"%(posX, posY))
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, posX, posY,posX, posY)
ctypes.windll.user32.SetCursorPos(posX, posY)#鼠标停在最后弹起的坐标点
mouse_down(670,880-182)
mouse_up(670,880-182)
time.sleep(1)
mouse_down(670,880-182)
time.sleep(1)
for i in range(1,5):
time.sleep(1)
offsetX = random.randint(300,500)
mouse_move(670+offsetX,880-182)
mouse_up(670,880-182)