require 'securerandom'
module BeEF
module Core
module Crypto
TOKEN_MINIMUM_LENGTH = 15
def self.secure_token(len = nil)
config = BeEF::Core::Configuration.instance
token_length = len || config.get('beef.crypto_default_value_length').to_i
raise TypeError, "Token length is less than the minimum length enforced by the framework: #{TOKEN_MINIMUM_LENGTH}" if token_length < TOKEN_MINIMUM_LENGTH
SecureRandom.random_bytes(token_length).unpack1('H*')
end
def self.api_token
config = BeEF::Core::Configuration.instance
token_length = 20
token = SecureRandom.random_bytes(token_length).unpack1('H*')
config.set('beef.api_token', token)
token
end
def self.random_alphanum_string(length = 10)
raise TypeError, "'length' is #{length.class}; expected Integer" unless length.is_a?(Integer)
raise TypeError, "Invalid length: #{length}" unless length.positive?
[*('a'..'z'), *('A'..'Z'), *('0'..'9')].shuffle[0, length].join
end
def self.random_hex_string(length = 10)
raise TypeError, "'length' is #{length.class}; expected Integer" unless length.is_a?(Integer)
raise TypeError, "Invalid length: #{length}" unless length.positive?
SecureRandom.random_bytes(length).unpack1('H*')[0...length]
end
def self.dns_rule_id
id = nil
begin
id = random_hex_string(8)
BeEF::Core::Models::Dns::Rule.all.each { |rule| throw StandardError if id == rule.id }
rescue StandardError
retry
end
id.to_s
end
end
end
end