-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathssl_cert_scraper.rb
More file actions
50 lines (38 loc) · 1.38 KB
/
ssl_cert_scraper.rb
File metadata and controls
50 lines (38 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# A ruby shell script to scrape for https certificates and decode them.
# @author - Evan Carmi
# a csv file with a number and host on each line. Ex: 1,google.com.
# Often useful: http://s3.amazonaws.com/alexa-static/top-1m.csv.zip
HOST_FILE = 'unseen.txt'
PORT = 443
# Directory for downloaded certificates
CERT_DIR = 'certs'
# Directory for decoded certificates
DECODED_DIR = 'decoded'
`mkdir -p #{CERT_DIR}/`
`mkdir -p #{DECODED_DIR}/`
def cmd_runner(cmd)
out = `#{cmd}`
unless $?.success?
puts "\n\n\nERROR\nFailed while running #{cmd} with error: #{out}\n\n\n"
end
$?.success?
end
File.open(HOST_FILE).each_line do |line|
domain = line.strip
# first check if server responds to https request
cmd = "curl -sI https://#{domain} --connect-timeout 2 -m 2"
worked = cmd_runner(cmd)
unless worked
domain.prepend("www.")
cmd = "curl -sI https://#{domain} --connect-timeout 2 -m 2"
end
next unless cmd_runner(cmd)
# download certificate
cmd1 = "echo -n | openssl s_client -connect #{domain}:#{PORT} | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > #{CERT_DIR}/#{domain}.cert"
next unless cmd_runner(cmd1)
# unpack certificate
cmd2 = "openssl x509 -in #{CERT_DIR}/#{domain}.cert -text -noout > #{DECODED_DIR}/#{domain}.cert.decoded"
next unless cmd_runner(cmd2)
cmd3 = "echo '#{domain}' >> downloaded_hosts.txt"
next unless cmd_runner(cmd3)
end