-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloud_scanner.py
419 lines (356 loc) · 16.2 KB
/
cloud_scanner.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#!/usr/bin/env python3
"""
Cloud Infrastructure Security Scanner Module
Part of AI-Powered Vulnerability Scanner
Supports AWS, Azure, and GCP
"""
import os
import json
import logging
import asyncio
from typing import Dict, List, Optional, Union
from dataclasses import dataclass
import boto3
from azure.identity import DefaultAzureCredential
from azure.mgmt.security import SecurityCenter
from azure.mgmt.subscription import SubscriptionClient
from google.cloud import security_center_v1
from google.cloud import asset_v1
from concurrent.futures import ThreadPoolExecutor
logger = logging.getLogger(__name__)
@dataclass
class CloudResource:
provider: str # aws, azure, or gcp
resource_type: str
resource_id: str
name: str
region: str
tags: Dict
configuration: Dict
@dataclass
class CloudVulnerability:
provider: str
resource_id: str
severity: str
description: str
recommendation: str
compliance_standards: List[str]
risk_score: float
@dataclass
class CloudScanResult:
provider: str
resources: List[CloudResource]
vulnerabilities: List[CloudVulnerability]
misconfigurations: List[Dict]
compliance_status: Dict
iam_issues: List[Dict]
network_findings: List[Dict]
encryption_status: Dict
scan_timestamp: str
class CloudScanner:
def __init__(self):
"""Initialize cloud scanner with necessary clients."""
self.aws_enabled = False
self.azure_enabled = False
self.gcp_enabled = False
self.initialize_clients()
def initialize_clients(self):
"""Initialize cloud provider clients."""
try:
# AWS
self.aws_session = boto3.Session()
self.aws_enabled = True
logger.info("AWS client initialized successfully")
except Exception as e:
logger.warning(f"Failed to initialize AWS client: {str(e)}")
try:
# Azure
self.azure_credential = DefaultAzureCredential()
self.subscription_client = SubscriptionClient(self.azure_credential)
self.azure_enabled = True
logger.info("Azure client initialized successfully")
except Exception as e:
logger.warning(f"Failed to initialize Azure client: {str(e)}")
try:
# GCP
self.gcp_security_client = security_center_v1.SecurityCenterClient()
self.gcp_asset_client = asset_v1.AssetServiceClient()
self.gcp_enabled = True
logger.info("GCP client initialized successfully")
except Exception as e:
logger.warning(f"Failed to initialize GCP client: {str(e)}")
async def scan_cloud_infrastructure(self, providers: List[str] = None) -> Dict[str, CloudScanResult]:
"""
Scan cloud infrastructure across specified providers.
Args:
providers: List of cloud providers to scan ('aws', 'azure', 'gcp')
Returns:
Dict of scan results per provider
"""
if not providers:
providers = ['aws', 'azure', 'gcp']
results = {}
tasks = []
for provider in providers:
if provider == 'aws' and self.aws_enabled:
tasks.append(self.scan_aws_infrastructure())
elif provider == 'azure' and self.azure_enabled:
tasks.append(self.scan_azure_infrastructure())
elif provider == 'gcp' and self.gcp_enabled:
tasks.append(self.scan_gcp_infrastructure())
# Run scans concurrently
scan_results = await asyncio.gather(*tasks, return_exceptions=True)
# Process results
for provider, result in zip(providers, scan_results):
if isinstance(result, Exception):
logger.error(f"Error scanning {provider}: {str(result)}")
continue
results[provider] = result
return results
async def scan_aws_infrastructure(self) -> CloudScanResult:
"""Scan AWS infrastructure for security issues."""
try:
print("Scanning AWS infrastructure...")
# Initialize AWS clients
securityhub = self.aws_session.client('securityhub')
inspector = self.aws_session.client('inspector2')
config = self.aws_session.client('config')
# Gather resources and findings concurrently
resources, findings, configs = await asyncio.gather(
self._get_aws_resources(),
self._get_aws_security_findings(securityhub),
self._get_aws_config_findings(config)
)
# Process findings into vulnerabilities
vulnerabilities = []
for finding in findings:
vulnerabilities.append(CloudVulnerability(
provider='aws',
resource_id=finding.get('Resources', [{}])[0].get('Id', ''),
severity=finding.get('Severity', {}).get('Label', 'UNKNOWN'),
description=finding.get('Description', ''),
recommendation=finding.get('Remediation', {}).get('Recommendation', {}).get('Text', ''),
compliance_standards=finding.get('Compliance', {}).get('SecurityControlId', []),
risk_score=float(finding.get('Severity', {}).get('Score', 0))
))
# Get IAM issues
iam_issues = await self._check_aws_iam_issues()
# Get network findings
network_findings = await self._check_aws_network_security()
# Check encryption status
encryption_status = await self._check_aws_encryption()
return CloudScanResult(
provider='aws',
resources=resources,
vulnerabilities=vulnerabilities,
misconfigurations=configs,
compliance_status=await self._get_aws_compliance_status(),
iam_issues=iam_issues,
network_findings=network_findings,
encryption_status=encryption_status,
scan_timestamp=datetime.now().isoformat()
)
except Exception as e:
logger.error(f"Error scanning AWS infrastructure: {str(e)}")
raise
async def scan_azure_infrastructure(self) -> CloudScanResult:
"""Scan Azure infrastructure for security issues."""
try:
print("Scanning Azure infrastructure...")
# Get all subscriptions
subscriptions = list(self.subscription_client.subscriptions.list())
all_resources = []
all_vulnerabilities = []
all_misconfigs = []
all_compliance = {}
all_iam = []
all_network = []
all_encryption = {}
# Scan each subscription
for subscription in subscriptions:
# Initialize Security Center client for this subscription
security_client = SecurityCenter(
self.azure_credential,
subscription.subscription_id
)
# Gather data concurrently
resources, assessments, policies = await asyncio.gather(
self._get_azure_resources(subscription.subscription_id),
self._get_azure_security_assessments(security_client),
self._get_azure_security_policies(security_client)
)
all_resources.extend(resources)
# Process security assessments into vulnerabilities
for assessment in assessments:
all_vulnerabilities.append(CloudVulnerability(
provider='azure',
resource_id=assessment.id,
severity=assessment.status.severity,
description=assessment.metadata.description,
recommendation=assessment.metadata.remediation_description,
compliance_standards=assessment.metadata.standards,
risk_score=float(assessment.status.score)
))
# Get additional security information
iam_results = await self._check_azure_iam_issues(subscription.subscription_id)
network_results = await self._check_azure_network_security(subscription.subscription_id)
encryption_results = await self._check_azure_encryption(subscription.subscription_id)
all_iam.extend(iam_results)
all_network.extend(network_results)
all_encryption.update(encryption_results)
return CloudScanResult(
provider='azure',
resources=all_resources,
vulnerabilities=all_vulnerabilities,
misconfigurations=all_misconfigs,
compliance_status=all_compliance,
iam_issues=all_iam,
network_findings=all_network,
encryption_status=all_encryption,
scan_timestamp=datetime.now().isoformat()
)
except Exception as e:
logger.error(f"Error scanning Azure infrastructure: {str(e)}")
raise
async def scan_gcp_infrastructure(self) -> CloudScanResult:
"""Scan GCP infrastructure for security issues."""
try:
print("Scanning GCP infrastructure...")
# Get organization ID
org_id = await self._get_gcp_org_id()
if not org_id:
raise ValueError("No GCP organization found")
# Create the organization path
org_path = f"organizations/{org_id}"
# Gather data concurrently
resources, findings = await asyncio.gather(
self._get_gcp_resources(org_path),
self._get_gcp_security_findings(org_path)
)
# Process findings into vulnerabilities
vulnerabilities = []
for finding in findings:
vulnerabilities.append(CloudVulnerability(
provider='gcp',
resource_id=finding.resource_name,
severity=finding.severity,
description=finding.description,
recommendation=finding.recommendation,
compliance_standards=finding.compliance_standards,
risk_score=float(finding.severity_score)
))
# Get additional security information
iam_issues = await self._check_gcp_iam_issues(org_path)
network_findings = await self._check_gcp_network_security(org_path)
encryption_status = await self._check_gcp_encryption(org_path)
return CloudScanResult(
provider='gcp',
resources=resources,
vulnerabilities=vulnerabilities,
misconfigurations=await self._get_gcp_misconfigurations(org_path),
compliance_status=await self._get_gcp_compliance_status(org_path),
iam_issues=iam_issues,
network_findings=network_findings,
encryption_status=encryption_status,
scan_timestamp=datetime.now().isoformat()
)
except Exception as e:
logger.error(f"Error scanning GCP infrastructure: {str(e)}")
raise
# AWS Helper Methods
async def _get_aws_resources(self) -> List[CloudResource]:
"""Get AWS resources across all regions."""
resources = []
regions = self.aws_session.get_available_regions('ec2')
async def scan_region(region):
try:
resource = boto3.resource('ec2', region_name=region)
instances = list(resource.instances.all())
for instance in instances:
resources.append(CloudResource(
provider='aws',
resource_type='ec2',
resource_id=instance.id,
name=next((tag['Value'] for tag in instance.tags or []
if tag['Key'] == 'Name'), ''),
region=region,
tags=instance.tags or {},
configuration={
'state': instance.state['Name'],
'type': instance.instance_type,
'vpc_id': instance.vpc_id,
'subnet_id': instance.subnet_id,
'security_groups': [sg['GroupId'] for sg in instance.security_groups]
}
))
except Exception as e:
logger.error(f"Error scanning AWS region {region}: {str(e)}")
await asyncio.gather(*[scan_region(region) for region in regions])
return resources
# Azure Helper Methods
async def _get_azure_resources(self, subscription_id: str) -> List[CloudResource]:
"""Get Azure resources for a subscription."""
from azure.mgmt.resource import ResourceManagementClient
resources = []
resource_client = ResourceManagementClient(self.azure_credential, subscription_id)
for resource in resource_client.resources.list():
resources.append(CloudResource(
provider='azure',
resource_type=resource.type,
resource_id=resource.id,
name=resource.name,
region=resource.location,
tags=resource.tags or {},
configuration=resource.as_dict()
))
return resources
# GCP Helper Methods
async def _get_gcp_resources(self, org_path: str) -> List[CloudResource]:
"""Get GCP resources for an organization."""
resources = []
# Create asset feed request
request = asset_v1.ListAssetsRequest(
parent=org_path,
asset_types=['compute.googleapis.com/Instance']
)
try:
# List all assets
for asset in self.gcp_asset_client.list_assets(request):
resources.append(CloudResource(
provider='gcp',
resource_type=asset.asset_type,
resource_id=asset.name,
name=asset.display_name,
region=asset.location,
tags=asset.resource.data.get('labels', {}),
configuration=asset.resource.data
))
except Exception as e:
logger.error(f"Error getting GCP resources: {str(e)}")
return resources
# Additional helper methods would be implemented here for each provider
# Including methods for checking IAM, network security, encryption, etc.
async def _get_aws_security_findings(self, securityhub) -> List[Dict]:
"""Get AWS SecurityHub findings."""
# Implementation here
return []
async def _get_aws_config_findings(self, config) -> List[Dict]:
"""Get AWS Config findings."""
# Implementation here
return []
async def _check_aws_iam_issues(self) -> List[Dict]:
"""Check AWS IAM issues."""
# Implementation here
return []
async def _check_aws_network_security(self) -> List[Dict]:
"""Check AWS network security."""
# Implementation here
return []
async def _check_aws_encryption(self) -> Dict:
"""Check AWS encryption status."""
# Implementation here
return {}
async def _get_aws_compliance_status(self) -> Dict:
"""Get AWS compliance status."""
# Implementation here
return {}