zoukankan      html  css  js  c++  java
  • Python脚本模拟登陆DVWA

    目录

    requests模拟登陆

    Selenium自动化测试登陆


    环境:python3.7 windows

    requests模拟登陆

    我们登陆DVWA的时候,看似只有一步:访问网站,输入用户名和密码,登陆成功则跳转到新页面。
    其实这中间分了三步。


    1、访问网站的瞬间,浏览器会先向网站的登录页面发送GET请求,然后服务器会返回带有 token 和 cookies 的信息给浏览器

    2、浏览器收到服务器返回的信息后,从中提取token和cookie。
       当我们输入用户名和密码登录页面的时候,这次请求信息包含了我们输入的用户名和密码还有之前提取到的cookie和token信息。
    如果输入的用户名和密码正确的话,服务器回复302重定向,将浏览器重定向到index.php页面。
    如果输入的用户名和密码错误的话,服务器回复302重定向,将浏览器重定向到login.php登录页面

    3、如果用户名和密码正常,浏览器发送GET请求,请求index.php页面
         如果用户名和密码错误,浏览器发送GET请求,请求login.php页面

    所以,我们用python模拟登录DVWA的时候,先模拟向服务器发送登录请求,然后提取出服务器返回的 token 和 cookie。再利用获得到的cookie和token结合我们的用户名和密码进行登录。

    代码实现:

    # -*- coding: utf-8 -*-
    """
    Created on Sat Oct  6 17:01:15 2018
    @author: 小谢
    """
    import urllib
    import requests
    from bs4 import BeautifulSoup
    
    ##第一步,先访问 http://127.0.0.1/login.php页面,获得服务器返回的cookie和token
    def get_cookie_token():
        headers={'Host':'127.0.0.1',
                 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
                 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                 'Accept-Lanuage':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                 'Connection':'keep-alive',
                 'Upgrade-Insecure-Requests':'1'}
        res=requests.get("http://127.0.0.1/login.php",headers=headers)
        cookies=res.cookies
        a=[(';'.join(['='.join(item)for item in cookies.items()]))]   ## a为列表,存储cookie和token
        html=res.text
        soup=BeautifulSoup(html,"html.parser")
        token=soup.form.contents[3]['value']
        a.append(token)
        return a
    
    a=get_cookie_token()              ##a列表中存储了服务器返回的cookie和toke
    
    ##第二步模拟登陆
    def Two():
        headers={'Host':'127.0.0.1',
                 'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
                 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                 'Accept-Lanuage':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
                 'Connection':'keep-alive',
                 'Content-Length':'88',
                 'Content-Type':'application/x-www-form-urlencoded',
                 'Upgrade-Insecure-Requests':'1',
                 'Cookie':a[0],
                 'Referer':'http://127.0.0.1/login.php'}
        values={'username':'admin',
                'password':'password',
                'Login':'Login',
                'user_token':a[1]
            }
        data=urllib.parse.urlencode(values)
        resp=requests.post("http://127.0.0.1/login.php",data=data,headers=headers)
        return 
    Two()
    
    #重定向到index.php
    headers={'Host':'127.0.0.1',
             'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0',
             'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
             'Accept-Lanuage':'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
             'Connection':'keep-alive',
             'Upgrade-Insecure-Requests':'1',
             'Cookie':a[0],
             'Referer':'http://127.0.0.1/login.php'}
    response=requests.get("http://127.0.0.1/index.php",headers=headers)
    print(response.text)

    实际用python模拟的话,他会多发两个包,具体为什么多发包我也不是很清楚,但是最后效果是达到了。成功用python模拟登录了DVWA。

    Selenium自动化测试登陆

    代码实现:

    # -*- coding: utf-8 -*-
    """
    Created on Sun Oct  7 16:35:22 2018
    @author: 小谢
    """
    from selenium import webdriver
    import time
    
    driver=webdriver.Chrome("G:Anaconda3-5.3.0chromedriver.exe")
    print("Login DVWA GO!")
    driver.get("http://127.0.0.1/login.php")
    print("get web in ")
    time.sleep(3)
    driver.find_element_by_name("username").send_keys("admin")   ##找到输入用户名的input框,并且输入用户名
    driver.find_element_by_name("password").send_keys("password")  ##找到输入密码的input框,并且输入密码
    driver.find_element_by_name("Login").click()
    print("登录成功...")
    time.sleep(10)                   ##登录成功后延时十秒退出
    driver.close()
  • 相关阅读:
    GridView中checkbox实现全选[转]
    go 格式化秒 running
    mysql 聚簇索引和非聚簇索引 running
    go context上下文取消 running
    go reflect running
    time.ticker running
    go 数据结构与算法之二分查找 running
    mysql 联合索引最左前缀匹配原则 running
    es 修改 mapping 字段类型 running
    linux 查看虚拟机网卡命令 running
  • 原文地址:https://www.cnblogs.com/csnd/p/11807949.html
Copyright © 2011-2022 走看看