-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPortBan.py
100 lines (81 loc) · 2.9 KB
/
PortBan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import argparse
import datetime
import os
import uuid
import logging
import yaml
import requests
import re
import flask
from flask import request
ip = "0.0.0.0"
app = flask.Flask(__name__)
log = logging.getLogger('werkzeug')
log.setLevel(logging.CRITICAL)
def readYaml():
with open("config.yaml", "r", encoding="utf-8") as file:
yaml_data = yaml.safe_load(file)
return yaml_data
@app.route('/<aaa>')
def home(aaa):
path = aaa
client_ip = request.remote_addr
if re.match("^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$",client_ip):
if path in rules:
rule = rules.get(path)
port = rule.get("port")
prompt = rule.get("prompt")
allow_access(client_ip,port)
wlog("{} ip: [{}] 已被加入 [{}] 白名单".format(getTime(),client_ip,prompt))
return "你的ip: [{}] 已被加入 [{}] 白名单,端口: {}".format(client_ip,prompt,port)
return "This is not your website, don't open it casually, you can accompany your wife and children when you have time. What? You don't have a girlfriend? Then you must be rich, don't you?"
else:
return "Oh, God, what are you doing? Oh, God, look what this bitch is thinking, get your stupid mouse off your burpsuite interface, you son of a bitch, don't touch your head, I can't figure out what this chicken is thinking."
def wlog(nr):
with open("log.txt","a") as f:
f.write(nr + "\n")
f.close()
def getIP():
url = "http://myip.ipip.net"
req = requests.get(url)
return re.findall("[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}", req.text)[0]
def getuuid():
return str(uuid.uuid4())
def getTime():
return str(datetime.datetime.now())[0:19] + " "
def parRunle(rules):
newDice = {}
for i in rules:
path = i.get("path")
if path == None:
path = getuuid()
newDice[path] = {"prompt":i.get("prompt"),"port":i.get("port"),"release":i.get("release")}
return newDice
def allow_access(ip,port):
comm = "iptables -I INPUT -p tcp --dport {} -s {} -j ACCEPT".format(port, ip)
# print(comm)
os.system(comm)
def banned_all(port):
comm = "iptables -A INPUT -p tcp --dport {} -j DROP".format(port)
# print(comm)
os.system(comm)
def checkRelease(rules):
for i in rules:
rule = rules[i]
banned_all(rule.get('port'))
if rule.get("release") != None:
for x in rule.get("release"):
allow_access(x,rule.get('port'))
if __name__ == '__main__':
yamlDict = readYaml()
webport = yamlDict.get("webport")
init = yamlDict.get("init")
os.system(init)
rules = parRunle(yamlDict.get("rules"))
# print(rules)
checkRelease(rules)
myip = getIP()
for i in rules:
rule = rules.get(i)
print("{}{}: http://{}:{}/{}".format(rule.get("prompt"),rule.get("port"),myip,webport,i))
app.run(host="0.0.0.0",port=webport)