PYTHON 黑帽子第二章总结
基于python3编写
import sys, socket, getopt, threading, argparse, subprocess # globals options
listen = False
command = False
upload = None
execute = None
target = None
upload_destination = None
port = None def main():
global target
global command
global execute
global listen
global upload_destination
global port # set up argument parsing
parser = argparse.ArgumentParser(description="netcat clone")
parser.add_argument("-p","--port", type=int, help="target port")
parser.add_argument("-t", "--target_host", type=str, help="target host", default="0.0.0.0")
parser.add_argument("-l", "--listen", help="listen on [host]:[port} for incomming connections", action="store_true",default=False) # action 有参数为true,没有参数default false
parser.add_argument("-e", "--execute", help="execute file-to-run execute the given file upn receiving a connection")
parser.add_argument("-c", "--command", help="initialize a command shell", action="store_true", default=False)
parser.add_argument("-u", "--upload",help="--upload=destination upon receing connection upload and write to destination")
args = parser.parse_args() # parse arguments
target = args.target_host
port = args.port
listen = args.listen
execute = args.execute
command = args.command
upload_destination = args.upload # if listen is false and send send data from stdin
if not listen and target is not None and port > 0:
print("DBG:read data from stdin")
# read buffer from stdin , this will block so send CTRL-D if not
# sending to stdin 从stdin发送
buff = sys.stdin.read() print("Sending {0} to client".format(buff))
# send data
client_sender(buff) # we are going to listen and potentially upload things ,excute
# commands and drop a shell back ,depending on the command line options
if listen:
server_loop() def client_sender(buff):
print("DBG:sending data to client on port" + str(port)) # create a sockets
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try:
# connect to target host
client.connect((target, port)) if len(buff):
client.send(buff.encode())
while True:
# now let's wait for data response
recv_len = 1
response = "" while recv_len:
print("DBG:waiting for response from client")
data = client.recv(4096)
recv_len = len(data)
response += data.decode(errors="ignore") if recv_len < 4096:
break
# end="" statement does not end
print(response, end="") # wait for more input
buff = input("")
buff += "\n"
# send it off
client.send(buff.encode())
except:
print("[*] Exception! Exiting.")
finally:
client.close() def server_loop():
global target
print("DBG:entering server loop") server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((target, port)) server.listen(5) while True:
client_socket, addr = server.accept() # spin a thread to handle the new client
client_thread = threading.Thread(target=client_handler, args=(client_socket,))
client_thread.start() def run_command(command):
# trim the newline rstrip trim the end of newline
command = command.rstrip()
print("DGB:executing command:" + command) try:
# this will launch a new process ,note:cd commands are useless
output = subprocess.check_output(command, stderr=subprocess.STDOUT, shell=True)
except:
output = "Failed to execute to command.\r\n" # send the output back to the client
return output # 服务端监听,获取从客户端发来的数据执行命令
def client_handler(client_socket):
global upload
global execute
global command
print("DBG:handling client socket") # check for upload
if upload_destination is not None:
print("DEBG:entering file upload") # read all of the bytes and write them to the destination
file_buff = "" # keep reading data until none is available
while True:
data = client_socket.recv(1024)
if not data:
break
else:
file_buff += data.decode() # write bytes to file
try:
f = open(upload_destination, "wb")
f.write(file_buff)
f.close() # ACK file writing
client_socket.send("Successfully saved file to {0}\r\n".format(upload_destination).encode())
except:
client_socket.send("Failed to save file to {0}\r\n".format(upload_destination).encode()) if execute is not None:
print("DBG: going to execute command") # run the command
output = run_command(execute)
client_socket.send(output.encode()) # go into loop if a command shell was resquested
if command:
print("DBG:shell requested") # show a prompt
client_socket.send("<BHP:#>".encode())
while True: # now recieve until linefeed
cmd_buff = ""
while "\n" not in cmd_buff:
cmd_buff += client_socket.recv(1024).decode() # send back the command output
response = run_command(cmd_buff) # 判断一个response是否为str类型
if isinstance(response, str):
response = response.encode() # send back the response
client_socket.send(response + "<BHP:#>".encode()) if __name__ == '__main__':
main()
使用实列:
服务端执行:
python necat_1.py -l -p -c

客户端执行:
python nccat_1.py -t localhost -p 9999

客户端执行需要EOF读取结束,linux(ctrl-d),windows(ctrl-z)
PYTHON 黑帽子第二章总结的更多相关文章
- python黑帽子(第二章)
TCP客户端 在渗透测试工程中,我们经常会遇到需要创建一个TCP客户端来连接网络.发送垃圾数据.进行模糊测试等任务的情况.但是所处环境不具备丰富的网络工具,下面是一个简单的TCP客户端 import ...
- Python 黑帽子第二章运行截图
- 读书笔记 ~ Python黑帽子 黑客与渗透测试编程之道
Python黑帽子 黑客与渗透测试编程之道 <<< 持续更新中>>> 第一章: 设置python 环境 1.python软件包管理工具安装 root@star ...
- 2017-2018-2 20179204 PYTHON黑帽子 黑客与渗透测试编程之道
python代码见码云:20179204_gege 参考博客Python黑帽子--黑客与渗透测试编程之道.关于<Python黑帽子:黑客与渗透测试编程之道>的学习笔记 第2章 网络基础 t ...
- 《零压力学Python》 之 第二章知识点归纳
第二章(数字)知识点归纳 要生成非常大的数字,最简单的办法是使用幂运算符,它由两个星号( ** )组成. 如: 在Python中,整数是绝对精确的,这意味着不管它多大,加上1后都将得到一个新的值.你将 ...
- python黑帽子(第五章)
对开源CMS进行扫描 import os import queue import requests # 原书编写时间过于久远 现在有requests库对已经对原来的库进行封装 更容易调用 import ...
- python黑帽子(第四章)
Scapy窃取ftp登录账号密码 sniff函数的参数 filter 过滤规则,默认是嗅探所有数据包,具体过滤规则与wireshark相同. iface 参数设置嗅探器索要嗅探的网卡,默认对所有的网卡 ...
- python黑帽子(第三章)
Windows/Linux下包的嗅探 根据os.name判断操作系统 下面是os的源码 posix是Linux nt是Windows 在windows中需要管理员权限.linux中需要root权限 因 ...
- python基础教程-第二章-列表和元组
本章将引入一个新的概念,:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在 一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构 ...
随机推荐
- 挑战程序竞赛 反转开关 poj3276
这个我其实也没有看太懂它的证明过程. 1.若某一个位置被翻转了n次,则其实际上被翻转了n%2次. 2.分析易知翻转的顺序并不影响最终结果. 3.现在我们着眼于第1个位置,可知若要将第1个位置进行翻转只 ...
- python的进栈出栈遍历
python实现出栈进栈 要求: 进栈 出栈 遍历所有 退出 stack = [] #创建列表 #进栈 def pushstack(): stack.append(input('Enter a nub ...
- 跨站点请求伪造(CSRF)总结和防御
什么是CRSF 构建一个地址,比如说是删除某个博客网站博客的链接,然后诱使已经登录过该网站的用户点击恶意链接,可能会导致用户通过自己的手将曾经发布在该网站的博客在不知情的情况下删除了.这种构建恶意链接 ...
- Zookeeper入门一篇就够了
谈点分布式 什么是分布式呢? 起初,我们的应用流量比较小,所有东西全部部署在一个服务器,比如全部丢给一个tomcat来处理,顶多做一个tomcat的多节点部署多分,再挂一台Nginx做一下负载均衡就O ...
- angular foreach的使用
var myAppModule = angular.module('myApp', []); var values = { name : 'misko', gender : 'male', " ...
- Java -> 构造器(构造方法)
构造方法 我们对封装已经有了基本的了解,接下来我们来看一个新的问题,依然以Person为例,由于Person中的属性都被private了,外界无法直接访问属性,必须对外提供相应的set和get方法.当 ...
- day_02~day_09的Python语法基础
Python基础学习 day_02 使用方法修改字符串的大小写 将字符串首字母变成大写 >>> name = "ada lovelace" >>> ...
- 24个经典的MySQL索引问题,你都遇到过哪些?
索引 1.什么是索引? 2.索引有哪些优缺点? 3.索引使用场景(重点) 4.索引有哪几种类型? 5.索引的数据结构(b树,hash) 6.索引的基本原理 7.索引算法有哪些? 8.索引设计的原则? ...
- JSR303后端校验详细笔记
目录 JSR303 使用步骤 关于不为空 分组校验 自定义校验 完整代码 JSR303 使用步骤 1.依赖 <!--数据校验--> <dependency> <group ...
- npm run build 时 报 __webpack_public_path__ = window.webpackPublicPath; 中的windows未定义
原本 webpack.js在webpack.config.babel.js同目录下,在app.jsx中引用,用mac打包没问题,但是window就报window未定义,改到src和app.jsx同目录 ...