Skip to content

Instantly share code, notes, and snippets.

@brevityinmotion
brevityinmotion / brevity-lambda-parameters.py
Created June 9, 2021 04:26
Retrieve parameters within Lambda functions
def lambda_handler(event, context):
def _getParameters(paramName):
client = boto3.client('ssm')
response = client.get_parameter(
Name=paramName
)
return response['Parameter']['Value']
inputBucketName = _getParameters('inputBucketName')
@brevityinmotion
brevityinmotion / brevity-lambda-update.sh
Created June 9, 2021 04:06
Lambda package update template
#!/bin/bash
LAMBDANAME="brevity-example"
mkdir /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME
cp -r /home/ec2-user/environment/brevity-infra/lib/brevitycore /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME
cp /home/ec2-user/environment/brevity-infra/lambdas/lambda_function_$LAMBDANAME.py /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME/lambda_function.py
cd /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME
zip -r ../$LAMBDANAME.zip *
aws s3 cp /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME.zip s3://brevity-deploy/infra/
aws lambda update-function-code --function-name $LAMBDANAME --s3-bucket brevity-deploy --s3-key infra/$LAMBDANAME.zip
@brevityinmotion
brevityinmotion / brevity-lambda-layer.sh
Created June 9, 2021 04:02
Lambda layer creation template script
#!/bin/bash
NEWLAYER="brevity-example"
## Creating a Lambda Layer
## Script credits from https://towardsdatascience.com/python-packages-in-aws-lambda-made-easy-8fbc78520e30
cd /home/ec2-user/environment/brevity-infra/lambdas/layers/
mkdir $NEWLAYER
@brevityinmotion
brevityinmotion / brevity-lambda-create.sh
Created June 9, 2021 04:01
Lambda create template script
#!/bin/bash
LAMBDANAME="brevity-example"
mkdir /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME
# Copy any custom python or dependent files to Lambda build directory
cp -r /home/ec2-user/environment/brevity-infra/lib/brevitycore /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME
# Copy the primary Lambda file to the build directory
cp /home/ec2-user/environment/brevity-infra/lambdas/lambda_function_$LAMBDANAME.py /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME/lambda_function.py
cd /home/ec2-user/environment/brevity-infra/lambdas/build/$LAMBDANAME
# Create the zipped Lambda package
zip -r ../$LAMBDANAME.zip *
@brevityinmotion
brevityinmotion / brevity-process-httpx.py
Created June 9, 2021 03:59
Function to parse and process output from HTTPX
def processHTTPXCrawl(programName, rawBucketPath, refinedBucketPath, inputBucketPath, presentationBucketPath):
filePath = refinedBucketPath + programName + '/' + programName + '-httpx-crawl.json'
df = pd.read_json(filePath, lines=True)
df['program'] = programName
from urllib.parse import urlparse
def _parseUrlRoot(urlvalue):
cleanurl = urlparse(urlvalue).netloc
return cleanurl
@brevityinmotion
brevityinmotion / brevity-sonar-query.py
Created June 9, 2021 03:57
Generation of an Athena query to run against the Project Sonar dataset
def sonarGenerateSubdomains(programName, refinedBucketPath, ATHENA_DB, ATHENA_BUCKET, ATHENA_TABLE):
# Retrieve the input data to process from the list of domains
storePath = refinedBucketPath + programName + '/' + programName + '-domains-roots.csv'
dfDomainRoots = pd.read_csv(storePath)
# Prepare the domain roots for the query
dfDomainRoots['athenaquery'] = "'%." + dfDomainRoots['domain'] + "'"
searchDomains = dfDomainRoots['athenaquery'].tolist()
searchDomainString = ' OR name LIKE '.join(searchDomains)
# print(searchDomainString)
query = "SELECT * FROM %s WHERE name LIKE %s AND date = (SELECT MAX(date) from %s);" % (ATHENA_TABLE,searchDomainString,ATHENA_TABLE)
@brevityinmotion
brevityinmotion / brevity-process-gospider.py
Created June 9, 2021 03:55
Function to run GoSpider and then process the output
def generateScriptGoSpider(programName, inputBucketName):
fileBuffer = io.StringIO()
fileContents = f"""#!/bin/bash
# Run custom goSpider script
export HOME=/root
export PATH=/root/go/bin:$PATH
mkdir $HOME/security/refined/{programName}
mkdir $HOME/security/refined/{programName}/crawl
gospider -S $HOME/security/inputs/{programName}/{programName}-urls-base.csv -o $HOME/security/raw/{programName}/crawl -u web -t 1 -c 5 -d 1 --js --sitemap --robots --other-source --include-subs --include-other-source
cd $HOME/security/raw/{programName}/
@brevityinmotion
brevityinmotion / brevity-droplet-delete.py
Created June 9, 2021 03:51
Lambda function to delete droplets that are in a shutdown state
import json, boto3
import brevitycore
import digitalocean
def lambda_handler(event, context):
secretName = 'digitalocean'
regionName = 'us-east-1'
accessToken = brevitycore.get_secret(secretName,regionName)
@brevityinmotion
brevityinmotion / brevity-droplet-create.py
Created June 9, 2021 03:49
Droplet creation plus userdata startup script example for DigitalOcean
import digitalocean
import json, io
def createDroplet(accessToken,dropletName,runOperation,programName,awsAccessKeyId,awsSecretKey):
def _generateUserDataScript(runOperation,programName,awsAccessKeyId,awsSecretKey):
import io
fileBuffer = io.StringIO()
fileContents = """#cloud-config
packages:
@brevityinmotion
brevityinmotion / brevity-shell-cloud-init.sh
Created June 9, 2021 03:44
Command to tail output from DigitalOcean cloud-init script
tail -f /var/log/cloud-init-output.log