Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/cert_provider.rb
Views: 11623
require 'rex/socket/ssl'12module Msf3module Ssl4module CertProvider56def self.rand_vars(opts = {})7opts ||= {}8opts[:cc] ||= 'US'9opts[:st] ||= Faker::Address.state_abbr10opts[:loc] ||= Faker::Address.city11opts[:org] ||= Faker::Company.name12opts[:ou] ||= Faker::Hacker.send(%w{noun verb adjective}.sample.to_sym).gsub(/\W+/,'.')13opts[:cn] ||= opts[:org].downcase.gsub(/and/,'').gsub(/\W+/,'.') + '.' + Faker::Internet.domain_suffix14opts[:email] ||= "#{opts[:ou]}@#{opts[:cn]}"15opts16end1718def self.ssl_generate_subject(opts = {})19opts = self.rand_vars(opts)20subject = ""21subject << "/C=#{opts[:cc]}" if opts[:cc]22subject << "/ST=#{opts[:st]}" if opts[:st]23subject << "/O=#{opts[:org]}" if opts[:org]24subject << "/OU=#{opts[:ou]}" if opts[:ou]25subject << "/CN=#{opts[:cn]}" if opts[:cn]26subject << "/emailAddress=#{opts[:email]}" if opts[:email]27subject28end2930# Not used, for API compatibility31def self.ssl_generate_issuer(32cc: 'US',33org: Faker::Company.name,34cn: Faker::Internet.domain_name35)36"#{cc}/O=#{org}/CN=#{cn}"37end3839#40# Generate a realistic-looking but obstensibly fake SSL41# certificate. Use Faker gem to mimic other self-signed42# certificates on the web to reduce the chance of sig43# identification by NIDS and the like.44#45# @return [String, String, Array]46def self.ssl_generate_certificate(cert_vars: {}, ksize: 2048, **opts)47yr = 24*3600*36548vf = opts[:not_before] || Time.at(Time.now.to_i - rand(yr * 3) - yr)49vt = opts[:not_after] || Time.at(vf.to_i + (rand(4..9) * yr))50cvars = self.rand_vars(cert_vars)51subject = opts[:subject] || ssl_generate_subject(cvars)52ctype = opts[:cert_type] || opts[:ca_cert].nil? ? :ca : :server53key = opts[:key] || OpenSSL::PKey::RSA.new(ksize){ }54cert = OpenSSL::X509::Certificate.new5556cert.version = opts[:version] || 257cert.serial = opts[:serial] || (rand(0xFFFFFFFF) << 32) + rand(0xFFFFFFFF)58cert.subject = OpenSSL::X509::Name.parse(subject)59cert.issuer = opts[:ca_cert] || cert.subject60cert.not_before = vf61cert.not_after = vt62cert.public_key = key.public_key6364bconst, kuse, ekuse = case ctype65when :ca66['CA:TRUE', 'cRLSign,keyCertSign']67when :server68['CA:FALSE', 'digitalSignature,keyEncipherment', 'serverAuth']69when :client70['CA:FALSE', 'nonRepudiation,digitalSignature,keyEncipherment', 'clientAuth,emailProtection']71when :ocsp72['CA:FALSE', 'nonRepudiation,digitalSignature', 'serverAuth,OCSPSigning']73when :tsca74['CA:TRUE,pathlen:0', 'cRLSign,keyCertSign']75end7677ef = OpenSSL::X509::ExtensionFactory.new78ef.subject_certificate = cert79ef.issuer_certificate = cert80cert.extensions = [81ef.create_extension("basicConstraints", bconst, true),82ef.create_extension("subjectKeyIdentifier", "hash")83]84if kuse and !kuse.empty?85cert.extensions << ef.create_extension("keyUsage", kuse)86end8788if ekuse and !ekuse.empty?89cert.extensions << ef.create_extension("extendedKeyUsage", ekuse)90end9192cert.sign(key, OpenSSL::Digest.new('SHA256'))9394[key, cert, nil]95end96end97end98end99100101