栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

【Python黑帽子】proxy.py脚本

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Python黑帽子】proxy.py脚本

#coding=utf-8
import sys
import socket
import threading

#ACSII
HEX_FILTER=''.join([(len(repr(chr(i)))==3) and chr(i) or '.' for i in range(256)])

def hexdump(src,length=16,show=True):
    if isinstance(src,bytes):
        src=src.decode()
    result=list()
    for i in range(0,len(src),length):
        word=str(src[i:i+length])

        printable=word.translate(HEX_FILTER)
        
        hexa=' '.join([f'{ord(c):02X}' for c in word])
        hexwidth=length*3
        result.append(f'{i:04x} {hexa:<{hexwidth}}  {printable}')
    if show:
        for line in result:
            print(line)
    else:
        return result

def revicve_from(connection):
    buffer=b''
    connection.settimeout(5)
    try:
        while True:
            data=connection.recv(4096)
            if not data:
                break
            buffer+=data
    except Exception as e:
        pass
    return buffer

def request_handler(buffer):
    #perform packet modication
    return bytes(buffer)

def respone_handler(buffer):
    #perform packet modication
    return bytes(buffer)

def proxy_handler(client_socket,remote_host,remote_port,recive_first):
    remote_socket=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    remote_socket.connect((remote_host,remote_port))

    if recive_first:
        remote_buffer=revicve_from(remote_socket)
        hexdump(remote_buffer)
    remote_buffer=respone_handler(remote_buffer)
    if len(remote_buffer):
        print("[<==] Send %d bytes to localhost." %len(remote_buffer))
        client_socket.send(remote_buffer)
    while True:
        local_buffer=revicve_from(client_socket)
        if len(local_buffer):
            line="[==>]Recive %d bytes from localhost." %len(local_buffer)
            print(line)
            hexdump(local_buffer)

            local_buffer=request_handler(local_buffer)
            remote_socket.send(local_buffer)
            print("[==>]Send to remote.")

        remote_buffer=revicve_from(remote_socket)
        if len(remote_buffer):
            print("[==>]Recive %d bytes from remote ." %len(remote_buffer))
            hexdump(remote_buffer)

            remote_buffer=respone_handler(remote_buffer)
            client_socket.send(remote_buffer)
        
        if not len(local_buffer) or not len(remote_buffer):
            client_socket.close()
            remote_socket.close()
            print("[*] no more data. Closing connection.")
            break

def server_loop(local_host,local_port,remote_host,remote_port,recive_first):
    server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        server.bind((local_host,local_port))
    except Exception as e:
        print("problem  on bind: %e" % e)

        print("[!!]Failed to listen on %s:%d" %(local_host,local_port))
        print("[!!]Check for other listening sockets or correct permissions.")
        sys.exit(0)
    print("[*] Listening on %s:%d" %(local_host,local_port))
    server.listen(5)

    while True:
        client_sockt,addr=server.accept()
        #print out the local connection information
        line=">Recived incoming connection from %s:%d" %(addr[0],addr[1])
        print(line)

        proxy_thread=threading.Thread(target=proxy_handler,args=(client_sockt,remote_host,remote_port,recive_first))

        proxy_thread.start()

def main():
    if len(sys.argv[1:])!=5:
        print("usage: ./proxy.py [localhost] [localport]",end=' ')
        print("[remotehost] [remoteport] [recive_first]")
        print("Example:./proxy.py 127.0.0.1 8888 10.0.2.15 5555 True")
        sys.exit(0)
    local_host=sys.argv[1]
    local_port=int(sys.argv[2])
    remote_host=sys.argv[3]
    remote_port=int(sys.argv[4])
    recive_first=sys.argv[5]

    if "True" in recive_first:
        recive_first=True
    else:
        recive_first=False
    server_loop(local_host,local_port,remote_host,remote_port,recive_first)


if __name__=="__main__":
    main()

以百度为例:python 02/proxy.py 127.0.0.1 8086 www.baidu.com 80 True
在浏览器输入:https://127.0.0.1:8086

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/932345.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号