-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathazure-nsg-flow-oms-upload.ps1
216 lines (187 loc) · 8.71 KB
/
azure-nsg-flow-oms-upload.ps1
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<#
azure-nsg-flow-oms-upload.ps1
Jason Boeshart, Cloud Solution Architect, Microsoft
Apache 2.0 License
.SYNOPSIS
Uploads NSG Flow Logs to an OMS workspace
.DESCRIPTION
This script uploads NSG Flow Logs to an OMS workspace. Based on code found
at https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-data-collector-api.
.PARAMETER OmsWorkspaceId
The OMS workspace ID as found in the OMS portal
.PARAMETER OmsSharedKey
The OMS Shared Key corresponding to the workspace ID
.PARAMETER OmsLogType
The record type that you'll be uploading to
.PARAMETER StorageAccountName
Name of the storage account containing NSG flow logs
.PARAMETER StorageAccountKey
Storage account key for the storage account with flow logs
.PARAMETER ContainerName
Name of the container in the storage account containing NSG flow logs, optional
and set to the standard default container created by Azure Network Watcher
.LINK
https://github.com/jboeshart/azure-nsg-flow-oms-upload
https://blogs.msdn.microsoft.com/cloud_solution_architect/2017/04/03/uploading-azure-nsg-flow-logs-to-oms/
#>
Param(
[Parameter(Mandatory=$True)]
[String] $OmsWorkspaceId,
[Parameter(Mandatory=$True)]
[String] $OmsSharedKey,
[Parameter(Mandatory=$True)]
[String] $OmsLogType,
[Parameter(Mandatory=$True)]
[String] $StorageAccountName,
[Parameter(Mandatory=$True, ParameterSetName="StorageAccountKey")]
[String] $StorageAccountKey,
[Parameter(Mandatory=$True, ParameterSetName="SasToken")]
[String] $SasToken,
[String] $ContainerName = "insights-logs-networksecuritygroupflowevent"
)
# Field with the created time for the records
$TimeStampField = "DateTime"
# Set epoch time for date calculations
$epoch = get-date "1/1/1970"
# Function to create the authorization signature
Function Build-Signature ($customerId, $sharedKey, $date, $contentLength, $method, $contentType, $resource)
{
$xHeaders = "x-ms-date:" + $date
$stringToHash = $method + "`n" + $contentLength + "`n" + $contentType + "`n" + $xHeaders + "`n" + $resource
$bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash)
$keyBytes = [Convert]::FromBase64String($sharedKey)
$sha256 = New-Object System.Security.Cryptography.HMACSHA256
$sha256.Key = $keyBytes
$calculatedHash = $sha256.ComputeHash($bytesToHash)
$encodedHash = [Convert]::ToBase64String($calculatedHash)
$authorization = 'SharedKey {0}:{1}' -f $customerId,$encodedHash
return $authorization
}
# Function to create and post the request
Function Post-OMSData($customerId, $sharedKey, $body, $logType)
{
$method = "POST"
$contentType = "application/json"
$resource = "/api/logs"
$rfc1123date = [DateTime]::UtcNow.ToString("r")
$contentLength = $body.Length
$signature = Build-Signature `
-customerId $customerId `
-sharedKey $sharedKey `
-date $rfc1123date `
-contentLength $contentLength `
-fileName $fileName `
-method $method `
-contentType $contentType `
-resource $resource
$uri = "https://" + $customerId + ".ods.opinsights.azure.com" + $resource + "?api-version=2016-04-01"
$headers = @{
"Authorization" = $signature;
"Log-Type" = $logType;
"x-ms-date" = $rfc1123date;
"time-generated-field" = $TimeStampField;
}
$response = Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Headers $headers -Body $body -UseBasicParsing
return $response.StatusCode
}
# Function to parse through the flow log and upload to OMS, function accepts a standard object
Function Submit-FlowData($nsgflowobject)
{
$uploadErrors = 0
foreach ($record in $nsgflowobject.records) {
$time = $record.time
$resourceId = $record.resourceId
$splitresourceId = $resourceId.Split("/")
$sub = $splitresourceId[2]
$rg = $splitresourceId[4]
$nsg = $splitresourceId[8]
foreach ($property in $record.properties) {
foreach ($flows in $property.flows) {
$rule = $flows.rule
foreach ($flows2 in $flows.flows) {
$mac = $flows2.mac
foreach ($flowTuples in $flows2.flowTuples) {
$splitflowTuples = $flowTuples.Split(",")
$dt = $epoch.AddSeconds($splitflowTuples[0]).ToUniversalTime().ToString()
$jsonObj = @{
SubscriptionId = $sub
ResourceGroup = $rg
NSG = $nsg
Rule = $rule
MAC = $mac
DateTime = $dt
SourceIp = $splitflowTuples[1]
DestinationIp = $splitflowTuples[2]
SourcePort = $splitflowTuples[3]
DestinationPort = $splitflowTuples[4]
TcpOrUdp = $splitflowTuples[5]
InOrOut = $splitflowTuples[6]
AllowOrDeny = $splitflowTuples[7]
}
$json = ConvertTo-Json $jsonObj
Write-Output "Submitting: " $json
$returnCode = Post-OMSData -customerId $OmsWorkspaceId -sharedKey $OmsSharedKey -body ([System.Text.Encoding]::UTF8.GetBytes($json)) -logType $OmsLogType
Write-Output "Return code: $($returnCode)"
if ($returnCode -ne "200") {
$uploadErrors++
}
}
}
}
}
}
if ($uploadErrors -eq 0){
# All uploads were successful, update file metadata so we don't re-upload on subsequent script runs
Write-Output "Return code 200 on all uploads, updating file metadata"
$CloudBlockBlob = [Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob] $Blob.ICloudBlob
$CloudBlockBlob.Metadata["OmsLogType"] = $OmsLogType
$CloudBlockBlob.SetMetadata()
}
else {
# Had one or more errors during upload, log an error and move on
Write-Output "One or more uploads had errors, please retry upload for file $($blob.Name)"
}
}
# Loop through the storage and check the files
If ($StorageAccountKey) {
$storageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $StorageAccountKey
}
elseif ($SasToken) {
$storageContext = New-AzureStorageContext -StorageAccountName $StorageAccountName -SasToken $SasToken
}
# Get all the blobs in the container.
$blobs = Get-AzureStorageBlob -Container $ContainerName -Context $storageContext
foreach ($blob in $blobs) {
# Check to see if the year, month, day, and hour of the flow log (as defined in the blob name) match the current universal time values
# If they match, it's the current file and we're going to leave it alone
if ( -not (`
(((Get-Date).ToUniversalTime() | Get-Date -UFormat %Y) -eq ($blob.Name.Split("/")[9].Split("=")[1])) -and `
(((Get-Date).ToUniversalTime() | Get-Date -UFormat %m) -eq ($blob.Name.Split("/")[10].Split("=")[1])) -and `
(((Get-Date).ToUniversalTime() | Get-Date -UFormat %d) -eq ($blob.Name.Split("/")[11].Split("=")[1])) -and `
(((Get-Date).ToUniversalTime() | Get-Date -UFormat %H) -eq ($blob.Name.Split("/")[12].Split("=")[1]))))
{
# Check for metadata to see if it's already been uploaded to OMS
if ($blob.ICloudBlob.Metadata.OmsLogType -ne $OmsLogType) {
Write-Output "Processing file: $($blob.Name)"
if ($StorageAccountKey) {
# Download the file content locally, have to do this as there's currently no way to stick it straight into a variable with just the storage key
Get-AzureStorageBlobContent -Container $ContainerName -Context $storageContext -Blob $blob.Name -Force -Destination .
# Convert blob content from JSON to a standard object
$blobcontent = Get-Content -Raw -Path $blob.Name | ConvertFrom-Json
# Call function to process file and upload to OMS
Submit-FlowData($blobcontent)
# Remove the temp file
Remove-Item $blob.Name
}
elseif ($SasToken) {
# Download the blob content via HTTPS directly to a variable, we can do this because we have SAS token
$blobcontent = Invoke-RestMethod -Uri $($blob.ICloudBlob.Uri.ToString() + $SasToken)
# Call function to process file and upload to OMS
Submit-FlowData($blobcontent)
}
}
}
else {
Write-Output "Not processing current file: $($blob.Name)"
}
}