forked from NetworkScout/ns
/
nsserver.py
139 lines (118 loc) · 3.99 KB
/
nsserver.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
#!/usr/bin/python
############################################################################
# Network-Scout - An Addition to Artillery #
# An artillery logging and web interface #
# By Shawn Jordan and Aedan Somerville #
# Special thanks to Dave Kennedy, DOW Chemical Co., Marshall University #
# Adafruit, Jusbour and the Open Source Community #
# Version 2.0 "THUNDERING HERD" #
################################# GO HERD #######################################
#################################################################################
from source import core
import sys, os, subprocess, socket, MySQLdb
#Starting Server side programs
try:
while True:
#(ASSIGN HOST AND PORT VARIABLES (HOST IS LOCAL, PORT IS 514 DESIGNATED BY -
#ARTILLERY PORT)
HOST = ''
PORT = 514
flag = 1
#OPEN THE TCP CONNECTION
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST,PORT))
s.listen(4)
(conn, (ip, port)) = s.accept()
data = conn.recv(2048)
#RECEIVE DATA FROM TCP CONNECTION
dfile = open("/var/networkscout/stuff/recievedinfo", "w")
dfile.write( (data) )
dfile.close()
#send back command/message
conn.send("You're message has been recieved.")
conn.close()
s.close()
##SETS MYSQL OR FILE CREATION
rpi = core.read_config("IS_R_PI")
if rpi == "NO":
log_size = os.stat("/var/networkscout/stuff/recievedinfo").st_size
if log_size < 1:
pass
else:
#LOOP OVER THE FILE TO READ ALL THE LINES
of_object = open("/var/networkscout/stuff/recievedinfo", "r")
webinfo = open("/var/networkscout/stuff/webinfo","a")
webinfo.write(of_object)
flag = 0
#checks to ensure all data is in database
if flag is 1:
pass
else:
cleanfile=open('/var/networkscout/stuff/recievedinfo','w')
cleanfile.write(' ')
cleanfile.close()
elif rpi == "YES":
log_size = os.stat("/var/networkscout/stuff/recievedinfo").st_size
if log_size < 1:
pass
else:
#LOOP OVER THE FILE TO READ ALL THE LINES
of_object = open("/var/networkscout/stuff/recievedinfo", "r")
loader = file.readlines(of_object)
of_object.close()
#ASSIGN 0 TO ALL VARIABLES
clip = []
ip = ''
eventtime = ''
alert = ''
mess = ''
flag = 0
#OPEN DATABASE TO MAKE SERVER CONNECTION
db = MySQLdb.connect("localhost","root","raspberry","Network_Scout")
cursor = db.cursor()
for shell in loader:
try:
clip = shell.split(',')
ip = clip[0]
eventtime = clip[1]
alert = clip[2]
mess = clip[3]
#PREPARE SQL QUERY TO INSERT A RECORD INTO THE DATABASE
sql = "INSERT INTO Attacks (rpi_ip,time,alert_level,message) VALUES (\'" + ip + "\',\'" + eventtime + "\',\'" + alert + "\',\'" + mess + "\');"
clip[:] = []
try:
#EXECUTE THE SQL COMMAND
cursor.execute(sql)
#COMMIT YOUR CHANGES IN THE DATABASE
db.commit()
except Exception, e:
#ROLLBACK IN CASE THERE IS AN ERROR
db.rollback()
print("Error: " + format(e))
print("Database was rolled back...")
flag=1
pass
except:
pass
#DISCONNECT FROM SERVER
db.close()
#checks to ensure all data is in database
if flag is 1:
pass
else:
#cleans file when all information is stored in MySQL
cleanfile=open('/var/networkscout/stuff/recievedinfo','w')
cleanfile.write(' ')
cleanfile.close()
else:
raise Error("The config file is incorrectly configured.");
except sys.excepthook, e:
print("Network-Scout had an issue... " + format(e))
pass
except KeyboardInterrupt:
print("Wibbly Wobbly Timey Wimey...Stuff")
sys.exit()
except Exception, e:
print("Network-Scout had an issue... " + format(e))
pass