Skip to content

Instantly share code, notes, and snippets.

@Soham3-1415
Soham3-1415 / pem_extractor.py
Created June 17, 2022 16:08
Extract PEM stuff from your file with this one easy script. Plug it into Unix find to extract all the PEM stuff from all the files.
#!/bin/python3
import sys,re,json
PEM = re.compile(b'(?P<whole>(?P<header>(?:-----)(?:(?:BEGIN) (?P<type>(?:[A-Z]+ )*(?:[A-Z]+)))(?:-----)(?:\n|\r|\r\n))(?P<inner_base64>(?:(?:(?:[0-9a-zA-Z\+\/]{4}){16})(?:\n|\r|\r\n))*(?:(?:(?:[A-Za-z0-9+\/]{4}){0,15})(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=|[A-Za-z0-9+\/]{4}))(?:\n|\r|\r\n))(?P<footer>(?:-----)(?:(?:END) (?P=type))(?:-----)))')
if not sys.argv[1]:
sys.exit(1)
filename = sys.argv[1]