This repository was archived by the owner on May 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathhome.py
More file actions
155 lines (125 loc) · 4.02 KB
/
home.py
File metadata and controls
155 lines (125 loc) · 4.02 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from flask import Flask, render_template, redirect, request
from flask.ext.basicauth import BasicAuth
import requests
import sys, os
import json
import ConfigParser
app = Flask(__name__)
#############################
# Password Protection #
#############################
app.config['BASIC_AUTH_USERNAME'] = 'ouss'
app.config['BASIC_AUTH_PASSWORD'] = 'pass'
basic_auth = BasicAuth(app)
@app.route('/')
@basic_auth.required
def secret_view():
return redirect('/home', code=302)
#############################
# Home page / start / stop #
#############################
def fail2banStatus():
f = os.popen('service fail2ban status')
status = f.read()
if ("inactive" in status or "not running" in status):
return "fail2ban is not running"
elif ("active" in status or "is running" in status):
return "fail2ban is running"
@app.route('/home', methods=['GET', 'POST'])
@basic_auth.required
def home():
status = fail2banStatus()
return render_template('index.html', status = status)
@app.route('/start', methods=['GET', 'POST'])
def start():
s = os.popen('service fail2ban start')
status = fail2banStatus()
return redirect("/", code=302)
@app.route('/stop', methods=['GET', 'POST'])
def stop():
s = os.popen('service fail2ban stop')
status = fail2banStatus()
return redirect("/", code=302)
##############################
# Config page #
##############################
@app.route('/config', methods=['GET','POST'])
@basic_auth.required
def config():
cp= ConfigParser.RawConfigParser()
cp.read( r"/etc/fail2ban/jail.conf" )
services = cp.sections()
return render_template('config.html', cp = cp, services = services)
@app.route('/enable/<s>', methods=['GET','POST'])
def enable(s=None):
cp= ConfigParser.RawConfigParser()
cp.read(r'/etc/fail2ban/jail.conf')
cp.set(s, 'enabled', 'true')
with open('/etc/fail2ban/jail.conf', 'w') as configfile:
cp.write(configfile)
f = os.popen('service fail2ban restart')
services = cp.sections()
return redirect("/config", code=302)
@app.route('/disable/<s>', methods=['GET','POST'])
def disable(s=None):
cp= ConfigParser.RawConfigParser()
cp.read(r'/etc/fail2ban/jail.conf')
cp.set(s, 'enabled', 'false')
with open('/etc/fail2ban/jail.conf', 'w') as configfile:
cp.write(configfile)
f = os.popen('service fail2ban restart')
services = cp.sections()
return redirect("/config", code=302)
##############################
# Filter page #
##############################
@app.route('/display/<s>', methods=['GET','POST'])
@basic_auth.required
def filter(s):
try:
filt = s
cp= ConfigParser.RawConfigParser()
file = "/etc/fail2ban/filter.d/"+filt+".conf"
h = open(file,'r')
f = h.read()
except IOError:
return render_template('filter.html', f = "there is a problem with this filter")
return render_template('filter.html', f = f, service = filt)
@app.route('/save/<s>', methods=['GET','POST'])
def save_filter(s):
try:
f = request.form['filter']
filt = s
file = "/etc/fail2ban/filter.d/"+filt+".conf"
h = open(file,'w')
h.write(f)
h.close()
except IOError:
return render_template('filter.html', f = "there is a problem with this filter")
return redirect('/config', code=302)
##############################
# Banned IP #
##############################
def getcountry(ip):
r = requests.get('http://ip-api.com/json/'+ip)
parsed_json=r.json()
return parsed_json
@app.route('/banned', methods=['GET', 'POST'])
@basic_auth.required
def banned():
f = os.popen("cat /var/log/fail2ban.log | grep Ban | awk '{print $7}'")
banned = f.read()
theFile = open('/var/log/fail2ban.log','r')
FILE = theFile.readlines()
theFile.close()
printList = []
for line in FILE:
if ('Ban' in line):
printList.append(line)
return render_template('banned.html', printList = printList, getcountry=getcountry)
##############################
# App launcher #
##############################
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0')