2017-04-09 77 views
1

嗯,首先很抱歉,因爲我不知道如何提出我的問題,上次在安全挑戰中,我試圖用curl發送一些請求,在幾分鐘之後他們進行了很多測試以找出挑戰是真正的工作,我試着寫一些Python代碼自動生成了我的請求,並贏得了一些時間想法或模塊te在python中生成結構化文本?

這裏有一些要求的,我曾經嘗試: 基本一個

curl http://10.20.0.50:80/ 

然後我有指定路徑示例:

curl http://10.20.0.50:80/transfert/solde 
curl http://10.20.0.50:80/account/creat 
... 

一段時間增加授權或餅乾...

curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" 

或添加一些參數:

curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" --data-raw '{"id":"521776"}' -v 

所以事情是一定要考很多事情有和沒有用,擅自cookie和更改cookie一些時間,並添加 - 數據原始...我試圖寫一個腳本來做到這一點,但它的醜陋:

url = "http://10.20.0.50:80/" 
auth = ' -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="' 
def generate(path,c=None,h=True,plus = None): 
    #c cookie , h if we put authentification 
    #plus add more code at the end of the request 
    global auth # authentification 
    global url 
    if c: 
    cook = ' -H "cookie: PHPSESSID={};"'.format(c) 
    req = "curl "+url+path 
    if h:#h bool 
     req += auth 
    if c : 
     req += cook 
    if plus : 
     req += plus 
    req+=" -v " 
    return req 

爲了便於閱讀,我刪除了一個參數--data-row,這個想法是我想知道是否有更好的方法來做到這一點!而不僅僅是這個例子,但一般來說,如果我想創建python代碼,生成一個類的代碼源,我必須指定類的名稱屬性和類型和代碼生成模板...

我希望你能幫助我:d PS:對不起,我的英語,如果我瘋了一些錯誤

+0

首先,你能修復你的python代碼縮進嗎? –

+0

我沒有注意到我不會過去它 –

回答

1

也許,一個方法來「提高」你的代碼做這樣的事情:

def generate(command = "", headers = [], raws = [], other = [], v = True): 
    if headers: 
     command += "".join(" -H " + k for k in h) 
    if raws: 
     command += "".join(" --data-raw " + k for k in raw) 
    if v: 
     command += " -v" 
    if other: 
     command += "".join(" " + k for k in other) 
    return command 

h = ['"Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA=="', '"cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;"'] 
raw = ["'{\"id\":\"521776\"}'"] 
cmd = "curl http://10.20.0.50:80/transfert/solde" 

command1 = generate(command=cmd,headers=h,raws= raw) 
command2 = generate(command=cmd,headers=h,raws=raw, v=False) 
command3 = generate(command=cmd,v = False) 

print("command1:",command1) 
print("command2:", command2) 
print("command3:", command3) 

輸出:

command1: curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" --data-raw '{"id":"521776"}' -v 
command2: curl http://10.20.0.50:80/transfert/solde -H "Authorization:Basic bXlhcGk6U3VwZXJTZWNyZXRQYXMkdzByZA==" -H "cookie: PHPSESSID=23c3jc3spuh27cru38kf9l2au5;" --data-raw '{"id":"521776"}' 
command3: curl http://10.20.0.50:80/transfert/solde 
+1

Thanx它看起來更好:D –

+1

我可以做什麼?你錯過了一封信? xD 如果您的意思是評價您的答案,請聯繫alredy 「感謝您的反饋!記錄下名聲低於15的人的投票記錄,但不會更改公開顯示的帖子分數。」 但它是隱形的:p 非常感謝我給了我更多想法來組織我的代碼 –