簡體   English   中英

Python STDOUT 與 openssl 子流程一起歸檔

[英]Python STDOUT to file with openssl subprocess

I am trying to write a python script to automate the process of checking for SSL renegotiation through openSSL and output the results to a file. 我遇到了2個問題。

我的第一個問題是初始握手中的 output 被寫入文件,但實際的重新協商部分沒有。 而是顯示在控制台上。

subprocess.call("echo \"R\" | openssl s_client -connect example.com:443", 
        shell=True, stdout=FILE)

我的另一個問題(盡管這可能是錯誤的地方)是我無法讓 openSSL 命令用於發送 GET 命令。

subprocess.call("echo -e \"GET / HTTP/1.1\r\n\r\n\" | openssl s_client -connect
        example.com:443", shell=True)   

同樣,初始連接已建立,但隨后 openSSL 存在,它不處理 GET 請求。

任何幫助將不勝感激。 謝謝。

沒有理由使用shell=True作為輸入。 相反,使用stdin=subprocess.PIPE 另外,請注意您的請求無效,因為 HTTP 1.1 需要Host header。 此外,我想不出使用命令行 openssl 代替ssl 模塊的理由。

話雖如此,這是一個工作示例:

import subprocess

f = open('http_answer', 'w')
_,log = subprocess.Popen(
    ['openssl', 's_client', '-quiet', '-connect', 'twitter.com:443'],
    stdout=f, stderr=subprocess.PIPE, stdin=subprocess.PIPE
).communicate('GET / HTTP/1.0\r\n\r\n')
print('Output of SSL:\n' + log)

@phihag 我對你的腳本做了一些細微的改動,對我來說效果很好。

import subprocess

f = open('http_answer', 'w')
_,log = subprocess.Popen(
    ['openssl', 's_client', '-quiet', '-connect', 'twitter.com:443','-sess_out', 'session.txt'],
    stdout=f, stderr=subprocess.PIPE, stdin=subprocess.PIPE ).communicate('GET / HTTP/1.0\r\nHOST: twitter.com\r\n\r\n') print('Output of SSL:\n' + log)

列出更改 1. 添加 'sess_out' 'session.txt' 參數,保留所有 SSL Session 參數,可以使用以下 Z50955D4B2031271F8FDA1764C1A66 命令查看

$openssl sess_id -in test.txt -text -cert -noout
  1. 將主機信息添加到 GET 命令,因為較新的部分僅使用 HOST 選項響應良好。

    'GET / HTTP/1.0\r\n主機:twitter.com\r\n\r\n'

@Drew,為時已晚,但我希望這有點幫助。 謝謝你。

請記住,openssl s_client 還對某些 output 使用 stderr。 您需要檢查重新談判是否進入 stderr,我相信它確實如此,盡管我的 memory 可能正在消失。

我以不同的方式完成了這項工作,但不是在 python 中。 我創建了一個進程並將標准輸入、標准輸出、標准錯誤文件描述符連接到我可以讀/寫的文件描述符上,我實際上驅動輸入並讀取 output。 這需要更多的工作,但您可以完全控制正在發生的事情並與流程進行交互。 我已經在 php 中完成了這項工作,該測試可在http://netsekure.org/2009/11/tls-renegotiation-test/在線獲得。

或者,您可以嘗試使用 python 對 openssl 本身進行編程,而不是使用 s_client,但這是更多的工作,我使用了以前的方法。

您可以檢查兩件事情,但您沒有明確說明您對哪一件感興趣:

  • 檢查遠程服務器是否支持客戶端發起的重新協商
  • 檢查遠程服務器是否支持安全重新協商擴展

只需對適用於這兩種情況的關鍵字執行 s_client 和 grep 即可簡單地推斷出這兩種情況。 這完全取決於您需要多少控制/復雜性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM