-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
156 lines (137 loc) · 5.12 KB
/
models.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
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
155
156
import sqlalchemy
from sentinelbackend.utils import hash_file
from sentinelbackend import db
import os
import datetime
if os.name != 'nt':
import iptc
class Blacklist(db.Model):
# sno = db.Column(db.Integer, primary_key=True, autoincrement=True)
ip = db.Column(db.String, primary_key=True)
port = db.Column(db.String(6), primary_key=True)
class badIP(db.Model):
# sno = db.Column(db.Integer, primary_key=True, autoincrement=True)
ip = db.Column(db.String, primary_key=True)
count = db.Column(db.Integer)
def getbadIphealth(ip):
if ip == 0:
return 0
result = list(badIP.query.filter_by(ip=ip))
if len(result) > 0:
return 6 + int((result.count % 10) / 5) if result.count > 5 else result.count
else:
return 0
class scheduledFiles(db.Model):
file = db.Column(db.String, primary_key=True)
hash = db.Column(db.String)
time = db.Column(db.String)
user = db.Column(db.String)
class badProcess(db.Model):
PID = db.Column(db.Integer, primary_key=True)
IP = db.Column(db.String, primary_key=True)
positives = db.Column(db.Integer)
totals = db.Column(db.Integer)
def addToBlacklist(ip, port):
user = Blacklist(ip=ip, port=port)
try:
db.session.add(user)
db.session.commit()
if port != '*':
command = ("iptables -A INPUT -p tcp --sport {} -s {} -j DROP").format(str(port), str(ip))
if os.name == 'nt':
command = "netsh advfirewall firewall add rule name=IPblock dir=in protocol=tcp remoteip={} localport={} action=block".format(ip, port)
print(command)
os.system(command)
return "blocked"
else:
if os.name == 'nt':
command = "netsh advfirewall firewall add rule name=IPblock dir=in protocol=tcp remoteip={} action=block".format(ip)
print(command)
os.system(command)
return "blocked"
rule = iptc.Rule()
rule.protocol = 0
rule.src = str(ip)
target = iptc.Target(rule, "DROP")
rule.target = target
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
chain.insert_rule(rule)
return "blocked"
except sqlalchemy.exc.IntegrityError:
return "ip {} is already blocked on {} port".format(ip, port if port != '*' else "all")
def removeFromBlacklist(ip, port):
if os.name == 'nt':
user = Blacklist.query.filter_by(ip=ip).filter_by(port=port)
user.delete()
db.session.commit()
command = "netsh advfirewall firewall delete rule name=IPblock dir=in protocol=tcp remoteip={} localport={}".format(ip, port)
if port=='*':
command = "netsh advfirewall firewall delete rule name=IPblock dir=in protocol=tcp remoteip={}".format(ip)
os.system(command)
return "unblocked"
if port != '*':
user = Blacklist.query.filter_by(ip=ip).filter_by(port=port)
check = 0 if len(list(user)) == 0 else 1
if check == 1:
command = ("iptables -D INPUT -p tcp --sport {} -s {} -j DROP").format(str(port), str(ip))
os.system(command)
user.delete()
db.session.commit()
return "unblocked"
else:
return "no such rule present"
else:
blockedIPlist = Blacklist.query.filter_by(ip = ip)
for blackList in blockedIPlist:
if blackList.port == '*':
rule = iptc.Rule()
rule.protocol = 0
rule.src = str(ip)
target = iptc.Target(rule, "DROP")
rule.target = target
chain = iptc.Chain(iptc.Table(iptc.Table.FILTER), "INPUT")
chain.delete_rule(rule)
else:
command = ("iptables -D INPUT -p tcp --sport {} -s {} -j DROP").format(str(blackList.port), str(blackList.ip))
os.system(command)
blockedIPlist.delete()
db.session.commit()
return "unblocked"
def getRules():
return list(map(lambda x: {
"ip": x.ip,
"port": x.port
}, Blacklist.query.all()))
def getScheduledFiles():
return list(map(lambda x: {
"file": x.file,
"hash": x.hash,
"time": x.time,
"user": x.user
}, scheduledFiles.query.all()))
def addScheduledFile(filepath, hash, user="Devansh"):
print(str(datetime.datetime.now()), user)
newFile = scheduledFiles(file=filepath, hash=hash, time=str(datetime.datetime.now()), user=user)
db.session.add(newFile)
db.session.commit()
def removeFileFromScheduled(filepath):
file = scheduledFiles.query.filter_by(file=filepath)
file.delete()
db.session.commit()
def badIPdetected(ip):
oldIp = badIP.query.filter_by(ip=ip)
# print(list(oldIp)[0])
if oldIp is None or len(list(oldIp)) == 0:
newIp = badIP(ip=ip, count=1)
db.session.add(newIp)
db.session.commit()
else:
ip = oldIp.first()
ip.count = ip.count + 1
db.session.commit()
pass
# db.drop_all()
db.create_all()
# addToBlascklist()
# removeFromBlacklist()
# badIPdetected("12.12.12.12")