Skip to content

Instantly share code, notes, and snippets.

View kgriffs's full-sized avatar

Kurt Griffiths kgriffs

View GitHub Profile
@kgriffs
kgriffs / example.py
Created June 29, 2020 22:29
isplit -- Iterative split for Python strings.
import re
# Based on: https://stackoverflow.com/a/59071238/21784
def isplit(s, sep='\n'):
sep = re.escape(sep)
start = 0
for m in re.finditer(sep, s):
@kgriffs
kgriffs / example.py
Created June 24, 2020 01:13
ZeroSSL + S3 + cryptography -- 90-Day Cert Issue
import time
from urllib.parse import urlparse
import boto3
from cryptography import x509
from cryptography.x509.oid import NameOID
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
@kgriffs
kgriffs / example.sh
Created June 9, 2020 16:54
Get the directory for the currently running bash script
# https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
@kgriffs
kgriffs / example.py
Created June 2, 2020 21:56
Get TLS (SSL) certificate from peer in Python
import socket
import ssl
import certifi
_CERT_BUNDLE_PATH = certifi.where()
class CertificateStatus(Enum):
UNKNOWN = -1
CONNECTION_ERROR = 100
@kgriffs
kgriffs / string_util.lua
Created May 27, 2020 17:41
Lua string utilities (contains, startswith, endswith, replace, insert)
function string:contains(sub)
return self:find(sub, 1, true) ~= nil
end
function string:startswith(start)
return self:sub(1, #start) == start
end
function string:endswith(ending)
@kgriffs
kgriffs / ansible.cfg
Created May 24, 2020 00:47
Ansible Config Tuning
; https://gryzli.info/2019/02/21/tuning-ansible-for-maximum-performance/#Enabling_Pipelining
[defaults]
# Use the YAML callback plugin.
stdout_callback = yaml
# Use the stdout_callback when running ad-hoc commands.
bin_ansible_callbacks = False
deprecation_warnings=False
@kgriffs
kgriffs / example.sh
Last active May 18, 2020 15:14
Fix zlib not found on macOS when installing a python package that requires compilation
export CPATH=`xcrun --show-sdk-path`/usr/include
@kgriffs
kgriffs / example.py
Created April 21, 2020 19:55
Python - Verify x509 cert matches private key using cryptography
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization
def verify(path):
with open(path, 'rb') as f:
cert = x509.load_pem_x509_certificate(f.read(), default_backend())
with open(path, 'rb') as key_file:
private_key = serialization.load_pem_private_key(
@kgriffs
kgriffs / naxsi.rules
Created March 17, 2020 15:12 — forked from goblindegook/naxsi.rules
WordPress (Nginx)
# -*- mode: nginx; mode:autopair; mode: flyspell-prog; ispell-local-dictionary: "american" -*-
# LearningMode;
SecRulesEnabled;
DeniedUrl "/RequestDenied"
CheckRule "$SQL >= 8" BLOCK;
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 4" BLOCK;
@kgriffs
kgriffs / primary_host_ip.py
Created March 12, 2020 23:55
Get the primary IP address of the host machine
# https://stackoverflow.com/questions/166506/finding-local-ip-addresses-using-pythons-stdlib
def _get_primary_ip():
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
# doesn't even have to be reachable
sk.connect(('10.255.255.255', 1))
primary_ip = sk.getsockname()[0]
except Exception:
primary_ip = '127.0.0.1'
finally: