Skip to content

Commit ded16c2

Browse files
committed
Merge branch 'dev'
2 parents 73c14ef + ab88de8 commit ded16c2

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

Config/config.default.json

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"deny_type": "DROP",
66
"ignored_ips": ["127.0.0.1"],
77
"request_time": 5,
8-
"check_time": 600,
9-
"run_once": true,
108
"reload_iptables": true,
119
"rules": {
1210
"ssh": {

README.md

+12-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# pyFilter
2-
pyFilter aims to filter out all of the requests that are not legitimate to your server, and blocks them if too many are sent. It works by reading log files and checking if a failed request has came from the same IP address within a user configurable amount of time and adding rules to the firewall if too many attempts have been captured.
1+
# PyFilter
2+
PyFilter aims to filter out all of the requests that are not legitimate to your server, and blocks them if too many are sent. It works by reading log files and checking if a failed request has came from the same IP address within a user configurable amount of time and adding rules to the firewall if too many attempts have been captured.
33

4-
By default pyFilter is configured to read from `/var/log/auth.log` for incoming SSH requests, however there are options for `Apache, Nginx and MySQL` too.
4+
By default PyFilter is configured to read from `/var/log/auth.log` for incoming SSH requests, however there are options for `Apache, Nginx and MySQL` too.
55

6-
pyFilter uses a database to store all the banned ip addresses to ensure ips arent added more than once. pyFilter currently supports sqlite and redis, by default it is setup to use sqlite so no installation of a redis server is needed. However redis has support for cross server ban syncing (more info below).
6+
PyFilter uses a database to store all the banned ip addresses to ensure ips arent added more than once. PyFilter currently supports sqlite and redis, by default it is setup to use sqlite so no installation of a redis server is needed. However redis has support for cross server ban syncing (more info below).
77

88
Installation:
99
-------------
@@ -15,9 +15,9 @@ Optional:
1515
- [py-redis](https://pypi.python.org/pypi/redis)
1616
- [redis](https://redis.io)
1717

18-
To install pyFilter download the files from this repo via your preferred method, for example `git clone https://github.com/Jason2605/pyFilter.git`.
18+
To install PyFilter download the files from this repo via your preferred method, for example `git clone https://github.com/Jason2605/PyFilter.git`.
1919

20-
**Optional:** `install.sh` will setup a service for pyFilter, and you can start/stop it by using `sudo systemctl start/stop pyFilter` and get the status of the pyFilter service using `sudo systemctl status pyFilter`. To run this make sure you give permission to the `install.sh` file `sudo chmod +x install.sh`.
20+
**Optional:** `install.sh` will setup a service for PyFilter, and you can start/stop it by using `sudo systemctl start/stop PyFilter` and get the status of the PyFilter service using `sudo systemctl status PyFilter`. To run this make sure you give permission to the `install.sh` file `sudo chmod +x install.sh`.
2121

2222
**Note: The default configuration file runs on sqlite, so installing py-redis and redis are optional.**
2323

@@ -49,7 +49,7 @@ Starting/stopping redis
4949
Configuration:
5050
-------------
5151

52-
- Copy the [default config file](https://github.com/Jason2605/pyFilter/blob/master/Config/config.default.json) and call it config.json.
52+
- Copy the [default config file](https://github.com/Jason2605/PyFilter/blob/master/Config/config.default.json) and call it config.json.
5353
```json
5454
{
5555
"settings": {
@@ -101,7 +101,7 @@ Configuration:
101101
}
102102
},
103103
"sqlite": {
104-
"database": "pyFilter.db"
104+
"database": "PyFilter.db"
105105
},
106106
"redis": {
107107
"host": "127.0.0.1",
@@ -135,7 +135,7 @@ To swap from sqlite to redis, change the current value `"database": "sqlite"` to
135135

136136
### Reload iptables
137137

138-
`iptables` is not persistant over restarts, so this setting will reload the table with the saved bans so far on launch and update the rules.
138+
`iptables` is not persistent over restarts, so this setting will reload the table with the saved bans so far on launch and update the rules.
139139

140140
### Log files
141141

@@ -164,7 +164,7 @@ The time format needs to match the log format to form a datetime object. For exa
164164

165165
### Ignored IP addresses
166166

167-
This is quite explanitory, if a regex matches however the IP address is within this list, it will be ignored so that IP address will not get banned.
167+
This is quite explanatory, if a regex matches however the IP address is within this list, it will be ignored so that IP address will not get banned.
168168

169169
You can add more IPs
170170
`"ignored_ips": ["127.0.0.1", "123.456.789.1"]`
@@ -179,15 +179,7 @@ Deny type is the way iptables will deal with the incoming packets, `DENY` is rec
179179

180180
### Failed attempts
181181

182-
Failed attempts is the number of matches that IP address needs to get trying to connect each rule for it to get blacklisted, for example `"failed_attempts": 5` 5 failed attempts on an SSH connection will get it banned, however 3 on SSH and 2 on MySQL will not get it banned, they are seperate.
183-
184-
### Run once
185-
186-
This setting will only run the script once and not check many times, useful if you want to create your own more advanced schedules.
187-
188-
### Check time
189-
190-
Check time is the amount of time in seconds it takes to do each rule, for example `"check_time": 600` check time is 600 seconds and there are 4 rules, there will be a gap of 150 seconds until the next rule is run, and a 600 second wait until the same rule is run again.
182+
Failed attempts is the number of matches that IP address needs to get trying to connect each rule for it to get blacklisted, for example `"failed_attempts": 5` 5 failed attempts on an SSH connection will get it banned, however 3 on SSH and 2 on MySQL will not get it banned, they are separate.
191183

192184
### Redis - Optional
193185

@@ -213,7 +205,7 @@ Enables/disables cross server ban syncing.
213205

214206
### Name
215207

216-
This is the name of the server, this **has** to be different for each server running pyFilter or the bans will not get synced properly. This name can be anything as long as it is unique, for example `"name": "VPS-Lon-1"`.
208+
This is the name of the server, this **has** to be different for each server running PyFilter or the bans will not get synced properly. This name can be anything as long as it is unique, for example `"name": "VPS-Lon-1"`.
217209

218210
### Check time
219211

pyFilter/database.py

+7
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class RedisConnection:
1414
Args:
1515
Dictionary passed from config.json
1616
"""
17+
1718
def __init__(self, config):
1819
if Redis is None:
1920
raise ImportError("Redis isn't installed!")
@@ -33,6 +34,7 @@ def insert(self, ip):
3334
Args:
3435
ip: IP address to be inserted into redis
3536
"""
37+
3638
self.redis_connection.hmset(ip, {self.name: 1})
3739

3840
def select(self, ip):
@@ -45,6 +47,7 @@ def select(self, ip):
4547
Returns:
4648
Returns 1 (integer) if IP address is found else None
4749
"""
50+
4851
return self.redis_connection.hget(ip, self.name)
4952

5053
def scan(self):
@@ -56,6 +59,7 @@ def scan(self):
5659
Returns:
5760
Returns a list of all IPs not relating to the name of this "server" from the passed config
5861
"""
62+
5963
all_results = []
6064
cursor = 0
6165
while True:
@@ -83,6 +87,7 @@ class SqliteConnection:
8387
Args:
8488
Dictionary passed from config.json
8589
"""
90+
8691
def __init__(self, config):
8792
database = config["database"]
8893
self.sqlite_connection = sqlite3.connect(database, check_same_thread=False)
@@ -98,6 +103,7 @@ def insert(self, ip):
98103
Args:
99104
ip: IP address to be inserted into sqlite
100105
"""
106+
101107
cursor = self.sqlite_connection.cursor()
102108
try:
103109
cursor.execute("INSERT INTO banned_ip VALUES (?)", (ip,))
@@ -117,6 +123,7 @@ def select(self, ip):
117123
Returns:
118124
Returns ip address as a string if found, else None is returned
119125
"""
126+
120127
cursor = self.sqlite_connection.cursor()
121128
cursor.execute("SELECT ip FROM banned_ip WHERE ip = ?", (ip,))
122129
ip = cursor.fetchone()

pyFilter/py_filter.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ def read_files(self, pattern_type="ssh"):
5656
where = f.tell()
5757
line = f.readline()
5858
if not line:
59-
if self.settings["run_once"]:
60-
return
6159

6260
if inode != os.stat(log_file).st_ino:
6361
break
@@ -74,7 +72,6 @@ def read_files(self, pattern_type="ssh"):
7472

7573
found = found[0]
7674
self.filter(pattern_type, found)
77-
7875
time.sleep(0.0001) # Ensure it doesnt kill CPU
7976

8077
def filter(self, pattern_type, found):
@@ -196,7 +193,7 @@ def make_persistent(self):
196193
print("Saving newly blacklisted IP's!")
197194
subprocess.call("iptables-save > Config/blacklist.v4", shell=True)
198195
self.ip_blacklisted = False
199-
time.sleep(300)
196+
time.sleep(300)
200197

201198
def monitor_redis(self):
202199
"""

0 commit comments

Comments
 (0)