Skip to content

Instantly share code, notes, and snippets.

@major
Created March 17, 2014 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save major/9606037 to your computer and use it in GitHub Desktop.
Save major/9606037 to your computer and use it in GitHub Desktop.
Terrible half-done script to pull data from scans.io into mongodb
#!/usr/bin/env python
import gzip
import OpenSSL
from pymongo import MongoClient
import ssl
import sys
import textwrap
from pprint import pprint
client = MongoClient('mongodb://localhost:27017/')
db = client.certdb
for line in gzip.open('20140310_certs.gz'):
(certhash, base64text) = line.split(',',1)
base64text = "\n".join(textwrap.wrap(base64text,76))
cert = "-----BEGIN CERTIFICATE-----\n%s\n-----END CERTIFICATE-----" % base64text
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
# certsubj = x509.get_subject().get_components()
# try:
# cn = [x[1] for x in certsubj if 'CN' in x][0]
# except:
# print certsubj
# print cn
subject_dict = dict(x509.get_subject().get_components())
issuer_dict = dict(x509.get_issuer().get_components())
certdict = {'hash_id': certhash, 'issuer': issuer_dict, 'subject': subject_dict}
print certdict
certsdb = db.certsdb
try:
mongo_id = certsdb.update({'hash_id': certhash},certdict,upsert=True)
print "Inserted into mongo: %s" % mongo_id
except:
print "Insert failed"
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment