GO语言开发GUI安全工具实践(二)

简介: GO语言开发GUI安全工具实践

对于GUI工具的理解:


首先要有一个模板

对比命令行下的工具,图形化的工具要有事件响应(比如邮件列出菜单)


import tkinter as tk
from tkinter.filedialog import *
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class App(Frame):
def __init__(self,master=None):
super(App, self).__init__()
super().__init__(master)
self.master=master
self.var = tk.StringVar()
self.zapi = "oI5pPuvuGPF5CSSRzCzk4WB8rvIYUkmM"
self.initUI()
self.textpad=None
self.Create()
self.pack()
def Create(self):
menubar=Menu(root)
menuFile=Menu(menubar)
menubar.add_cascade(label="options",menu=menuFile)
menuFile.add_command(label="shellcode",accelerator="ctrl+a",command=self.test)
menubar.add_cascade(label="test",menu=menuFile)
menuFile.add_command()
menuFile.add_command(label="open",accelerator="ctrl+b",command=self.fileopen)
menuFile.add_command(label="save",accelerator="ctrl+c",command=self.filesave)
menuFile.add_command(label="new",accelerator="ctrl+d",command=self.filenew)
root["menu"]=menubar
self.textpad=Text(root,width=200,height=10)
self.textpad.pack()
self.contextMenu=Menu(root)
//右键事件
self.contextMenu.add_command(label="一键装逼",command=self.zha)
self.contextMenu.add_command(label="bg",command=self.openAskColor)
root.bind("<Button-3>",self.createCon)
def zha(self):
pass
def openAskColor(self):
pass
def fileopen(self):
pass
def filesave(self):
pass
def filenew(self):
pass
def createCon(self,event):
self.contextMenu.post(event.x_root,event.y_root)
def test(self):
pass
def initUI(self):
group_top = tk.LabelFrame(self, padx=15, pady=10)
group_top.pack(padx=10, pady=5)
tk.Button(group_top, text="scanIP", width=10, command=self.scanIP).grid(row=0, column=1)
self.path_entry = tk.Entry(group_top, width=30)
self.path_entry.grid(row=0, column=0, pady=5, padx=5)
//添加按钮
tk.Button(group_top, text="开始执行", width=10, command=self.func).grid(row=1, column=1, sticky=tk.E)
tk.Button(group_top, text="停止", width=10, command=self.destroy).grid(row=1, column=0, sticky=tk.W)
console_frame = tk.Frame(group_top).grid(row=2, column=0, columnspan=2)
self.console_text = tk.Text(
console_frame, fg="green", bg="black", width=40, height=20, state=tk.DISABLED)
scrollbar = tk.Scrollbar(console_frame, command=self.console_text.yview)
self.console_text.pack(expand=1, fill=tk.BOTH)
self.console_text['yscrollcommand'] = scrollbar.set
def output_to_console(self, new_text):
self.console_text.config(state=tk.NORMAL)
self.console_text.insert(tk.END, new_text)
self.console_text.see(tk.END)
self.console_text.config(state=tk.DISABLED)
def func(self):
pass
def usage(self):
A = """
__   .__    .___  .___      .__
____________ |  | _|__| __| _/__| _/____  |__|
/  ___/\____ \|  |/ /  |/ __ |/ __ |\__  \ |  |
\___ \ |  |_> >    <|  / /_/ / /_/ | / __ \|  |
/____  >|   __/|__|_ \__\____ \____ |(____  /__|
\/ |__|        \/       \/    \/     \/
"""
B = """
***          by YanMu                            ***
"""
self.output_to_console(f"{A}\n");self.output_to_console(f"{B}\n")
def scanIP(self):
pass
if __name__ == '__main__':
root=Tk()
root.title("Pythonstudy by 雷石安全")
root.geometry("1200x500+200+300")
app = App(master=root)
root.resizable(height=False)
app.usage()
app.mainloop()

代码中pass地方可以写你相应的功能点


demo演示:


449500f1d0c7461be22b6ef94958f6ba_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.png


然后我们可以在正常的函数除填写我们相应的功能


import tkinter as tk
import shodan
from tkinter.colorchooser import *
from tkinter.filedialog import *
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class App(Frame):
def __init__(self,master=None):
super(App, self).__init__()
super().__init__(master)
self.master=master
self.var = tk.StringVar()
self.zapi = ""//你的api
self.initUI()
self.textpad=None
self.Create()
self.pack()
def Create(self):
menubar=Menu(root)
menuFile=Menu(menubar)
menubar.add_cascade(label="options",menu=menuFile)
menuFile.add_command(label="shellcode",accelerator="ctrl+a",command=self.test)
menubar.add_cascade(label="test",menu=menuFile)
menuFile.add_command()
menuFile.add_command(label="open",accelerator="ctrl+b",command=self.fileopen)
menuFile.add_command(label="save",accelerator="ctrl+c",command=self.filesave)
menuFile.add_command(label="new",accelerator="ctrl+d",command=self.filenew)
root["menu"]=menubar
self.textpad=Text(root,width=200,height=10)
self.textpad.pack()
self.contextMenu=Menu(root)
self.contextMenu.add_command(label="一键装逼",command=self.zha)
self.contextMenu.add_command(label="bg",command=self.openAskColor)
root.bind("<Button-3>",self.createCon)
def zha(self):
pass
def openAskColor(self):
s=askcolor(color="red",title="bg")
self.textpad.config(bg=s[1])
def fileopen(self):
self.textpad.delete("1.0","end")
with askopenfile(title="txt") as f:
self.textpad.insert(INSERT,f.read())
self.filename=f.name
def filesave(self):
with open(self.filename,"w") as f:
c=self.textpad.get(1.0,END)
f.write(c)
def filenew(self):
self.filename=asksaveasfile("另存为",initialfile="log.txt",filetypes=[("文本文档","*.txt")],defaultextension=".txt")
self.filesave()
def createCon(self,event):
self.contextMenu.post(event.x_root,event.y_root)
def test(self):
pass
def initUI(self):
group_top = tk.LabelFrame(self, padx=15, pady=10)
group_top.pack(padx=10, pady=5)
tk.Button(group_top, text="scanIP", width=10, command=self.scanIP).grid(row=0, column=1)
self.path_entry = tk.Entry(group_top, width=30)
self.path_entry.grid(row=0, column=0, pady=5, padx=5)
tk.Button(group_top, text="开始执行", width=10, command=self.func).grid(row=1, column=1, sticky=tk.E)
tk.Button(group_top, text="停止", width=10, command=self.destroy).grid(row=1, column=0, sticky=tk.W)
console_frame = tk.Frame(group_top).grid(row=2, column=0, columnspan=2)
self.console_text = tk.Text(
console_frame, fg="green", bg="black", width=40, height=20, state=tk.DISABLED)
scrollbar = tk.Scrollbar(console_frame, command=self.console_text.yview)
self.console_text.pack(expand=1, fill=tk.BOTH)
self.console_text['yscrollcommand'] = scrollbar.set
def output_to_console(self, new_text):
self.console_text.config(state=tk.NORMAL)
self.console_text.insert(tk.END, new_text)
self.console_text.see(tk.END)
self.console_text.config(state=tk.DISABLED)
def func(self):
se = ""//要搜索的设备
api = shodan.Shodan(self.zapi)
res = api.search(se)
# a=str(result['ip_str'])
for result in res['matches']:
url = str(result['ip_str']) + ":" + str(result['port']) + ":" + str(
result['location']['country_name']) + ":" + str(result['domains'])
# print(url)
self.output_to_console(f"{url}\n")
def usage(self):
A = """
__   .__    .___  .___      .__
____________ |  | _|__| __| _/__| _/____  |__|
/  ___/\____ \|  |/ /  |/ __ |/ __ |\__  \ |  |
\___ \ |  |_> >    <|  / /_/ / /_/ | / __ \|  |
/____  >|   __/|__|_ \__\____ \____ |(____  /__|
\/ |__|        \/       \/    \/     \/
"""
B = """
***          by YanMu                            ***
"""
self.output_to_console(f"{A}\n");self.output_to_console(f"{B}\n")
def scanIP(self):
pa = self.path_entry.get()
scapi = shodan.Shodan(self.zapi)
hosts = scapi.host(pa)
er = str(hosts['ip_str']) + ":" + str(hosts.get('org', 'n/a')) + ":" + str(hosts.get('os', 'n/a'))
self.output_to_console(f"{er}\n")
for host in hosts['data']:
sas = str(host['port']) + ":" + str(host['data'])
self.output_to_console(f"{sas}\n")
if __name__ == '__main__':
root=Tk()
root.title("Pythonstudy by 雷石安全")
root.geometry("1200x500+200+300")
app = App(master=root)
root.resizable(height=False)
app.usage()
app.mainloop()

162063bc781c0e8a14629e4745e14638_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.png


这里我们填写shodan的搜索为例,当然当时练习的时候尝试写一个记事本的功能来着


GUI个人思路:


有一个样式框架,往里面填东西就行。

你可以把别人命令行式的代码,剪吧剪吧放到自己的工具里面(缺陷:这个demo不能及时反映响应信息)


最后介绍一下,B/S架构思考:


这里我用的tornado框架

还是先有一个模板

import tornado.web
import os
import shodan
import tornado.ioloop
import tornado.httpserver
from tornado.options import define,options,parse_command_line
define("port",default=8888,help="运行端口",type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
def write_error(self, status_code, **kwargs):
self.write("your caused a %d error."%status_code)
class CoupletHandler(tornado.web.RequestHandler):
def post(self):
first=self.get_argument("first")
second=self.get_argument("second")
self.render("couplet.html",first=sumurls,second=second)
if __name__ == '__main__':
parse_command_line()
print("""
logo:leishi
by caibi-yanmu
""")
print("http://localhost:{}/".format(options.port))
base_dir=os.path.dirname(__file__)
app=tornado.web.Application(
handlers=[
(r"/",IndexHandler),
(r"/couplet",CoupletHandler)
],
template_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),'templates'),
static_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),'static'),
)
http_server=tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

d38eb3aad9997aeb68abf3b7e1bc8471_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.png


然后我们向这个demo里面加我们的功能点,这里主要方便测试(外表不重要,嘤嘤嘤)


5dcb6050cab2ef4a6ff674374ce2d6bb_640_wx_fmt=jpeg&wxfrom=5&wx_lazy=1&wx_co=1.jpg



import tornado.web
import os
import shodan
import tornado.ioloop
import tornado.httpserver
from tornado.options import define,options,parse_command_line
define("port",default=8888,help="运行端口",type=int)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
def write_error(self, status_code, **kwargs):
self.write("your caused a %d error."%status_code)
class CoupletHandler(tornado.web.RequestHandler):
def post(self):
first=self.get_argument("first")
second=self.get_argument("second")
api=shodan.Shodan(first)
sumurls=""
try:
results=api.search(second)
for i in results['matches']:
url=i['ip_str']+":"+str(i['port'])
sumurls+=url+"\n"
except shodan.APIError as e:
print(e)
self.render("couplet.html",first=sumurls,second=second)
if __name__ == '__main__':
parse_command_line()
print("""
logo:leishi
by caibi-yanmu
""")
print("http://localhost:{}/".format(options.port))
base_dir=os.path.dirname(__file__)
app=tornado.web.Application(
handlers=[
(r"/",IndexHandler),
(r"/couplet",CoupletHandler)
],
template_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),'templates'),
static_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),'static'),
)
http_server=tornado.httpserver.HTTPServer(app)
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()

这里师傅们可以根据自己的前端代码去填充对应结果


d62bd901a9f33ae96c4dc52da41adfef_640_wx_fmt=png&wxfrom=5&wx_lazy=1&wx_co=1.png


总结:


我们平常写的命令行下的工具无论是在GUI形式下还是B/S形式下,最后核心的功能点不变,所以说,要是我们平常在测试的过程中,完全可以只写在命令行模式下的小工具。


相关文章
|
5月前
|
消息中间件 缓存 NoSQL
Redis各类数据结构详细介绍及其在Go语言Gin框架下实践应用
这只是利用Go语言和Gin框架与Redis交互最基础部分展示;根据具体业务需求可能需要更复杂查询、事务处理或订阅发布功能实现更多高级特性应用场景。
364 86
|
4月前
|
存储 安全 Java
【Golang】(4)Go里面的指针如何?函数与方法怎么不一样?带你了解Go不同于其他高级语言的语法
结构体可以存储一组不同类型的数据,是一种符合类型。Go抛弃了类与继承,同时也抛弃了构造方法,刻意弱化了面向对象的功能,Go并非是一个传统OOP的语言,但是Go依旧有着OOP的影子,通过结构体和方法也可以模拟出一个类。
288 1
|
6月前
|
Cloud Native Go API
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
480 0
|
6月前
|
Cloud Native Java Go
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
320 0
|
6月前
|
Cloud Native Java 中间件
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
352 0
|
12月前
|
编译器 Go
揭秘 Go 语言中空结构体的强大用法
Go 语言中的空结构体 `struct{}` 不包含任何字段,不占用内存空间。它在实际编程中有多种典型用法:1) 结合 map 实现集合(set)类型;2) 与 channel 搭配用于信号通知;3) 申请超大容量的 Slice 和 Array 以节省内存;4) 作为接口实现时明确表示不关注值。此外,需要注意的是,空结构体作为字段时可能会因内存对齐原因占用额外空间。建议将空结构体放在外层结构体的第一个字段以优化内存使用。
|
12月前
|
运维 监控 算法
监控局域网其他电脑:Go 语言迪杰斯特拉算法的高效应用
在信息化时代,监控局域网成为网络管理与安全防护的关键需求。本文探讨了迪杰斯特拉(Dijkstra)算法在监控局域网中的应用,通过计算最短路径优化数据传输和故障检测。文中提供了使用Go语言实现的代码例程,展示了如何高效地进行网络监控,确保局域网的稳定运行和数据安全。迪杰斯特拉算法能减少传输延迟和带宽消耗,及时发现并处理网络故障,适用于复杂网络环境下的管理和维护。
|
6月前
|
Cloud Native 安全 Java
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
403 1
|
6月前
|
Cloud Native Java Go
Go:为云原生而生的高效语言
Go:为云原生而生的高效语言
386 0
|
8月前
|
开发框架 JSON 中间件
Go语言Web开发框架实践:路由、中间件、参数校验
Gin框架以其极简风格、强大路由管理、灵活中间件机制及参数绑定校验系统著称。本文详解其核心功能:1) 路由管理,支持分组与路径参数;2) 中间件机制,实现全局与局部控制;3) 参数绑定,涵盖多种来源;4) 结构体绑定与字段校验,确保数据合法性;5) 自定义校验器扩展功能;6) 统一错误处理提升用户体验。Gin以清晰模块化、流程可控及自动化校验等优势,成为开发者的优选工具。