FROM lambci/lambda:build-python3.6
ENV AWS_DEFAULT_REGION ap-northeast-1
ADD . .
CMD pip3 install -r requirements.txt -t /var/task && \
zip -9 deploy_package.zip lambda_function.py && \
zip -r9 deploy_package.zip *
paramiko == 2.7.1
cffi == 1.14.1
boto3 == 1.14.35
pycrypto == 2.6.1
ls | grep -v -E 'lambda_function.py|sample.py|requirements.txt|Dockerfile|build.sh' | xargs rm -Rf
docker build -t sshmonitor .
docker run -v "$PWD":/var/task sshmonitor
import boto3
import logging
import paramiko
print('Start Scanning EC2 instances which password authentication is set to')
logging.basicConfig()
logging.getLogger("paramiko").setLevel(logging.DEBUG)
def lambda_handler(event, context):
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ec2 = boto3.client('ec2')
response = ec2.describe_instances()
reservations = response["Reservations"]
for reservation in reservations:
instance = reservation["Instances"][0]
try:
ip = instance["PublicIpAddress"]
print('Accessing: ' + ip)
client.connect(ip, username='Security Scanner.', password='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
except:
print('Done: ' + ip)
return 'Finish Scanning!'
Related