#1# Copyright (c) 2006-2025 Wade Alcorn - [email protected]2# Browser Exploitation Framework (BeEF) - https://beefproject.com3# See the file 'doc/COPYING' for copying permission4#56module BeEF7module Core8module Rest9class Admin < BeEF::Core::Router::Router10config = BeEF::Core::Configuration.instance11time_since_last_failed_auth = 01213before do14# @todo: this code comment is a lie. why is it here?15# error 401 unless params[:token] == config.get('beef.api_token')16halt 401 unless BeEF::Core::Rest.permitted_source?(request.ip)1718# halt if requests are inside beef.restrictions.api_attempt_delay19if time_since_last_failed_auth != 0 && !BeEF::Core::Rest.timeout?('beef.restrictions.api_attempt_delay',20time_since_last_failed_auth,21->(time) { time_since_last_failed_auth = time })22halt 40123end2425headers 'Content-Type' => 'application/json; charset=UTF-8',26'Pragma' => 'no-cache',27'Cache-Control' => 'no-cache',28'Expires' => '0'29end3031# @note Authenticate using the config set username/password to retrieve the "token" used for subsquent calls.32# Return the secret token used for subsquene tAPI calls.33#34# Input must be specified in JSON format35#36# +++ Example: +++37# POST /api/admin/login HTTP/1.138# Host: 127.0.0.1:300039# Content-Type: application/json; charset=UTF-840# Content-Length: 1841#42# {"username":"beef", "password":"beef"}43#===response (snip)===44# HTTP/1.1 200 OK45# Content-Type: application/json; charset=UTF-846# Content-Length: 3547#48# {"success":"true","token":"122323121"}49#50post '/login' do51request.body.rewind52begin53data = JSON.parse request.body.read54if data['username'].eql?(config.get('beef.credentials.user')) && data['password'].eql?(config.get('beef.credentials.passwd'))55return {56'success' => true,57'token' => config.get('beef.api_token').to_s58}.to_json59end6061BeEF::Core::Logger.instance.register('Authentication', "User with ip #{request.ip} has failed to authenticate in the application.")62time_since_last_failed_auth = Time.now63halt 40164rescue StandardError65error 40066end67end68end69end70end71end727374