forked from erjosito/azcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowlogs.kql
150 lines (142 loc) · 5.51 KB
/
flowlogs.kql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Create table to receive raw logs from Azure Storage (over Event Grid)
.create table ['rawFlowLogs'] (['records']:dynamic, ['EventProcessedUtcTime']:datetime, ['PartitionId']:int, ['EventEnqueuedUtcTime']:datetime)
/////////////////////
// VNet Flow Logs //
/////////////////////
// Create Mapping function (VNet Flow Logs)
.create-or-alter function
with (docstring = 'Parses raw flowlogs records into strongly-typed columns', folder = 'FlowLogs')
FlowLogMapping() {
rawFlowLogs
| mv-expand records = records
| project Time = todatetime(records["time"]),
macAddress = tostring(records["macAddress"]),
category = tostring(records["category"]),
flowLogVersion = toint(records["flowLogVersion"]),
flowLogGUID = tostring(records["flowLogGUID"]),
flowLogResourceID = tostring(records["flowLogGUID"]),
targetResourceID = tostring(records["targetResourceID"]),
vnetResourceID = tostring(records["vnetResourceID"]),
nicResourceID = tostring(records["nicResourceID"]),
vmResourceID = tostring(records["vmResrouceID"]),
location = tostring(records["location"]),
zone = tostring(records["zone"]),
flowRecords = todynamic(records["flowRecords"])
| mv-expand flowRecords
| extend fields = split(flowRecords, ',')
| extend Timestamp = tostring(fields[0]),
SourceIP = tostring(fields[1]),
DestinationIP = tostring(fields[2]),
SrcPort = tostring(fields[3]),
DstPort = tostring(fields[4]),
L4Protocol = tostring(fields[5]),
Direction = tostring(fields[6]),
State = tostring(fields[7]),
Encryption = tostring(fields[8]),
PacketsSrcToDst = toint(fields[9]),
BytesSrcToDst = toint(fields[10]),
PacketDstToSrc = toint(fields[11]),
BytesDstToSrc = toint(fields[12])
| project-away flowRecords
}
// Create target table for VNet Flow Logs
.create table flowLogs (
Time:datetime,
macAddress:string,
category:string,
flowLogVersion:int,
flowLogGUID:string,
flowLogResourceID:string,
targetResourceID:string,
vnetResourceID:string,
vmResourceID:string,
nicResourceID:string,
location:string,
zone:string,
flowRecords:dynamic,
Timestamp:string,
SourceIP:string,
DestinationIP:string,
SrcPort:string,
DstPort:string,
L4Protocol:string,
Direction:string,
State:string,
Encryption:string,
PacketsSrcToDst:int,
BytesSrcToDst:int,
PacketsDstToSrc:int,
BytesDstToSrc:int
)
// Update policy for rawFlowLogs
.alter table flowLogs policy update
@'[{ "IsEnabled": true, "Source": "rawFlowLogs", "Query": "FlowLogMapping()", "IsTransactional": false, "PropagateIngestionProperties": false}]'
////////////////////
// NSG Flow Logs //
////////////////////
// Create Mapping function (NSG Flow Logs)
.create-or-alter function
with (docstring = 'Parses raw flowlogs records into strongly-typed columns', folder = 'FlowLogs')
FlowLogMapping() {
rawFlowLogs
| mv-expand records = records
| take 1
| project Time = todatetime(records["time"]),
macAddress = tostring(records["macAddress"]),
category = tostring(records["category"]),
flowLogVersion = toint(records["properties"]["Version"]),
nsgResourceID = tostring(records["resourceId"]),
flows = todynamic(records["properties"]["flows"])
| mv-expand flows
| extend rule = tostring(flows["rule"])
| extend flowsFlows = todynamic(flows["flows"])
| mv-expand flowsFlows
| extend macAddress2=tostring(flowsFlows["mac"]),
flowsFlowsTuples = todynamic(flowsFlows["flowTuples"])
| mv-expand flowsFlowsTuples
| extend fields = split(flowsFlowsTuples, ',')
| extend Timestamp = tostring(fields[0]),
srcIP = tostring(fields[1]),
dstIP = tostring(fields[2]),
srcPort = tostring(fields[3]),
dstPort = tostring(fields[4]),
Protocol = tostring(fields[5]),
Direction = tostring(fields[6]),
Decision = tostring(fields[7]),
State = tostring(fields[8]),
PacketsSrcToDst = toint(fields[9]),
BytesSrcToDst = toint(fields[10]),
PacketDstToSrc = toint(fields[11]),
BytesDstToSrc = toint(fields[12])
| project-away flows, flowsFlows, flowsFlowsTuples,fields
}
// Create target table for NSG Flow Logs
.drop table flowLogs
.create table flowLogs (
Time:datetime,
macAddress:string,
category:string,
flowLogVersion:int,
nsgResourceID:string,
flows:dynamic,
rule:string,
flowsFlows:dynamic,
macAddress2:string,
flowsFlowsTuples:dynamic,
Timestamp:string,
srcIP:string,
dstIP:string,
srcPort:string,
dstPort:string,
Protocol:string,
Direction:string,
Decision:string,
State:string,
PacketsSrcToDst:int,
BytesSrcToDst:int,
PacketsDstToSrc:int,
BytesDstToSrc:int
)
// Update policy for rawFlowLogs
.alter table flowLogs policy update
@'[{ "IsEnabled": true, "Source": "rawFlowLogs", "Query": "FlowLogMapping()", "IsTransactional": false, "PropagateIngestionProperties": false}]'