吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1136|回复: 20
收起左侧

[Python 原创] 窗口呈现防火墙封禁威胁IP命令并查询IP归属地

[复制链接]
67haha 发表于 2024-1-23 17:35
本帖最后由 67haha 于 2024-1-30 17:47 编辑

python不精通的网工路过,之前因为攻防频繁需要对一些IP进行封禁,但是每次封禁之前都去查询对应IP归属地判断是否为业务地址以及对威胁IP需要登录防火墙一条一条敲命令去进行封禁,所以自己网上了下python的一些例子然后自己摸索写出来一个脚本出来,第一次从水区跑出来玩,写的拉的地方大佬轻喷,鄙人还在学习中~~

操作&&实现:值班人员会将探针识别的威胁IP发出,我们复制对应信息粘贴在第一个文本框后按下吾爱按钮,窗口会呈现对应IP归属地及三家防火墙Huawei、Hillstone、H3C的命令,之后将对应命令通过SSH方式登录去粘贴(当时想过使用netmiko或者paramiko,想了一会还是感觉手动粘贴更放心)
注:这里Hillstone用的blacklist是我们自己做的地址簿然后安全策略做的deny,如果您自己的地址簿是其他名称请对应修改hillstone.insert(tk.INSERT, "config\naddress blacklist\n")的blacklist
gouzi2.gif

微信截图_20240123172326.png

模块

    import tkinter as tk
    import re
    from PIL import Image, ImageTk
    import requests
    from bs4 import BeautifulSoup
    import base64
    import io# 窗口
    root = tk.Tk()
    root.title("封禁IP及其归属地查询")
    def center_window(window):
            screen_width = window.winfo_screenwidth()
            screen_height = window.winfo_screenheight()
            window.update_idletasks()

            window_width = window.winfo_reqwidth()
            window_height = window.winfo_reqheight()
            x = (screen_width - window_width) // 2
            y = (screen_height - window_height) // 2
            window.geometry(f"{window_width}x{window_height}+{x}+{y}")

图片区

    base64_strings = {
    #logo的base编码,详细代码在txt文件内
            "huawei_logo": "<iVBOR.....>",
            "hillstone": "<iVBOR.....>",
            "h3c_logo": "<iVBOR.....>",
            "52pojie": "<iVBOR.....>"
    }
    def create_image_label(root, photo_base64, row, column, sticky):
            base64_strings = base64.b64decode(photo_base64)
            image= Image.open(io.BytesIO(base64_strings))
            photo = ImageTk.PhotoImage(image)
            label = tk.Label(root, image=photo)
            label.grid(row=row, column=column, sticky=sticky)
            label.photo = photo

按钮触发IP操作

    def gettext():
            content = huawei.get("1.0", "100.end")
            ip = re.findall(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}', content)
    # 清空所有窗口内容
            huawei.delete('1.0', "100.end")
            hillstone.delete('1.0', "100.end")
            h3c.delete('1.0', "100.end")
            belong.delete('1.0', "100.end")
    # 输入窗口内容
            hillstone.insert(tk.INSERT, "config\naddress blacklist\n")
            huawei.insert(tk.INSERT, "sys" + "\n")
            h3c.insert(tk.INSERT, "sys" + "\n")
    # 窗口文本框获取IP
            def get_ip():
                    url = f"http://www.jsons.cn/ipbatch/"
                    data = {'txt_ip': ip}
                    response = requests.post(url, data=data)
                    if response.status_code == 200:
                            soup = BeautifulSoup(response.text, "html.parser")
                            table = soup.find("table", {"class": "table-bordered"})
                            rows = table.find_all("tr")

                            for j in range(1, len(rows)):
                                    srcip = rows[j].find_all("td")[0].text.strip()
                                    country = rows[j].find_all("td")[1].text.strip()
                                    province = rows[j].find_all("td")[2].text.strip()
                                    city = rows[j].find_all("td")[3].text.strip()
                                    display = f"{srcip} {country} {province} {city}"
                                    belong.insert(tk.INSERT, display + "\n")
                    else:
                            belong.insert(tk.INSERT, "查询失败")

            for i in ip:
                    huawei.insert(tk.INSERT, f"firewall blacklist item source-ip {i}\n")
                    hillstone.insert(tk.INSERT, f"ip {i}/32\n")
                    h3c.insert(tk.INSERT, f"blacklist ip {i}\n")
            get_ip()

文本框区域

    huawei = tk.Text(root, height=20, width=50)
    huawei.grid(row=1, column=0)
    hillstone = tk.Text(root, height=20, width=50)
    hillstone.grid(row=1, column=1)
    h3c = tk.Text(root, height=20, width=50)
    h3c.grid(row=3, column=0)
    belong = tk.Text(root, height=20, width=50)
    belong.grid(row=3, column=1)

显示窗口

    create_image_label(root, "huawei_logo.png", 0, 0, "w")
    create_image_label(root, "hillstone.png", 0, 1, "e")
    create_image_label(root, "h3c_logo.png", 2, 0, "w")
    create_image_label(root, "52pojie.jpg", 2, 1, "e")
    center_window(root)
    tk.Button(root, text="吾爱出品-https://www.52pojie.cn/", width=40, height=2 ,command=gettext, bg="#CCCCFF").grid(row=2, column=0, columnspan=2)
    root.mainloop()

[最终采用评论老哥@VanYun只把图片进行base64转码来规避这个问题]请教:pyinstaller能否把图片也一起封装单文件内,小弟自己试了几次一直不行,显示找不到我的图片,最后只能把图片跟exe文件放置同目录,如果有大佬知道怎么做到的话,还望指点,谢谢!





源码(lanzou不给传py文件,将txt后缀改为py就可以跑了)下载地址:https://wwd.lanzoul.com/ini1r1mqnpad

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 67haha 发表于 2024-1-24 09:46
sai609 发表于 2024-1-24 01:08
为啥不交给IT运维解决,
为啥随便网页下载一个代码就上线使用
为啥有问题自己不会改

您好,我本身就是IT运维,只是方便自己使用写了一个脚本,代码也不是直接下载的,自己查对应的模块使用方法包括参考网上的例子去一点点编写的,目前是运行中对于我本身是没遇到什么问题的,只是打包这一块有点不懂,不解的内容已经在文中最后写出来了。如果您有好的建议,请告知我,谢谢~
lookfeiji 发表于 2024-1-31 18:00
[Python] 纯文本查看 复制代码
pyinstaller --onefile --windowed  --ico icon.ico --add-data music.py;. MusicPlayer.py   #  --add-data name.jpg;.  这句是加配置文件的

--ico这个是加程序图标的
Songzhiqiang 发表于 2024-1-23 18:04
moruye 发表于 2024-1-23 20:32
感谢分享,下载学习学习
VanYun 发表于 2024-1-23 23:01
图标的话我都是base64编码储存在Python函数里,用到就调用,然后再转换成图片直接显示在窗口上

免费评分

参与人数 1吾爱币 +2 热心值 +1 收起 理由
67haha + 2 + 1 谢谢@Thanks!

查看全部评分

VanYun 发表于 2024-1-23 23:04
VanYun 发表于 2024-1-23 23:01
图标的话我都是base64编码储存在Python函数里,用到就调用,然后再转换成图片直接显示在窗口上

或者是使用Nuitka打包成多文件,然后使用单文件打包软件来打包成临时解压的单文件,比如NSIS什么的
sai609 发表于 2024-1-24 01:08
为啥不交给IT运维解决,
为啥随便网页下载一个代码就上线使用
为啥有问题自己不会改
为啥不改还不自己学
daymissed 发表于 2024-1-24 09:23
感谢分享,下载学习学习
 楼主| 67haha 发表于 2024-1-24 09:47
Songzhiqiang 发表于 2024-1-23 18:04
我用的  auto-py-to-exe,可以将图片打包进去

好的,谢谢您的分享,我这边去尝试下。
qingsong01 发表于 2024-1-25 16:56
感谢分享,学习学习
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 03:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表