|
| 1 | +require 'rex/socket/ssl' |
| 2 | +require 'faker' |
| 3 | + |
| 4 | +module Msf |
| 5 | +module Ssl |
| 6 | + module CertProvider |
| 7 | + |
| 8 | + def self.rand_vars(opts = {}) |
| 9 | + opts ||= {} |
| 10 | + opts[:cc] ||= 'US' |
| 11 | + opts[:st] ||= Faker::Address.state_abbr |
| 12 | + opts[:loc] ||= Faker::Address.city |
| 13 | + opts[:org] ||= Faker::Company.name |
| 14 | + opts[:ou] ||= Faker::Hacker.send(%w{noun verb adjective}.sample.to_sym).gsub(/\W+/,'.') |
| 15 | + opts[:cn] ||= opts[:org].downcase.gsub(/and/,'').gsub(/\W+/,'.') + '.' + Faker::Internet.domain_suffix |
| 16 | + opts[:email] ||= "#{opts[:ou]}@#{opts[:cn]}" |
| 17 | + opts |
| 18 | + end |
| 19 | + |
| 20 | + def self.ssl_generate_subject(opts = {}) |
| 21 | + opts = self.rand_vars(opts) |
| 22 | + subject = "" |
| 23 | + subject << "/C=#{opts[:cc]}" if opts[:cc] |
| 24 | + subject << "/ST=#{opts[:st]}" if opts[:st] |
| 25 | + subject << "/O=#{opts[:org]}" if opts[:org] |
| 26 | + subject << "/OU=#{opts[:ou]}" if opts[:ou] |
| 27 | + subject << "/CN=#{opts[:cn]}" if opts[:cn] |
| 28 | + subject << "/emailAddress=#{opts[:email]}" if opts[:email] |
| 29 | + subject |
| 30 | + end |
| 31 | + |
| 32 | + # Not used, for API compatibility |
| 33 | + def self.ssl_generate_issuer( |
| 34 | + cc: 'US', |
| 35 | + org: Faker::Company.name, |
| 36 | + cn: Faker::Internet.domain_name |
| 37 | + ) |
| 38 | + "#{cc}/O=#{org}/CN=#{cn}" |
| 39 | + end |
| 40 | + |
| 41 | + # |
| 42 | + # Generate a realistic-looking but obstensibly fake SSL |
| 43 | + # certificate. Use Faker gem to mimic other self-signed |
| 44 | + # certificates on the web to reduce the chance of sig |
| 45 | + # identification by NIDS and the like. |
| 46 | + # |
| 47 | + # @return [String, String, Array] |
| 48 | + def self.ssl_generate_certificate(opts = {}, ksize = 2048) |
| 49 | + yr = 24*3600*365 |
| 50 | + vf = opts[:not_before] || Time.at(Time.now.to_i - rand(yr * 3) - yr) |
| 51 | + vt = opts[:not_after] || Time.at(vf.to_i + (rand(9)+1) * yr) |
| 52 | + cvars = opts[:cert_vars] || self.rand_vars |
| 53 | + subject = opts[:subject] || ssl_generate_subject(cvars) |
| 54 | + ctype = opts[:cert_type] || opts[:ca_cert].nil? ? :ca : :server |
| 55 | + key = opts[:key] || OpenSSL::PKey::RSA.new(ksize){ } |
| 56 | + cert = OpenSSL::X509::Certificate.new |
| 57 | + |
| 58 | + cert.version = opts[:version] || 2 |
| 59 | + cert.serial = opts[:serial] || (rand(0xFFFFFFFF) << 32) + rand(0xFFFFFFFF) |
| 60 | + cert.subject = OpenSSL::X509::Name.new([["C", subject]]) |
| 61 | + cert.issuer = opts[:ca_cert] || cert.subject |
| 62 | + cert.not_before = vf |
| 63 | + cert.not_after = vt |
| 64 | + cert.public_key = key.public_key |
| 65 | + |
| 66 | + bconst, kuse, ekuse = case ctype |
| 67 | + when :ca |
| 68 | + ['CA:TRUE', 'cRLSign,keyCertSign'] |
| 69 | + when :server |
| 70 | + ['CA:FALSE', 'digitalSignature,keyEncipherment', 'serverAuth'] |
| 71 | + when :client |
| 72 | + ['CA:FALSE', 'nonRepudiation,digitalSignature,keyEncipherment', 'clientAuth,emailProtection'] |
| 73 | + when :ocsp |
| 74 | + ['CA:FALSE', 'nonRepudiation,digitalSignature', 'serverAuth,OCSPSigning'] |
| 75 | + when :tsca |
| 76 | + ['CA:TRUE,pathlen:0', 'cRLSign,keyCertSign'] |
| 77 | + end |
| 78 | + |
| 79 | + ef = OpenSSL::X509::ExtensionFactory.new |
| 80 | + ef.subject_certificate = cert |
| 81 | + ef.issuer_certificate = cert |
| 82 | + cert.extensions = [ |
| 83 | + ef.create_extension("basicConstraints", bconst, true), |
| 84 | + ef.create_extension("subjectKeyIdentifier", "hash") |
| 85 | + ] |
| 86 | + if kuse and !kuse.empty? |
| 87 | + cert.extensions << ef.create_extension("keyUsage", kuse) |
| 88 | + end |
| 89 | + |
| 90 | + if ekuse and !ekuse.empty? |
| 91 | + cert.extensions << ef.create_extension("extendedKeyUsage", ekuse) |
| 92 | + end |
| 93 | + |
| 94 | + cert.sign(key, OpenSSL::Digest::SHA256.new) |
| 95 | + |
| 96 | + [key, cert, nil] |
| 97 | + end |
| 98 | + end |
| 99 | +end |
| 100 | +end |
0 commit comments