Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SecureRandom #1817

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions core/main/crypto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
# Browser Exploitation Framework (BeEF) - http://beefproject.com
# See the file 'doc/COPYING' for copying permission
#
require 'securerandom'

module BeEF
module Core
module Crypto

# @note the minimum length of the security token
TOKEN_MINIMUM_LENGTH = 15

#
# Generate a secure random token
#
Expand All @@ -27,7 +27,7 @@ def self.secure_token(len = nil)
raise TypeError, "Token length is less than the minimum length enforced by the framework: #{TOKEN_MINIMUM_LENGTH}" if (token_length < TOKEN_MINIMUM_LENGTH)

# return random hex string
OpenSSL::Random.random_bytes(token_length).unpack("H*")[0]
SecureRandom.random_bytes(token_length).unpack("H*")[0]
end

#
Expand All @@ -37,11 +37,12 @@ def self.secure_token(len = nil)
# @return [String] Security token
#
def self.api_token

config = BeEF::Core::Configuration.instance
token_length = 20

# return random hex string
token = OpenSSL::Random.random_bytes(token_length).unpack("H*")[0]
token = SecureRandom.random_bytes(token_length).unpack("H*")[0]
config.set('beef.api_token', token)
token
end
Expand Down Expand Up @@ -69,7 +70,7 @@ def self.random_hex_string(length = 10)
raise TypeError, 'Invalid length' unless length.integer?
raise TypeError, 'Invalid length' unless length.positive?

OpenSSL::Random.random_bytes(length).unpack('H*').first[0...length]
SecureRandom.random_bytes(length).unpack('H*').first[0...length]
end

#
Expand Down