-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.py
106 lines (89 loc) · 3.08 KB
/
function.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
import json
import sys
import os
import boto3
import base64
import socket
from urllib import request
from pathlib import Path
from botocore.exceptions import ClientError
region = os.environ['REGION']
secretsname = os.environ['SECRETS_NAME']
secclient = boto3.client('secretsmanager')
def lambda_handler(event, context):
print("Lambda Debug Network")
# resolver configuration file
read_file('files.cfg')
secrets_manager_get(secretsname)
hosts('hosts.cfg')
url_resolves('request.cfg')
# dns_show_entries()
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
def port_is_open(ip, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(ip,port)
if result == 0:
print f'port {port} for ip {ip} is open'
else:
print f'port {port} for ip {ip} is NOT open'
sock.close()
def read_file(filepath):
# Make it generic. If file not found, just error it
print("\n\tReading file: {}\n".format(filepath))
with open(filepath) as fp:
for cnt, line in enumerate(fp):
fileline = line.strip()
print("\n\t\tReading file: '{}'\n".format(fileline))
my_file=Path(line.strip())
if my_file.exists():
with open(fileline) as ff:
for cnti, linei in enumerate(ff):
print("\t\t{}".format(linei))
print("\n")
def secrets_manager_list():
print("Retrieving all secret names")
response = secclient.list_secrets()
for secret in response['SecretList']:
print(secret['Name'])
def secrets_manager_get(secret):
print("Retrieving secret: {}".format(secret))
response = secclient.get_secret_value(
SecretId=secret
)
print(response['SecretString'])
def hosts(filepath):
print("Resolving hosts from file config: {}".format(filepath))
with open(filepath) as fp:
for cnt, line in enumerate(fp):
hostname = line.strip()
print("host '{}'\n".format(hostname))
hostname_resolves(hostname=hostname)
def hostname_resolves(hostname):
try:
ip = socket.gethostbyname(hostname)
print("Host {} resolved {}\n".format(hostname, ip))
except Exception as e:
print("Host {} not resolved. Error {}\n".format(hostname, e))
def url_resolves(filepath):
print("Trying to request url data from file: {}".format(filepath))
with open(filepath) as fp:
for cnt, line in enumerate(fp):
value = line.strip()
print("url '{}'\n".format(value))
url_request(value)
def url_request(url):
try:
contents = request.urlopen("https://secretsmanager.us-west-2.amazonaws.com").read()
print("URL {} response {}\n".format(url, contents))
except Exception as e:
print("Url {} not accessible. Error {}\n".format(url, e))
def dns_show_entries():
import dns.resolver
my_resolver = dns.resolver.Resolver()
my_resolver.nameservers = ['8.8.8.8']
answer = my_resolver.query('google.com')
for data in answer:
print(data)