如何使用 Amazon Athena 分析 Amazon VPC 流日志?

3 分钟阅读
0

我想使用 Amazon Athena 分析 Amazon Virtual Private Cloud(Amazon VPC)流日志。

简短描述

Amazon VPC 流日志允许您捕获有关进出 VPC 中网络接口的 IP 流量的信息。这些日志可用于调查网络流量模式以及识别 Amazon VPC 网络中的威胁和风险。

解决方法

使用 Athena 分析 VPC 日志

要使用 Amazon Athena 分析访问日志,请执行以下操作:

1.    在 Amazon Athena console query editor (Amazon Athena 控制台查询编辑器) 选项卡上,通过运行类似于以下内容的命令来创建数据库 test_db_vpclogs
**重要提示:**最好在要用于保存流日志的 Amazon S3 存储桶所在的 AWS 区域中创建数据库。

CREATE DATABASE test_db_vpclogs;

**注意:**请务必将 test_db_vpclogs 替换为要创建的数据库的名称。

2.    在创建的数据库中,通过运行类似于以下内容的命令为 VPC 流日志创建表。使用此命令,您可以创建表、对该表进行分区,并根据您的使用场景使用分区投影自动填充分区。

CREATE EXTERNAL TABLE IF NOT EXISTS test_table_vpclogs (
version int,
account string,
interfaceid string,
sourceaddress string,
destinationaddress string,
sourceport int,
destinationport int,
protocol int,
numpackets int,
numbytes bigint,
starttime int,
endtime int,
action string,
logstatus string,
vpcid string,
subnetid string,
instanceid string,
tcpflags int,
type string,
pktsrcaddr string,
pktdstaddr string,
aws_region string,
azid string,
sublocationtype string,
sublocationid string,
pktsrcawsservice string,
pktdstawsservice string,
flowdirection string,
trafficpath string
)
PARTITIONED BY (region string, day string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ' '
LOCATION 's3://awsexamplebucket/awsexampleprefix/awsexamplelogs/1111222233334444/vpcflowlogs/'
TBLPROPERTIES
(
"skip.header.line.count"="1",
"projection.enabled" = "true",
"projection.region.type" = "enum",
"projection.region.values" = "us-east-1,us-west-2,ap-south-1,eu-west-1",
"projection.day.type" = "date",
"projection.day.range" = "2021/01/01,NOW",
"projection.day.format" = "yyyy/MM/dd",
"storage.location.template" = "s3://awsexamplebucket/awsexampleprefix/awsexamplelogs/1111222233334444/vpcflowlogs/${region}/${day}"
)

确保执行以下操作:

  • 将查询中的 test_table_vpclogs 替换为表的名称。
  • 修改查询中的 LOCATION 参数,以指向包含日志数据的 Amazon S3 存储桶。

**注意:**如果 Amazon S3 中不存在投影分区,则 Athena 仍会投影该分区。最好在查询中使用分区属性。

3.    使用控制台中的查询编辑器,对表运行 SQL 语句。您可以保存查询、查看以前的查询或下载 CSV 格式的查询结果。

示例查询

**注意:**将查询中的 test_table_vpclogs 替换为所创建的表的名称。修改表列值和其他变量,以适合您的查询。

1.    要查看某一特定时间段内按时间顺序显示的前 100 个访问日志条目,请运行类似于以下内容的查询:

SELECT * FROM test_table_vpclogs WHERE day >= '2021/02/01' AND day < '2021/02/28' ORDER BY time ASC LIMIT 100;

2.    要查看哪台服务器在某一特定时间段内接收到前十个 HTTP 数据包,请运行类似于以下内容的查询:

SELECT SUM(numpackets) AS
  packetcount,
  destinationaddress
FROM test_table_vpclogs
WHERE destinationport = 443 AND day >= '2021/03/01' AND day < '2021/03/31'
GROUP BY destinationaddress
ORDER BY packetcount DESC
LIMIT 10;

此查询计算在 HTTPS 端口 443 上收到的数据包数量,按目标 IP 地址对它们进行分组。然后,它返回前一周的前 10 个条目。

3.    要查看在某一特定时间段内创建的日志,请运行类似于以下内容的查询:

SELECT interfaceid, sourceaddress, action, protocol, to_iso8601(from_unixtime(starttime))
AS start_time, to_iso8601(from_unixtime(endtime))
AS end_time
FROM test_table_vpclogs
WHERE day >= '2021/04/01' AND day < '2021/04/30';

此查询将返回从 2020-12-04 11:28:19.000 到 2020-12-04 11:28:33.000 这一时间段内创建的日志。

4.    要查看某一特定时间段内特定源 IP 地址的访问日志,请运行类似于以下内容的查询:

SELECT * FROM test_table_vpclogs WHERE sourceaddress= '10.117.1.22' AND day >= '2021/02/01' AND day < '2021/02/28';

5.    要列出拒绝的 TCP 连接,请运行类似于以下内容的查询:

SELECT day_of_week(date) AS
day,
date,
interfaceid,
sourceaddress,
action,
protocol
FROM test_table_vpclogs
WHERE action = 'REJECT' AND protocol = 6 AND day >= '2021/02/01' AND day < '2021/02/28' LIMIT 10;

6.    要查看以“10.117”开头的 IP 地址范围的访问日志,请运行类似于以下内容的查询:

SELECT * FROM test_table_vpclogs WHERE split_part(sourceaddress,’.’, 1)=’10’ AND split_part(sourceaddress,’.’, 2) =‘117’;

7.    要查看某一特定时间范围内特定目标 IP 地址的访问日志,请运行类似于以下内容的查询:

SELECT * FROM test_table_vpclogs WHERE destinationaddress= '10.0.1.14' AND day >= '2021/01/01' AND day < '2021/01/31';

相关信息

如何使用流日志监控 VPC 中的流量?

使用 Amazon Athena 和 Amazon QuickSight 分析 VPC 流日志

AWS 官方
AWS 官方已更新 2 年前