这里我们讲使用socket模块快速创建TCP,UDP服务器和客户端
所在环境为:kali
编写环境为kali中的:wing personal8
我们在渗透的过程中,在企业内网中几乎没有网络工具或者编译器环境下,我们为了完成一些数据传输,发送垃圾数据,进行模糊测试就需要手动打击爱你一个tcp客户端以达到目标
import socket
target_host = "0.0.0.0"
target_port=999
#建立一个socket连接
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#连接服务端
client.connect((target_host,target_port))
#发送数据
client.send("GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n")
#接受一些数据
response = client.recv(4096)
print (reponses)
import socket
target_host = "www.baidu.com"
target_port = 80
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
client.connect((target_host,target_port))
aaa = "GET / HTTP/1.1\r\nHost:baidu.com\r\n\r\n"
client.send(str.encode(aaa)) #send在python3中 接收的是bytes需要做一个转换
response = client.recv(4096)
print (bytes.decode(response))
import socket
target_host="127.0.0.1"
target_port=80
client=socket.socket(socket.AF_INEF,socket.SOCK_DGRAM)
cliemt.sendto("AAABBBCCC",(target_host,target_port))
data,addr=client.recvfrom(4096)
print (data)
import socket
target_host = '127.0.0.1'
terget_post = 80
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
client.sendto(str.encode('sdsdsdsdsdsd'),(target_host,terget_post)) #同样这里做一个转换
data, addr = client.recvfrom(4096)
print (data)
import socket
import threading
bind_ip= "0.0.0.0"
bind_port=999
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)
print ("[*] Listening on %s:%d" % (bind_ip,bind_port))
#这里是客户处理线程
def handle_client(client_socket):
#打印客户端发送的得到的内容
request=client_socket.recv(1024)
print ("[*] Received: %s" % request)
#返还一个数据包
client_socket.send("ACK!")
client_socket.close()
while True:
client,addr = server.accept()
print ("[*] Accept connection from: %s:%d" % (addr[0],addr[1]))
#挂起客户端线程处理传入的数据
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
import socket
import threading
bind_ip = '0.0.0.0'
bind_port = 999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5) #最大连接数
print ("[*] listen on %s:%d" % (bind_ip,bind_port))
def handle_client(client_socket):
request = client_socket.recv(1024) #接受类型并打印
client_socket.send(str.encode('ACK!'))#这里也要进行转码
client_socket.close()
while True:
client,addr = server.accept()
print ("[*] Accepted connection from: %s:%d" % (addr[0],addr[1]))
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
进入文件存储目录下
python xxx.py
进入文件存储目录
python xxx.py