-
Notifications
You must be signed in to change notification settings - Fork 4
/
scan.py
207 lines (184 loc) · 7.94 KB
/
scan.py
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
# 06/23/2019 - Adding new feature to Scan uploaded files against set of Yara signatures uploaded on s3 bucket.
#author: Abhinav Singh
#pre-update
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import boto3
import clamav
import yarascan
import copy
import json
import fsf_client
from urllib.parse import unquote_plus
from common import *
from datetime import datetime
from distutils.util import strtobool
import sys
import os
from botocore.vendored import requests
ENV = os.getenv("ENV", "")
EVENT_SOURCE = os.getenv("EVENT_SOURCE", "S3")
def event_object(event):
if EVENT_SOURCE.upper() == "SNS":
event = json.loads(event['Records'][0]['Sns']['Message'])
bucket = event['Records'][0]['s3']['bucket']['name']
key = unquote_plus(event['Records'][0]['s3']['object']['key'])
if (not bucket) or (not key):
print("Unable to retrieve object from event.\n%s" % event)
raise Exception("Unable to retrieve object from event.")
return s3.Object(bucket, key)
def verify_s3_object_version(s3_object):
# validate that we only process the original version of a file, if asked to do so
# security check to disallow processing of a new (possibly infected) object version
# while a clean initial version is getting processed
# downstream services may consume latest version by mistake and get the infected version instead
if str_to_bool(AV_PROCESS_ORIGINAL_VERSION_ONLY):
bucketVersioning = s3.BucketVersioning(s3_object.bucket_name)
if bucketVersioning.status == "Enabled":
bucket = s3.Bucket(s3_object.bucket_name)
versions = list(bucket.object_versions.filter(Prefix=s3_object.key))
if len(versions) > 1:
print("Detected multiple object versions in %s.%s, aborting processing" % (
s3_object.bucket_name, s3_object.key))
raise Exception("Detected multiple object versions in %s.%s, aborting processing" % (
s3_object.bucket_name, s3_object.key))
else:
print("Detected only 1 object version in %s.%s, proceeding with processing" % (
s3_object.bucket_name, s3_object.key))
else:
# misconfigured bucket, left with no or suspended versioning
print(
"Unable to implement check for original version, as versioning is not enabled in bucket %s" % s3_object.bucket_name)
raise Exception("Object versioning is not enabled in bucket %s" % s3_object.bucket_name)
def download_s3_object(s3_object, local_prefix):
local_path = "%s/%s/%s" % (local_prefix, s3_object.bucket_name, s3_object.key)
create_dir(os.path.dirname(local_path))
s3_object.download_file(local_path)
return local_path
def delete_s3_object(s3_object):
try:
s3_object.delete()
except:
print("Failed to delete infected file: %s.%s" % (s3_object.bucket_name, s3_object.key))
else:
print("Infected file deleted: %s.%s" % (s3_object.bucket_name, s3_object.key))
def set_av_metadata(s3_object, result):
content_type = s3_object.content_type
metadata = s3_object.metadata
metadata[AV_STATUS_METADATA] = result
metadata[AV_TIMESTAMP_METADATA] = datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S UTC")
s3_object.copy(
{
'Bucket': s3_object.bucket_name,
'Key': s3_object.key
},
ExtraArgs={
"ContentType": content_type,
"Metadata": metadata,
"MetadataDirective": "REPLACE"
}
)
def set_av_tags(s3_object, result):
curr_tags = s3_client.get_object_tagging(Bucket=s3_object.bucket_name, Key=s3_object.key)["TagSet"]
new_tags = copy.copy(curr_tags)
for tag in curr_tags:
if tag["Key"] in [AV_STATUS_METADATA, AV_TIMESTAMP_METADATA]:
new_tags.remove(tag)
new_tags.append({"Key": AV_STATUS_METADATA, "Value": result})
new_tags.append({"Key": AV_TIMESTAMP_METADATA, "Value": datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S UTC")})
s3_client.put_object_tagging(
Bucket=s3_object.bucket_name,
Key=s3_object.key,
Tagging={"TagSet": new_tags}
)
def sns_start_scan(s3_object):
if AV_SCAN_START_SNS_ARN is None:
return
message = {
"bucket": s3_object.bucket_name,
"key": s3_object.key,
"version": s3_object.version_id,
AV_SCAN_START_METADATA: True,
AV_TIMESTAMP_METADATA: datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S UTC")
}
sns_client = boto3.client("sns")
sns_client.publish(
TargetArn=AV_SCAN_START_SNS_ARN,
Message=json.dumps({'default': json.dumps(message)}),
MessageStructure="json"
)
def sns_scan_results(s3_object, result):
if AV_STATUS_SNS_ARN is None:
return
message = {
"bucket": s3_object.bucket_name,
"key": s3_object.key,
"version": s3_object.version_id,
AV_STATUS_METADATA: result,
AV_TIMESTAMP_METADATA: datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S UTC")
}
sns_client = boto3.client("sns")
sns_client.publish(
TargetArn=AV_STATUS_SNS_ARN,
Message=json.dumps({'default': json.dumps(message)}),
MessageStructure="json",
MessageAttributes={
AV_STATUS_METADATA: {
'DataType': 'String',
'StringValue': result
}
}
)
def slack_notification(result):
webhook_url = 'https://hooks.slack.com/services/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'
response = requests.post(webhook_url, json={'text': result})
http_reply = {"statusCode": 200, "body": response.text}
return http_reply
def lambda_handler(event, context):
start_time = datetime.utcnow()
print("Script starting at %s\n" %
(start_time.strftime("%Y/%m/%d %H:%M:%S UTC")))
s3_object = event_object(event)
verify_s3_object_version(s3_object)
sns_start_scan(s3_object)
file_path = download_s3_object(s3_object, "/tmp")
clamav.update_defs_from_s3(AV_DEFINITION_S3_BUCKET, AV_DEFINITION_S3_PREFIX)
scan_result = clamav.scan_file(file_path)
slack_notification(scan_result)
print("yara scanning to begin")
yarascan.update_sigs_from_s3(YARA_RULES_S3_BUCKET, YARA_RULES_S3_PREFIX)
scan_result_yara = yarascan.scan_file(file_path)
print(scan_result_yara)
lambda_result = {"clamav": "Detected", "yara": "Dummrule1.yara"}
with open(file_path, 'rb') as f:
filename = os.path.basename(file_path)
print("sending control to fsf")
fsf = fsf_client.FSFClient(file_path, f.name, False, 'Analyst', False, False, False, f.read(), lambda_result)
print("initiating submission")
print(fsf.initiate_submission())
print("Scan of s3://%s resulted in %s\n" % (os.path.join(s3_object.bucket_name, s3_object.key), scan_result))
if "AV_UPDATE_METADATA" in os.environ:
set_av_metadata(s3_object, scan_result)
set_av_tags(s3_object, scan_result)
sns_scan_results(s3_object, scan_result)
#metrics.send(env=ENV, bucket=s3_object.bucket_name, key=s3_object.key, status=scan_result)
# Delete downloaded file to free up room on re-usable lambda function container
try:
os.remove(file_path)
except OSError:
pass
if str_to_bool(AV_DELETE_INFECTED_FILES) and scan_result == AV_STATUS_INFECTED:
delete_s3_object(s3_object)
print("Script finished at %s\n" %
datetime.utcnow().strftime("%Y/%m/%d %H:%M:%S UTC"))
def str_to_bool(s):
return bool(strtobool(str(s)))