阿里人机检测验证码的自动化操作尝试

1. 通过检测浏览器状态来进行人机检测 使用selenium打开浏览器

通过 selenium 打开的浏览器是出于测试状态的,和正常的浏览器不太一样。
  1. 通过 selenium 打开的浏览器哪怕人工手动拖动验证码也会被判断出来是机器操作并显示"出错了"。
  2. 通过查阅资料发现阿里人机交互会检查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})""", })

2. 通过判断操作方式来进行人机检测 【被识别】使用selenium通过操作元素
  1. 当验证码检测到是通过找到拖动元素使用 ActionChains 模拟鼠标滚动后,可以检测出来是机器操作并显示"出错了"。
  2. 当检测出来一次后,后续哪怕人工手动操作也会显示"出错了"。
  3. 重新刷新网页后人工手动操作可以正常处理。
【阿里人机检测验证码的自动化操作尝试】阿里人机检测验证码的自动化操作尝试
文章图片

selenium 拖动代码
while True: for i in range(1,10): div = driver.find_element_by_id(("nc_%d_n1z"%i)) if div != None: breakActionChains(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)

    推荐阅读