def scan_act3(): with ArpScan.this_lock: if ArpScan.this_stroke_collection == None: return ArpDump.printout("scan_act3") ArpScan.this_stroke_thread.join() ArpScan.this_stroke_thread = None # reading, scanning and analyzing ex_collection = DBCurrent.load_collection() # ArpDump.printout(new_collection) diff_result = VMDiff.diff(ex_collection, ArpScan.this_stroke_collection) if not diff_result.empty(): # ArpDump.printout(diff_result) # return DBCurrent.clear() DBCurrent.store_collection(ArpScan.this_stroke_collection) DBHistory.store_collection(diff_result) ArpScan.this_stroke_act.status = 1 ArpScan.do_mail(diff_result) else: ArpScan.this_stroke_act.status = 2 # ArpDump.printout(cur_act.status) DBAct.set_record(ArpScan.this_stroke_act.getdate(), ArpScan.this_stroke_act.status, ArpScan.this_stroke_act.type) ArpScan.this_stroke_collection = None ArpScan.this_stroke_act = None ArpScan.start_date = datetime.now()
def create_db(): if not DBLayer.connection: ArpDump.printout("DBBio: create_db: No connection established!") return c = DBLayer.connection.cursor() c.execute('''create table if not exists vms_bio(ip text, desc text)''') DBLayer.connection.commit()
def create_db(): if not DBLayer.connection: ArpDump.printout("DBAct: create_db: No connection established!") return c = DBLayer.connection.cursor() c.execute('''create table if not exists vms_acts(changedate text, status int, type int)''') DBLayer.connection.commit()
def do_mail(diff_result): try: for recipient in VMSettings.inform_to_list: sbody = str(diff_result) VMInform.send_an_email(str(VMSettings.inform_from_name), str(recipient), "There was changes", sbody) except Exception as ex: ArpDump.printout("ArpScan.do_mail: unable to send mail")
def clear(): if not DBLayer.is_connected(): return 0 try: c = DBLayer.connection.cursor() c.execute("delete from vms_bio") DBLayer.connection.commit() except Exception as ex: ArpDump.printout("DBBio.clear: unable to clear vms_current table")
def create_db(): if not DBLayer.connection: ArpDump.printout( "DBCurrent: create_db: No connection established!") return c = DBLayer.connection.cursor() c.execute( '''create table if not exists vms_current(ip text, mac text, name text)''' ) DBLayer.connection.commit()
def clear(): if not DBLayer.is_connected(): return False try: ArpDump.printout("clear!") c = DBLayer.connection.cursor() c.execute('''delete * from vms_acts''') DBLayer.connection.commit() except Exception as ex: ArpDump.printout("DBAct.clear: unable to clear vms_acts table")
def scan_act2(): if (VMSettings.arp_daemon_scan_type == '1'): new_collection = ArpScanArp.scan() elif (VMSettings.arp_daemon_scan_type == '2'): new_collection = ArpScanScapy.scan() else: new_collection = [] with ArpScan.this_lock: ArpScan.this_stroke_collection = new_collection ArpDump.printout("scan_act2")
def parse_arp_line(arp_line): if len(arp_line) == 0: ArpDump.printout("Error: parse_arp_line: arp_line length is 0") return arp_params = arp_line.split("\t") if len(arp_params) != 3: ArpDump.printout( "Error: parse_arp_line: arp_params length is not 3") return new_vm = VMRecord(arp_params[0], arp_params[1], arp_params[2]) return new_vm
def send_an_email(sfrom, sto, ssubject, smsg): try: msg = MIMEText(str) msg['Subject'] = ssubject msg['From'] = sfrom msg['To'] = sto s = smtplib.SMTP('localhost') s.sendmail(sfrom, [sto], msg.as_string()) s.quit() except Exception as ex: ArpDump.printout("ArpScan.do_mail: unable to send mail from {0} to {1}. Reason: {2}".format(sfrom, sto, ex))
def load_collection(): if not DBLayer.is_connected(): return 0 collection = VMBioCollection() try: c = DBLayer.connection.cursor() res = c.execute("select ip, desc from vms_bio") for item in res: item = VMBio(item[0], item[1]) collection.append(item) except Exception as ex: ArpDump.printout("DBBio.load_collection: unable to load collection") return collection
def analyze(data): ArpDump.printout("received: " + data) cmds = data.split("|") if len(cmds) > 0: pcmd = ArpShuttle() pcmd.cmd = cmds[0] pcmd.add_keys = len(cmds) - 1 cmds.insert(0, "-") pcmd.args = cmds with ArpScan.this_lock: ArpScan.this_pcmd = pcmd pcmd.event.wait() return pcmd.answer return "{}"
def load_collection(): if not DBLayer.is_connected(): return 0 collection = VMRecordCollection() try: c = DBLayer.connection.cursor() res = c.execute("select ip, mac, name from vms_current") for item in res: item = VMRecord(item[0], item[1], item[2]) collection.append(item) except Exception as ex: ArpDump.printout( "DBCurrent.load_collection: unable to load collection") return collection
def scan_act1(_type=0): with ArpScan.this_lock: if ArpScan.this_stroke_thread == None: ArpDump.printout("scan_act1") ArpScan.start_date = datetime.now() ArpScan.this_stroke_act = VMAct() ArpScan.this_stroke_act.type = _type DBAct.set_record(ArpScan.this_stroke_act.getdate(), ArpScan.this_stroke_act.status, ArpScan.this_stroke_act.type) ArpScan.this_stroke_collection = None ArpScan.this_stroke_thread = ArpScanThreadStrike( ArpScan.scan_act2) return True return False
def read_from_bytes(barray): collection = VMRecordCollection() try: arp_lines = barray.decode("utf-8").split("\n") i = 0 for line in arp_lines: if i > 1: if len(line) > 0: new_vm = ArpScanArp.parse_arp_line(line) collection.append(new_vm) else: break i += 1 except Exception as ex: ArpDump.printout("unable to perform file read") return collection
def load_collection(limit): if not DBLayer.is_connected(): return False collection = VMActCollection() try: c = DBLayer.connection.cursor() ilimit = int(limit) if (ilimit == 0): res = c.execute("select changedate, status, type from vms_acts order by changedate desc") else: _limit = (int(limit),) res = c.execute("select changedate, status, type from vms_acts order by changedate desc limit ?", _limit) for item in res: collection.append(VMAct(item[0], item[1], item[2])) except Exception as ex: ArpDump.printout("DBAct.load_collection: unable to load collection") return collection
def run(): # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Bind the socket to the port server_address = (VMSettings.arp_daemon_address, int(VMSettings.arp_daemon_port)) ArpDump.printout('starting up on %s port %s' % server_address) sock.bind(server_address) # Listen for incoming connections sock.listen(1) while True: # Wait for a connection ArpDump.printout('waiting for a connection') connection, client_address = sock.accept() try: ArpDump.printout('connection from {0}'.format(client_address)) # Receive the data in small chunks and retransmit it data_ = "" while True: data = connection.recv(2048).decode("utf-8") data_ += data #print("strike: " + data_ + "\n") if len(data_) > 1: if data[0] == '|': i = 1 strlen = "" while data_[i] != '|': strlen += data_[i] i += 1 istrlen = int(strlen) if istrlen + 2 + len(strlen) == len(data_): data_ = data_[4:] break if len(data_) > 0: data_out = ArpScan.analyze(data_) data_out_ = "|" data_out_ += str(len(data_out)) data_out_ += "|" data_out_ += data_out connection.sendall(data_out_.encode("utf-8")) finally: # Clean up the connection connection.close()
while data_[i] != '|': strlen += data_[i] i += 1 istrlen = int(strlen) if istrlen + 2 + len(strlen) == len(data_): data_ = data_[4:] break if len(data_) > 0: data_out = ArpScan.analyze(data_) data_out_ = "|" data_out_ += str(len(data_out)) data_out_ += "|" data_out_ += data_out connection.sendall(data_out_.encode("utf-8")) finally: # Clean up the connection connection.close() if __name__ == "__main__": arp_init.init() ArpDump.printout("Starting daemon...") ArpScan.start() ArpDump.printout("Starting the server...") run()