使用python发送和接收http请求,还需要发送会话cookie

发布于 2024-10-16 09:58:00 字数 2506 浏览 3 评论 0原文

我正在我的服务器上测试我的登录功能(在 x 次尝试失败后自动禁止 ip)。我发现了一个简单的 python 脚本,它发送如下 http 请求:

import urllib, urllib2, re, os, sys, cookielib

def main():
    host = 'http://192.168.1.133/intro.php'
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)'
    headers = {'User-Agent': user_agent}
    username = 'admin'
    error =  '<p class="highlight">Login failed. Please try again.</p>'
    word_file = 'pass.txt'
    word_pos = 1

    file = open(word_file, 'r')
    word_list = file.read().split('\n')
    file.close

    for word in word_list:
        form = {'username': username, 'password': word}

        print 'Tring password: %s (%d/%d)' % (word, word_pos, len(word_list))
        word_pos = word_pos + 1

        data = urllib.urlencode(form)
        request = urllib2.Request(host, data, headers)
        response = urllib2.urlopen(request)

        size = response.headers.get("content-length")

        #print 'response %s' % (response.read())
        #print 'size: %s' % (size)

        if not re.search(error, response.read()):
            print 'Login Combination: [%s:%s]' % (username, word)
            save_combo = open('login comination.txt', 'w')
            save_combo.write(username + ':' + word)
            save_combo.close
            break

if __name__ == '__main__':
    main()

但我的 intro.php 文件如下所示:

$a = session_id();
if ($a == '') session_start();

我必须发送会话 cookie。这是一个典型的登录请求:

POST /intro.php HTTP/1.1
Host: 192.168.1.133
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-2.01 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Proxy-Connection: keep-alive
Referer: http://192.168.1.133/intro.php
Cookie: __utma=48024167.372656243.1297092510.1297169678.1297174274.6; __utmz=48024167.1297117542.3.2.utmcsr=search.genieo.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmc=48024167; lang=en_US; PHPSESSID=7v8tt2ln0kaavo0ko7a7pklho6; __utmb=48024167.1.10.1297174274
Content-Type: application/x-www-form-urlencoded
Content-Length: 39

username=yeyeye&pwd=123456&submit=Login

我想创建 cookie 变量,这样我就可以在每个请求中发送 cookie,而不仅仅是我现在发送的 POST、Host、User-Agent。我刚刚安装了 python,所以我不太确定如何使用 cookielib ..

I am testing my login function at my server (auto ip ban after x failed attempts). I found a simple python script that send http request like this:

import urllib, urllib2, re, os, sys, cookielib

def main():
    host = 'http://192.168.1.133/intro.php'
    user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)'
    headers = {'User-Agent': user_agent}
    username = 'admin'
    error =  '<p class="highlight">Login failed. Please try again.</p>'
    word_file = 'pass.txt'
    word_pos = 1

    file = open(word_file, 'r')
    word_list = file.read().split('\n')
    file.close

    for word in word_list:
        form = {'username': username, 'password': word}

        print 'Tring password: %s (%d/%d)' % (word, word_pos, len(word_list))
        word_pos = word_pos + 1

        data = urllib.urlencode(form)
        request = urllib2.Request(host, data, headers)
        response = urllib2.urlopen(request)

        size = response.headers.get("content-length")

        #print 'response %s' % (response.read())
        #print 'size: %s' % (size)

        if not re.search(error, response.read()):
            print 'Login Combination: [%s:%s]' % (username, word)
            save_combo = open('login comination.txt', 'w')
            save_combo.write(username + ':' + word)
            save_combo.close
            break

if __name__ == '__main__':
    main()

But my intro.php file look like this:

$a = session_id();
if ($a == '') session_start();

I have to send session cookies. This is a typical login request:

POST /intro.php HTTP/1.1
Host: 192.168.1.133
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-2.01 Firefox/3.6.13
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Proxy-Connection: keep-alive
Referer: http://192.168.1.133/intro.php
Cookie: __utma=48024167.372656243.1297092510.1297169678.1297174274.6; __utmz=48024167.1297117542.3.2.utmcsr=search.genieo.com|utmccn=(referral)|utmcmd=referral|utmcct=/; __utmc=48024167; lang=en_US; PHPSESSID=7v8tt2ln0kaavo0ko7a7pklho6; __utmb=48024167.1.10.1297174274
Content-Type: application/x-www-form-urlencoded
Content-Length: 39

username=yeyeye&pwd=123456&submit=Login

I want to make cookie variables so I can send cookies with every request, not just POST, Host, User-Agent that I send now. I just installed python so I am not quite sure how to use cookielib..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉风有信 2024-10-23 09:58:00

替换

headers = {'User-Agent': user_agent}

headers = {
    'User-Agent': user_agent,
    'Cookie': '__utma=48024167.372656243.1297092510.1297169678.1297174274.6...'
}

Replace

headers = {'User-Agent': user_agent}

with

headers = {
    'User-Agent': user_agent,
    'Cookie': '__utma=48024167.372656243.1297092510.1297169678.1297174274.6...'
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文