我正在尝试使用 SQL 数据库中的 netflow 数据创建 DoS 检测程序。到目前为止,我对如何实现这一点有了大致的了解,但需要一些指导。
到目前为止的程序流程是:
<?php
$run = 'true';
$servername = "10.1.80.50";
$username = "netflow";
$password = "********";
$dbname = "netflow";
$flowQuery = 'SELECT distinct TIMESTAMP, SRCADDR, DSTADDR, SRCPORT, DSTPORT, PROT FROM netflow.flows ORDER BY DSTADDR ASC';
// Query flows for unique timed flows
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$flows = mysqli_query($conn, $flowQuery);
// Begin creating baseline data
while ($run == 'true') {
while($row = $flows->fetch_assoc()) {
$baselineQuery = 'SELECT FROM_UNIXTIME((UNIX_TIMESTAMP(TIMESTAMP) div (5*60))*(5*60)+(5*60)) as timeIntervals,
ROUND(SUM(DPKTS),0) as PACKETDATA,
MIN(DPKTS) AS MINPACKETS,
MAX(DPKTS) AS MAXPACKETS,
ROUND(SUM(DOCTETS),0) as OCTETDATA,
MIN(DOCTETS) AS MINOCTETS,
MAX(DOCTETS) AS MAXOCTETS
FROM (
SELECT FLOW_ID,TIMESTAMP,EXADDR,SRCADDR,DSTADDR,SRCPORT,DSTPORT,DPKTS,DOCTETS
FROM netflow.flows WHERE SRCADDR = "'.$row["SRCADDR"].'" AND DSTADDR = "'.$row["DSTADDR"].'" AND DSTPORT = "'.$row["DSTPORT"].'" AND SRCPORT = "'.$row["SRCPORT"].'" AND PROT = "'.$row["PROT"].'"
ORDER BY FLOW_ID DESC LIMIT 500
) AS custom
GROUP BY 1 ORDER BY timeIntervals DESC
LIMIT 240';
$baselines = mysqli_query($conn, $baselineQuery);
}
usleep(60000000);
}
print_r($baselines);
?>
我的逻辑是:
- 查找过去 8 小时内的所有唯一流。
- 使用此数据查找这些唯一流中的每一个的所有流数据。
- 将创建的数组拆分为每个流的子数组或单独的数组。
- 一旦每个流都有自己的数组,平均数据包和数据以获得伪基线。
- 将最新的流量条目与这些平均值进行比较,并确定是否存在异常(一个方向的数据流增加/减少超过 200%)。
- 如果检测到异常,请发送一封有关该事件的电子邮件警报。
- 等待 60 秒并恢复循环。
我知道代码远未完成,可能非常草率,但非常感谢任何帮助!