基于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 黑帽子第二章总结的更多相关文章

  1. python黑帽子(第二章)

    TCP客户端 在渗透测试工程中,我们经常会遇到需要创建一个TCP客户端来连接网络.发送垃圾数据.进行模糊测试等任务的情况.但是所处环境不具备丰富的网络工具,下面是一个简单的TCP客户端 import ...

  2. Python 黑帽子第二章运行截图

  3. 读书笔记 ~ Python黑帽子 黑客与渗透测试编程之道

    Python黑帽子  黑客与渗透测试编程之道   <<< 持续更新中>>> 第一章: 设置python 环境 1.python软件包管理工具安装 root@star ...

  4. 2017-2018-2 20179204 PYTHON黑帽子 黑客与渗透测试编程之道

    python代码见码云:20179204_gege 参考博客Python黑帽子--黑客与渗透测试编程之道.关于<Python黑帽子:黑客与渗透测试编程之道>的学习笔记 第2章 网络基础 t ...

  5. 《零压力学Python》 之 第二章知识点归纳

    第二章(数字)知识点归纳 要生成非常大的数字,最简单的办法是使用幂运算符,它由两个星号( ** )组成. 如: 在Python中,整数是绝对精确的,这意味着不管它多大,加上1后都将得到一个新的值.你将 ...

  6. python黑帽子(第五章)

    对开源CMS进行扫描 import os import queue import requests # 原书编写时间过于久远 现在有requests库对已经对原来的库进行封装 更容易调用 import ...

  7. python黑帽子(第四章)

    Scapy窃取ftp登录账号密码 sniff函数的参数 filter 过滤规则,默认是嗅探所有数据包,具体过滤规则与wireshark相同. iface 参数设置嗅探器索要嗅探的网卡,默认对所有的网卡 ...

  8. python黑帽子(第三章)

    Windows/Linux下包的嗅探 根据os.name判断操作系统 下面是os的源码 posix是Linux nt是Windows 在windows中需要管理员权限.linux中需要root权限 因 ...

  9. python基础教程-第二章-列表和元组

    本章将引入一个新的概念,:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在 一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构 ...

随机推荐

  1. B. Preparing for Merge Sort

    \(考虑的时候,千万不能按照题目意思一组一组去模拟\) \(要发现每组的最后一个数一定大于下一组的最后一个数\) \(那我们可以把a中的数一个一个填充到vec中\) #include <bits ...

  2. P2320鬼谷子的钱袋(分治)

    ------------恢复内容开始------------ 描述:https://www.luogu.com.cn/problem/P2320 m个金币,装进一些钱袋.钱袋中大于1的钱互不相同. 问 ...

  3. H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分

    H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...

  4. Code::Blocks无法调试 Starting the debuggee failed: No executable specified, use `target exec'

    1.必须建立工程 2.工程名不可有特殊字符或空格,可以有字母.数字.下划线 2.编译器设置里勾选-g(产生调试符号) 3.重新编译项目(如果之前编译过了) 4.调试器设置 > Default & ...

  5. 物流配送管理系统(ssm,mysql)

    项目演示视频观看地址:https://www.toutiao.com/i6811872614676431371/ 下载地址: 51document.cn 可以实现数据的图形展示.报表展示.报表的导出. ...

  6. 流媒体与实时计算,Netflix公司Druid应用实践

    Netflix(Nasdaq NFLX),也就是网飞公司,成立于1997年,是一家在线影片[租赁]提供商,主要提供Netflix超大数量的[DVD]并免费递送,总部位于美国加利福尼亚州洛斯盖图.199 ...

  7. shell 条件结构之 if 语句使用总结

    文章目录 #条件判断的格式 [ exp ] [[ exp ]] test exp 注意: exp 与 "["."]"括号之间必须要有空格,否则会报语法错误: [ ...

  8. SwiftUI - 一起来仿写微信APP之一首页列表视图

    简介 最近在学习 SwiftUI ,我一般都是先去学习界面布局,所以就想着仿写一下经常使用的软件的界面,所以先拿微信开刀.因为不想一次性发太多的内容,所以只好将主题分解,一部分一部分地去讲,接下来我们 ...

  9. js 调用webservice及nigix解决跨域问题

    前言 我们写一些简单的爬虫的时候会遇到跨域问题,难道我们一定要用后台代理去解决吗? 答案是否定的.python之所以适应爬虫,是因为库真的很好用. 好吧python不是今天的主角,今天的主角是js. ...

  10. 一文搞懂HMM(隐马尔可夫模型)-转载

    写在文前:原博文地址:https://www.cnblogs.com/skyme/p/4651331.html 什么是熵(Entropy) 简单来说,熵是表示物质系统状态的一种度量,用它老表征系统的无 ...