示例#1
0
文件: scores.py 项目: zhshua/IPC
def scores(mydb, path, fx_startid, time_startid, time_len):
    mycursor = mydb.cursor()
    conf = conf_read()
    sqlcom = 'select * from 分析表 limit ' + str(fx_startid - 1) + ",1"
    df = pd.read_sql(sqlcom, con=mydb)

    if time_len != 0:
        sqlcom1 = 'select * from 非法操作记录表 limit ' + str(time_startid - 1) + "," + str(time_len + 1)
        df1 = pd.read_sql(sqlcom1, con=mydb)
        time = df1.shape[0]
    else:
        time = 0
    flow = int(df.iloc[0, 0])
    speed = int(float(df.iloc[0, 1]))
    tcp = int(df.iloc[0, 2])
    login = int(df.iloc[0, 3])
    fail = int(df.iloc[0, 4])
    ports = int(df.iloc[0, 5])
    if (is_Chinese(df.iloc[0, 8]) == False):
        whi = int(float(df.iloc[0, 8]))
        min_flow = int(conf.get_flow())
        min_speed = int(conf.get_speed())
        min_tcp = int(conf.get_tcp())
        min_login = int(conf.get_login())
        min_fail = int(conf.get_fail())
        min_ports = int(conf.get_ports())

        dos_score = (((min_flow - flow) / (min_flow + flow) + ((min_speed - speed) / (min_speed + speed)) + (
                (min_tcp - tcp) / (min_tcp + tcp)))) / 3 * 100
        ports_score = ((min_ports - ports) / min_ports) * 100
        login_score = ((min_login - login) / min_login + (min_fail - fail) / min_fail) / 2 * 100
        time_score = ((- 1) / (time + 1)) * 100
        white_score = (whi * 2 - 1) * 600
        total_score = int((dos_score + ports_score + login_score + time_score + white_score) / 10)

        if total_score <= -50:
            result = "网络状态糟糕"
        if -50 < total_score <= 0:
            result = "网络状态较差"
        if 0 < total_score <= 50:
            result = "网络状态一般"
        if 50 < total_score <= 100:
            result = "网络状态良好"
    else:
        total_score = 100
        result = '状态良好'

    # print(int(dos_score),int(ports_score),int(login_score),int(time_score),int(white_score),int(total_score))

    # sql = "INSERT INTO 评分表 (得分,状态,来源) VALUES (%s,%s,%s)"
    sql = "UPDATE 分析表 SET 得分=%s,状态=%s WHERE id=%s"
    # print(sql)
    val = (str(total_score), str(result), str(fx_startid))
    mycursor.execute(sql, val)
    mydb.commit()  # 写入数据库
示例#2
0
文件: svmhandle.py 项目: zhshua/IPC
def remix():
    fdata = open("svmdata.txt", "w")
    conf = conf_read()
    for i in range(1000000):  # 产生百万个样本集,由于特征量及标签过多,需要的大量的样本集才能保证模型精度,
        # 后续SVM训练模型花了10+小时,不过已经保存好模型

        x = 0

        flow = random.randrange(0, 100000)  # 流量
        login = random.randrange(0, 10)  # 登录次数
        fail = random.randrange(0, login + 1)  # 登录失败次数
        ports = random.randrange(0, 15)  # 访问端口次数
        tcp = random.randrange(3, 15)  # tcp连接数
        start = random.randrange(0, 86399)  # 起始时间(第一个数据包)
        end = random.randrange(start + 1, 86400)  # 截止时间(最后一个数据包)
        speed = math.ceil(flow / (end - start))  # 计算流量速度

        min_flow = int(conf.get_flow())
        min_speed = int(conf.get_speed())
        min_tcp = int(conf.get_tcp())
        min_login = int(conf.get_login())
        min_fail = int(conf.get_fail())
        min_ports = int(conf.get_ports())
        min_start = conf.get_start_time()
        min_end = conf.get_end_time()
        if (min_flow < flow) | (min_speed < speed) | (min_tcp <
                                                      tcp):  # 打标签,采用四位二进制小数,
            # 从高到低:dos,远程登录,端口扫描,非法时间登录
            x = x + 8
        if (min_login < login) & (min_fail < fail):
            x = x + 4
        if min_ports < ports:
            x = x + 2
        if (start <
            (datetime.datetime.strptime(min_start, '%H:%M:%S') - datetime.datetime(1970, 1, 1)).seconds) \
                | ((datetime.datetime.strptime(min_end, '%H:%M:%S') - datetime.datetime(1970, 1, 1)).seconds
                   < end):
            x = x + 1

        fdata.writelines(
            str(flow) + "," + str(speed) + "," + str(login) + "," + str(fail) +
            "," + str(ports) + "," + str(tcp) + "," + str(start) + "," +
            str(end) + "," + str(x) + "\n")  # 保存样本集

    fdata.close()
    print("样本集生成成功")
示例#3
0
    def drop_login(self):
        sql = "DROP TABLE IF EXISTS 远程登录记录表"
        self.mycursor.execute(sql)

    def drop_time(self):
        sql = "DROP TABLE IF EXISTS 非法操作记录表"
        self.mycursor.execute(sql)

    def drop_white(self):
        sql = "DROP TABLE IF EXISTS 白名单"
        self.mycursor.execute(sql)


if __name__ == "__main__":
    conf = conf_read()

    mydb = mysql.connector.connect(host=conf.get_host(),
                                   user=conf.get_user(),
                                   passwd=conf.get_passwd(),
                                   database=conf.get_database(),
                                   use_unicode=True,
                                   charset="utf8")  # 登录数据库
    print("数据库登录成功")

    ta = table()  # 实例化表类
    ta.login(mydb)  # 登录

    ta.drop_jx()  # 清空
    ta.drop_fx()
    ta.drop_dos()
示例#4
0
            print(f"Fan manual {manual}")
        if manual == "0":
            write = True
        else:
            write = False
    if write:
        with open(f"{path}fan1_manual", "w") as f:
            f.write("1")
    else:
        if DEBUG:
            print("All good!")
    exec(gend)
    return 0


gend = conf_read()

# CLI options
if len(sys.argv) == 1:
    pass
else:
    if "h" in sys.argv[1]:
        print(
            "You have to put options together (ex. -dc).\n\n-d\t\t\tSet debug flag\n-c\t\t\tSet config file (ex. tem.py -c [configfile])\n-h\t\t\tThis help menu"
        )
        exit()
    if "g" in sys.argv[1]:
        print("Generating config (still being worked on)")
        conf_write()
        exit(0)
    if "d" in sys.argv[1]: