pythonazure-blob-storagemitmproxy

Want to push the mitmdump to azure blob storage


As a requirement, i want to push the capture mitmdump to azure blob storage. I am using the below code. But getting error as "in script .\log_azureblob.py: ('Cannot connect to proxy.', NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000256D5BF62D0>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient, BlobType
from mitmproxy import http, flowfilter,ctx

blob_service_client = BlobServiceClient.from_connection_string(connection_string)

blob_client = blob_service_client.get_blob_client(container=container_name, blob=blob_name)

if not blob_client.exists():
# if the blob doesn't exist, create it with some initial data
initial_data = b'Initial data for the blob'
blob_client.upload_blob(initial_data)

def start():
# set up the logger
ctx.log.info("BlobLogger started.")

def request(flow):
# get the request data and append it to the blob
request_data = f"{flow.request.method} {flow.request.url} HTTP/1.1\n"
for name, value in flow.request.headers.items():
request_data += f"{name}: {value}\n"
request_data += "\n" # add a blank line between the headers and body
if flow.request.content:
request_data += flow.request.content.decode("utf-8") + "\n"

try:
    blob_client.append_block(request_data.encode("utf-8"))
except Exception as e:
    ctx.log.error(f"Error appending request data to blob: {e}")
def response(flow):
# get the response data and append it to the blob
response_data = f"HTTP/1.1 {flow.response.status_code} {flow.response.reason}\n"
for name, value in flow.response.headers.items():
response_data += f"{name}: {value}\n"
response_data += "\n" # add a blank line between the headers and body
if flow.response.content:
response_data += flow.response.content.decode("utf-8") + "\n"

try:
    blob_client.append_block(response_data.encode("utf-8"))
except Exception as e:
    ctx.log.error(f"Error appending response data to blob: {e}")
def done():
# clean up the logger
ctx.log.info("BlobLogger stopped.")



Solution

  • The error message shows that the connection to the proxy server failed.

    You need to check if the proxy server is running or not and also if the connection string is valid or not.

    Below is the code to upload a file to Azure Blob Storage using the Azure Blob Storage client library.

    conn_string = "connection sting"
    contnr_name = "Container"
    blb_name = "mylog.log"
    blb_service_client = BlobServiceClient.from_connection_string(conn_string)
    blb_client = blb_service_client.get_blob_client(container=contnr_name, blob=blb_name)
     
    if not blb_client.exists():
    initial_data = b'Initial data for the blob'
    blb_client.upload_blob(initial_data)
    def start():
    ctx.log.info("BlobLogger started.")
    
    def request(flow):
    request_data = f"{flow.request.method} {flow.request.url} HTTP/1.1\n"
    for name, value in flow.request.headers.items():
    request_data += f"{name}: {value}\n"
    request_data += "\n"
    if flow.request.content:
    
    request_data += flow.request.content.decode("utf-8") + "\n"
    try:
    blb_client.append_block(request_data.encode("utf-8"))
    except Exception as e:
    ctx.log.error(f"Error appending request data to blob: {e}")
    def response(flow):
    response_data = f"HTTP/1.1 {flow.response.status_code} {flow.response.reason}\n"
    for name, value in flow.response.headers.items():
    response_data += f"{name}: {value}\n"
    response_data +="\n"
    
    if flow.response.content:
    response_data += flow.response.content.decode("utf-8") + "\n"
    try:
    blb_client.append_block(response_data.encode("utf-8"))
    except Exception as e:
    ctx.log.error(f"Error appending response data to blob: {e}")
    def done():
    ctx.log.info("BlobLogger stopped")
    

    Code execution

    enter image description here

    enter image description here

    For further information check this MSDoc.