Exemple #1
0
def daemonize(read_pipe, write_pipe, queries, replies, keys):
    global interface_list
    #Creates interface_list sequencially
    status = interface_list_init(queries, keys)
    gurulhutils.status_check(status, write_pipe)

    router_thread = threading.Thread(target=route_replies,
                                     args=[replies],
                                     daemon=True)
    router_thread.start()

    try:
        while (status != "down"):
            gurulhutils.sleep(1)
            for interface in interface_list:
                if (interface["status"] == "down"):
                    print(interface["name"] + " Interface is down.",
                          flush=True)
                    interface["read_pipe"].close()
                    interface["write_pipe"].close()
                    interface = interface_handler_init

    except Exception as e:
        if (debug):
            print("Error in interface_handler.py, daemonize(): ",
                  e,
                  flush=True)
Exemple #2
0
def server_loop():
    global server_socket
    global slave_list
    global module_list

    while (True):
        gurulhutils.sleep(1)
        try:
            slave_socket, address = server_socket.accept()
            slave_socket.setblocking(True)
            slave = {"socket": slave_socket, "address": address}
            handshake = gurulhutils.socket_recv(slave_socket)

            slave.update(handshake)

            gurulhutils.socket_send(slave_socket, "ping")
            pong = gurulhutils.socket_recv(slave_socket)

            if (pong == "pong"):
                for module in slave["modules"]:
                    if module not in module_list:
                        module_list.append(module)

                slave_socket.setblocking(False)
                slave_list.append(slave)

        except BlockingIOError:
            pass
        except Exception as e:
            if (debug):
                print("Error in botnet_handler.py, server_loop: ",
                      e,
                      flush=True)
Exemple #3
0
def daemonize(read_pipe, write_pipe, queries, replies, keys):
    global server_socket
    global slave_list
    global module_list

    slave_list = []
    module_list = []

    status = server_setup()
    gurulhutils.status_check(status, write_pipe)

    server_thread = threading.Thread(target=server_loop, daemon=True)
    router_thread = threading.Thread(target=route_loop,
                                     args=[write_pipe, queries],
                                     daemon=True)
    listen_thread = threading.Thread(target=listen_loop,
                                     args=[replies],
                                     daemon=True)

    server_thread.start()
    router_thread.start()
    listen_thread.start()

    while (True):
        gurulhutils.sleep(1)
        pass
Exemple #4
0
def get_replies(read_pipe):
    global replies

    while (True):
        gurulhutils.sleep(1)
        read_pipe.poll()
        reply = read_pipe.recv()
        replies.append(reply)
Exemple #5
0
def digest_queries(queue):
    global queries

    while (True):
        gurulhutils.sleep(1)
        for query in queries:
            queue.put(query)
            queries.remove(query)
Exemple #6
0
def route_replies(replies):
    global interface_list
    while (True):
        gurulhutils.sleep(1)
        reply = replies.get()
        if (reply["rtype"] and reply["rtype"] != "None"):
            for interface in interface_list:
                if (reply["rinterface"] == interface["name"]):
                    interface["write_pipe"].send(reply)
                    break
Exemple #7
0
def digest_replies():
    global replies

    response_dictionary = {
        "text": bot.sendMessage,
        "photo": bot.sendPhoto,
        "audio": bot.sendAudio,
        "file": bot.sendDocument,
        "video": bot.sendVideo,
        "voice": bot.sendVoice,
        "location": bot.sendLocation,
        "contact": bot.sendContact,
    }

    while (True):
        gurulhutils.sleep(1)
        for reply in replies:
            print(reply, flush=True)
            try:
                if (reply["qtype"] == "inline_query"):
                    bot.answerInlineQuery(inline_query_id=reply["id"],
                                          results=[{
                                              "type":
                                              "article",
                                              "id":
                                              reply["id"],
                                              "title":
                                              reply["qcontent"],
                                              "description":
                                              reply["rcontent"],
                                              "message_text":
                                              reply["rcontent"]
                                          }])
                elif ("keyboard" in reply.keys()):
                    response_dictionary[reply['rtype']](
                        reply['to'],
                        reply['rcontent'],
                        reply_markup=reply["keyboard"])
                else:
                    response_dictionary[reply['rtype']](reply['to'],
                                                        reply['rcontent'])
                replies.remove(reply)
            except Exception as e:
                if (debug):
                    print("Error in telegram_interface.py, digest_replies(): ",
                          e,
                          flush=True)
Exemple #8
0
def daemonize(read_pipe, write_pipe, queue, key):
    global queries
    global replies

    queries = []
    replies = []
    status = init(key)
    gurulhutils.status_check(status, write_pipe)

    thread_dictionary = {
        "get_queries": [get_queries, (), True, "get_queries"],
        "digest_queries": [digest_queries, [queue], True, "digest_queries"],
        "get_replies": [get_replies, [read_pipe], True, "get_replies"],
        "digest_replies": [digest_replies, (), True, "digest_replies"]
    }

    thread_list = []

    for thread in thread_dictionary.values():
        thread_list.append(
            threading.Thread(target=thread[0],
                             args=thread[1],
                             daemon=thread[2],
                             name=thread[3]))

    for thread in thread_list:
        thread.start()

    while (status != "down"):
        gurulhutils.sleep(1)
        try:
            for thread in thread_list:
                if (not thread.is_alive()):
                    thread = threading.Thread(
                        target=thread_dictionary[thread.name][0],
                        args=thread_dictionary[thread.name][1],
                        daemon=thread_dictionary[thread.name][2],
                        name=thread_dictionary[thread.name][3])

        except Exception as e:
            if (debug):
                print("Error in telegram_interface.py, main(): ",
                      e,
                      flush=True)
            status = "down"
Exemple #9
0
def get_queries():
    global queries

    last_read = 0

    while (True):
        gurulhutils.sleep(1)
        try:
            updates = bot.getUpdates(last_read, timeout=5)

            for query in updates:
                try:
                    fix_edited_message(query)
                    content_type, chat_type, chat_id = telepot.glance(
                        query["message"])
                    queries.append({
                        u'qtype': content_type,
                        u'qinterface': u'Telegram',
                        u'qcontent': query['message'][content_type],
                        u'from': chat_id,
                        u'extra': query
                    })
                except:
                    query_id, chat_id, query_string = telepot.glance(
                        query["inline_query"], flavor="inline_query")
                    queries.append({
                        u'qtype': "inline_query",
                        u'qinterface': u'Telegram',
                        u'qcontent': query_string,
                        u'from': chat_id,
                        u'id': query_id,
                        u'extra': query
                    })
                last_read = int(query["update_id"]) + 1
        except (telepot.exception.BadHTTPResponse):
            if (debug):
                print(
                    "Error in telegram_interface.py, get_queries(): Bad HTTP Response",
                    flush=True)
        except Exception as e:
            if (debug):
                print("Error in telegram_interface.py, get_queries(): ",
                      e,
                      flush=True)
Exemple #10
0
def listen_loop(replies):
    global slave_list
    while (True):
        gurulhutils.sleep(1)
        for slave in slave_list:
            try:
                reply = gurulhutils.socket_recv(slave["socket"])
                if (reply["rtype"] == "warning"):
                    pass
                else:
                    replies.put(reply)
            except BlockingIOError:
                pass
            except Exception as e:
                if (debug):
                    print("Error in botnet_handler.py, listen_loop: ",
                          e,
                          flush=True)
                slave["socket"].close()
                slave_list.remove(slave)
Exemple #11
0
def route_loop(write_pipe, queries):
    while (True):
        gurulhutils.sleep(1)
        query = queries.get()
        if (query["qtype"] == "text" or query["qtype"] == "inline_query"):
            token = query["qcontent"].split(" ")
            print(module_list)
            if token[0] in module_list:
                schedule(query, token[0])
            elif (token[0] == "/botctrl"):
                write_pipe.send(query)
            elif (token[0].find("/") < 0):
                schedule(query, "chat")
            else:
                print(token[0])
                query.update({
                    "rtype":
                    "text",
                    "rcontent":
                    "Sorry, I have no active module to recognize this."
                })
                write_pipe.send(query)
Exemple #12
0
def digest_replies():
    global replies
    global server
    global addr

    while True:
        gurulhutils.sleep(1)
        for reply in replies:
            try:
                msg = MIMEMultipart()
                msg["From"] = addr
                msg["To"] = reply["to"]
                msg["Subject"] = "Re:" + reply["qcontent"]
                msg.attach(MIMEText(reply["rcontent"], "plain"))

                server.sendmail(addr, reply["to"], msg.as_string())

                replies.remove(reply)
            except Exception as e:
                if (debug):
                    print("Error in email_interface.py, digest_replies(): ",
                          e,
                          flush=True)
Exemple #13
0
            botnet_handler.update({"status": "failed"})
            return "fail", botnet_handler
    except Exception as e:
        if (debug):
            print("Error in main.py, botnet_handler_init: ", e, flush=True)
        return "fail", None


if __name__ == "__main__":
    keys = keys_get()
    queries = multiprocessing.Queue()
    replies = multiprocessing.Queue()

    print("Starting Database connection . . . ", end="", flush=True)
    status, database = gurulhutils.db_init(keys["Database"])
    gurulhutils.status_check(status)

    print("Starting Interface Handler . . . ", end="", flush=True)
    status, interface_handler = interface_handler_init(queries, replies, keys)
    gurulhutils.status_check(status)

    print("Starting BotNet Handler . . . ", end="", flush=True)
    status, botnet_handler = botnet_handler_init(replies, keys)
    gurulhutils.status_check(status)

    while (status != "down"):
        try:
            gurulhutils.sleep(1)
        except Exception as e:
            if (debug): print("Error in main.py, main: ", e, flush=True)