在渗透测试过程中,经常需要创建一个TCP客户端,用来测试服务、发送数据、进行 fuzz 等等。如果黑客潜伏在某大型企业的内网环境中,则不太可能直接获取网络工具或编译器,有时甚至连复制/粘贴或者连接外网这种最基本的功能都用不了。在这种情况下,能创建一个 TCP 客户端将会是一项极其有用的能力。
客户端代码:
from doctest import SkipDocTestCase
import socket
from urllib import response
from black import target_version_option_callback
target_host = "127.0.0.1"
target_port = 9998
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((target_host, target_port))
client.send(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
response = client.recv(4096)
print(response.decode())
client.close()
服务端代码:
from http import client
import socket
import threading
IP = '127.0.0.1'
PORT = 9998
def main():
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((IP, PORT))
server.listen(5)
print(f'[*] Listening on {IP}:{PORT}')
while True:
client, address = server.accept()
print(f'[*] Acceped connection from {address[0]}:{address[1]}')
client_handler = threading.Thread(
target=handle_client, args=(client,))
client_handler.start()
def handle_client(client_socket):
with client_socket as sock:
request = sock.recv(1024)
print(f'[*] Received: {request.decode("utf-8")}')
sock.send(b'Hello')
if __name__ == '__main__':
main()
运行结果如下:
本文代码来源为《Python黑帽子》一书,该内容仅供学习研究之用,不得传播,24小时内应予以删除,如若喜欢请购买正版书籍。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)