一.Linux虚拟机中自带的nc与win主机实现监听并返回的简单实例:
监听端listing.py 监听
import socket
import threading
bind_ip = "10.21.21.120"
bind_port = 9999
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)
# this is our client handling thread
def handle_client(client_socket):
# just print out what the client sends
request = client_socket.recv(1024)
print "[*] Received: %s" % request
# send back a packet
client_socket.send("ACK!")
print client_socket.getpeername()
client_socket.close()
while True:
client,addr = server.accept()
print "[*] Accepted connection from: %s:%d" % (addr[0],addr[1])
# spin up our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start()
(1)主机(服务器端listing.py)开始监听,返回提示正在主机IP上实行监听
(2)链接虚拟机nc
(3)主机接收到nc信息,提示接收的来自哪个IP Port
(4)nc输入字符串
(5)主机接收到输入的字符串,打印输出,并显示,返回一个应答给虚拟机
(6)nc接收到主机返回的应答,打印输出。
注意:这里是nc客户端与主机服务端监听同时使用,如果只有nc,这会提示无法连接。
二.python 取代netcat
同时实现客户端,服务器端
#!/usr/bin/python
import sys
import socket
import getopt
import threading
import subprocess
# define some global variables
listen = False
command = False
upload = False
execute = ""
target = ""
upload_destination = ""
port = 0
# this runs a command and returns the output