def get_indicators(): con = helpers.db_connection() indicatorlist = [] with con: cur = con.cursor() cur.execute("SELECT * FROM indicators") indicators = cur.fetchall() names = [description[0] for description in cur.description] for ind in indicators: newdict = {} for i in names: newdict[i] = str(ind[i]) indicatorlist.append(newdict) return jsonify({'indicators': indicatorlist})
def get_network(): con = helpers.db_connection() indicatorlist = [] with con: cur = con.cursor() cur.execute("SELECT * FROM indicators where type='IPv4' or type='IPv6' or type='Domain' or type='Network'") indicators = cur.fetchall() names = [description[0] for description in cur.description] for ind in indicators: newdict = {} for i in names: newdict[i] = str(ind[i]) indicatorlist.append(newdict) return jsonify({'network_indicators': indicatorlist})
def get_campaigns(campaign): con = helpers.db_connection() indicatorlist = [] campaign = urllib.unquote(campaign).decode('utf8') with con: cur = con.cursor() cur.execute("SELECT * FROM indicators where campaign='" + campaign + "'") indicators = cur.fetchall() names = [description[0] for description in cur.description] for ind in indicators: newdict = {} for i in names: newdict[i] = str(ind[i]) indicatorlist.append(newdict) return jsonify({'campaigns': indicatorlist})
def get_relationships(ip): con = helpers.db_connection() indicatorlist = [] with con: cur = con.cursor() cur.execute("SELECT relationships from indicators where object='" + ip + "'") rels = cur.fetchall() rels = rels[0][0] rellist = rels.split(",") temprel = {} for rel in rellist: try: with con: cur = con.cursor() cur.execute("SELECT * from indicators where object='" + str(rel) + "'") reltype = cur.fetchall() reltype = reltype[0] temprel[reltype['object']] = reltype['type'] except: pass return jsonify({'relationships': temprel})
import helpers # General utils including config params and database connection conf = helpers.get_config() DBHOST = conf["MySQL"]["server"] DBUSER = conf["MySQL"]["dbuser"] DBNAME = conf["MySQL"]["dbname"] DBCHARSET = conf["MySQL"]["dbcharset"] if __name__ == '__main__': connection = helpers.db_connection(DBHOST, DBUSER, DBNAME, DBCHARSET) tests = { 'create_test_tab': ('create table bot_test_tab (col1 varchar(10), col2 integer)', None), 'insert_test_tab': ('insert into bot_test_tab values (%s, %s)', ('a', 1)), 'select_test_tab': ('select col1 from bot_test_tab where col2 = (%s)', (1)), 'drop_test_tab': ('drop table if exists bot_test_tab', None) } test_sequence = ('drop_test_tab', 'create_test_tab', 'insert_test_tab', 'select_test_tab', 'drop_test_tab') for key in test_sequence: print("execute", key, "Args:", tests[key][1], end=" ") sql = tests[key][0] args = tests[key][1] c = connection.cursor() ret = c.execute(sql, args)
broker.publish(inbound_topic, "ok", 0) else: broker.publish(inbound_topic, "no_session", 0) except: broker.publish(inbound_topic, "error", 0) if __name__ == "__main__": current_dir = os.path.dirname(os.path.realpath(__file__)) with daemon.DaemonContext(working_directory=current_dir): if not config.load(): sys.exit(1) # Check if the database is accessible try: helpers.db_connection() except: sys.exit(1) # Create and connect to the MQTT broker broker = mqtt.Client() broker.on_connect = on_connect broker.on_message = on_message try: broker.connect(config.broker_address, config.broker_port) except: sys.exit(1) # Enter loop to receive messages (handles auto-reconnecting) broker.loop_forever()
def session(connection): # Get Config conf = helpers.get_config() DBHOST = conf["MySQL"]["server"] DBUSER = conf["MySQL"]["dbuser"] DBNAME = conf["MySQL"]["dbname"] logging.info("Starting Bot session-thread...") # Initialize the database connection logging.info(" session-thread connecting to database...") dbconnection = helpers.db_connection(DBHOST, DBUSER, DBNAME) dbcursor = dbconnection.cursor() dbconnectionid = helpers.db_connectionid(dbcursor) logging.info(" ...connected") botSentence = 'Hello!' weight = 0 trainMe = False checkStore = False def receive(connection): logging.debug(" receive(connection): PID {}, thread {} \n".format( pid, thread)) received = connection.recv(1024) if not received: return False else: return received while True: pid = os.getpid() thread = threading.current_thread() # pass received message to chatbot received = receive(connection) humanSentence = received.decode().strip() if humanSentence == '' or humanSentence.strip(punctuation).lower( ) == 'quit' or humanSentence.strip(punctuation).lower() == 'exit': break # Chatbot processing botSentence, weight, trainMe, checkStore = chatlogic.chat_flow( dbcursor, humanSentence, weight) logging.debug( " Received botSentence {} from chatbot.chat_flow".format( botSentence)) if trainMe: logging.debug(" trainMe is True") send = "Please train me by entering some information for me to learn, or reply \"skip\" to skip' ".encode( ) connection.send(send) previousSentence = humanSentence received = receive(connection) humanSentence = received.decode().strip() logging.debug(" trainMe received {}".format(humanSentence)) if humanSentence != "skip": chatlogic.train_me(previousSentence, humanSentence, dbcursor) botSentence = "Thanks I have noted that" else: botSentence = "OK, moving on..." trainMe = False if checkStore: logging.debug("CheckStore is True") send = 'Shall I store this information as a fact for future reference? (Reply "yes" to store)'.encode( ) connection.send(send) previousSentence = humanSentence received = receive(connection) humanSentence = received.decode().strip() logging.debug(" checkStore received {}".format(humanSentence)) if regexpYes.search(humanSentence.lower()): #Store previous Sentence logging.debug(" Storing...") chatlogic.store_statement(previousSentence, dbcursor) logging.debug(" Statement Stored.") botSentence = random.choice(chatlogic.STATEMENT_STORED) else: botSentence = "OK, moving on..." checkStore = False dbconnection.commit() logging.debug(" sending botSentence back: {}".format(botSentence)) send = botSentence.encode() connection.send(send) logging.info(" Closing Session")
return botSentence, weight, trainMe, checkStore if __name__ == "__main__": conf = helpers.get_config() regexpYes = re.compile(r'yes') DBHOST = conf["MySQL"]["server"] DBUSER = conf["MySQL"]["dbuser"] DBNAME = conf["MySQL"]["dbname"] print("Starting Bot...") # initialize the connection to the database print("Connecting to database...") connection = helpers.db_connection(DBHOST, DBUSER, DBNAME) cursor = connection.cursor() connectionID = helpers.db_connectionid(cursor) print("...connected") trainMe = False checkStore = False botSentence = 'Hello!' while True: # Output bot's message if DEBUG_WEIGHT: print('Bot> ' + botSentence + ' DEBUG_WEIGHT:' + str(round(weight,5) ) ) else: print('Bot> ' + botSentence)