2017-09-21 9 views
2

私がやっていること:クライアントがすでにソケットであるループでclient.send(req)経由でgetリクエストを送信した後に、client.recv(4096)サーバーに接続します。ループ内のPython socket.recv一度だけ受信

私がしたいこと:本質的には、ループの各繰り返しでテストされるファイルがサーバーに存在するかどうかを確認したいだけです。

実行中のこと:ループは最初の反復でのみ応答を取得します。

Backstory &その他の情報:私はすでに殴られたハッキン​​グの課題を解決しようとしています以下は私のコードですが、できる限り私がコメントしています。私はPyPyを使用しています。私が言いたいことを忘れていたことや不明な点については、自由に質問してください。

私が試し持っているもの:を私が試してみました:より複雑しばらく使用すると、StackOverflowの、非ブロックソケットで混乱して読書を少し探して、受信されているすべてのデータを収集しようとするループ。

可能な代替ルート:要求ライブラリは、ソケットよりも私をここで助けてくれますか?

マイスクリプト:事前に

# I need socket obviously, and I am using time as a method to slow the process down just to wait for the server 
import socket 
import time 

# My dictionaries of things to try ('pre' is not yet integrated) 
exts = ['conf', 'bak', 'swp', 'txt', 'old', 'tar', 'gz', 'bz2', 'zip'] 
pre = ['old', 'bak', 'backup', 'copyof'] 

# Create and connect the socket 
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
client.connect(("challenge01.root-me.org", 80)) 

# Send a test request 
client.send("HEAD/HTTP/1.1\r\nHost: challenge01.root-me.org\r\n\r\n") 
resp = client.recv(4096) 

# Tell myself it's working 
if "200" in resp: 
    print "[i] Connection is working." 

# Setting up my request for the loop 
head = "GET /realiste/ch1/login.php" 
http = " HTTP/1.1\r\nHost: challenge01.root-me.org\r\n\r\n" 

# Getting my lists to hold the requests and responses ready 
urls = [] 
respers = [] 

# Saving myself some typing 
def store(request, response): 
    urls.append(request) 
    respers.append(response) 

# Here's the main loop. It's looping through my dictionary (techinically a list) 
# of extensions. 
for ext in exts: 

    # It creates a request with each iteration, in essence adding .old or .bak to the file in the request 
    req = head + '.' + ext + http 

    # Send it off to the server 
    client.send(req) 

    # So I set my response variable to "" and then start grabbing data 
    # If it has data, I put it in my response 
    # If it's empty, I move on out of this while loop and back into the main for loop 
    # Thing is, I may get a file or a large response. If either happen, I don't want the entire thing. 
    # So I set up a little if/else to look for a connection code. As soon as 
    # it finds it, it cuts the while loop. 
    # To summarize, once it gets the entire response, or once it finds a connection code, 
    # it stops the loop. 
    resp = "" 
    while True: 
     currentResp = client.recv(4096) 
     if currentResp != "": 
      resp += currentResp 
      if "200" in resp or "400" in resp or "404" in resp or "502" in resp: 
       store(req, resp) 
       break 
      else: 
       continue 
     else: 
      break 

    # Give the server a breather 
    time.sleep(0.5) 

# Fancy shmancy output 
for search in range(0, len(respers)): 
    ecx = 1 
    if "200" in respers[search]: 
     print "[" + str(ecx) + "] " + urls[search].replace("\n", "").replace("\r", "") 
     print "|__ ::: " + respers[search].splitlines()[0] 
     print "|" 

# Finish. 
print "[*] Done." 

ありがとう!

答えて

0

リクエストでこの問題が修正されました。調整コードは次のとおりです。

import requests 
import time 

exts = ['conf', 'bak', 'swp', 'txt', 'old', 'tar', 'gz', 'bz2', 'zip'] 
pre = ['old', 'bak', 'backup', 'copyof'] 

session = requests.head("http://challenge01.root-me.org/") 
if session.status_code == 200: 
    print "[i] Connection is working." 

http = "http://challenge01.root-me.org" 
file = "/realiste/ch1/login.php" 
urls = [] 
codes = [] 

def store(request, code): 
    urls.append(request) 
    codes.append(code) 

for ext in exts: 
    req = http + file + "." + ext 
    connection = requests.head(req) 
    store(req, connection.status_code) 
    time.sleep(0.1) 

for search in range(0, len(codes)): 
    ecx = 1 
    if codes[search] != 404: 
     print "=============" 
     print "[" + str(ecx) + "] URL ::: " + urls[search].replace("\n", "").replace("\r", "") 
     print "|__ COD ::: " + str(codes[search]) 

print "=============" 
print "[*] Done." 
関連する問題