Skip to content

Commit

Permalink
fix: Minor form cleanup on hash lists and rules
Browse files Browse the repository at this point in the history
Signed-off-by: UncleSp1d3r <[email protected]>
  • Loading branch information
unclesp1d3r committed Jun 11, 2024
1 parent 79379ee commit dec1632
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 40 deletions.
35 changes: 14 additions & 21 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ GEM
activerecord (>= 5.2, < 7.2)
activesupport (>= 5.2, < 7.2)
aws-eventstream (1.3.0)
aws-partitions (1.940.0)
aws-partitions (1.943.0)
aws-sdk-core (3.197.0)
aws-eventstream (~> 1, >= 1.3.0)
aws-partitions (~> 1, >= 1.651.0)
Expand All @@ -121,7 +121,7 @@ GEM
aws-sdk-kms (1.83.0)
aws-sdk-core (~> 3, >= 3.197.0)
aws-sigv4 (~> 1.1)
aws-sdk-s3 (1.152.0)
aws-sdk-s3 (1.152.1)
aws-sdk-core (~> 3, >= 3.197.0)
aws-sdk-kms (~> 1)
aws-sigv4 (~> 1.8)
Expand Down Expand Up @@ -160,7 +160,7 @@ GEM
regexp_parser (>= 1.5, < 3.0)
xpath (~> 3.2)
cocoon (1.2.15)
concurrent-ruby (1.3.1)
concurrent-ruby (1.3.3)
connection_pool (2.4.1)
crass (1.0.6)
cssbundling-rails (1.4.0)
Expand Down Expand Up @@ -304,11 +304,11 @@ GEM
racc (~> 1.4)
nokogiri (1.16.5-x86_64-linux)
racc (~> 1.4)
oj (3.16.3)
oj (3.16.4)
bigdecimal (>= 3.0)
orm_adapter (0.5.0)
pagy (8.4.4)
parallel (1.24.0)
parallel (1.25.1)
parser (3.3.2.0)
ast (~> 2.4.1)
racc
Expand All @@ -323,7 +323,7 @@ GEM
nio4r (~> 2.0)
raabro (1.4.0)
racc (1.8.0)
rack (3.0.11)
rack (3.1.2)
rack-mini-profiler (3.3.1)
rack (>= 1.2.0)
rack-session (2.0.0)
Expand Down Expand Up @@ -377,8 +377,8 @@ GEM
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.8)
strscan (>= 3.0.9)
rexml (3.3.0)
strscan
rspec-core (3.13.0)
rspec-support (~> 3.13.0)
rspec-expectations (3.13.0)
Expand Down Expand Up @@ -426,10 +426,8 @@ GEM
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.3)
parser (>= 3.3.1.0)
rubocop-capybara (2.20.0)
rubocop (~> 1.41)
rubocop-factory_bot (2.25.1)
rubocop (~> 1.41)
rubocop-factory_bot (2.26.1)
rubocop (~> 1.61)
rubocop-minitest (0.35.0)
rubocop (>= 1.61, < 2.0)
rubocop-ast (>= 1.31.1, < 2.0)
Expand All @@ -450,13 +448,8 @@ GEM
rubocop-rails
rubocop-rake (0.6.0)
rubocop (~> 1.0)
rubocop-rspec (2.30.0)
rubocop (~> 1.40)
rubocop-capybara (~> 2.17)
rubocop-factory_bot (~> 2.22)
rubocop-rspec_rails (~> 2.28)
rubocop-rspec_rails (2.28.3)
rubocop (~> 1.40)
rubocop-rspec (3.0.1)
rubocop (~> 1.61)
rubocop-thread_safety (0.5.1)
rubocop (>= 0.90.0)
ruby-next-core (1.0.3)
Expand Down Expand Up @@ -509,7 +502,7 @@ GEM
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
sprockets-rails (3.5.0)
sprockets-rails (3.5.1)
actionpack (>= 6.1)
activesupport (>= 6.1)
sprockets (>= 3.0.0)
Expand Down Expand Up @@ -659,4 +652,4 @@ RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.5.10
2.5.11
4 changes: 3 additions & 1 deletion app/controllers/api/v1/client/agents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ def submit_benchmark
benchmark_record.runtime = benchmark[:runtime].to_i
records.append(benchmark_record)
end
@agent.hashcat_benchmarks.destroy_all
if @agent.hashcat_benchmarks.append(records)
return @agent.benchmarked
@agent.benchmarked
return
end
render json: { errors: @agent.errors }, status: :unprocessable_entity
end
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/hash_lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ def index
def show; end

# GET /hash_lists/new
def new
@hash_list = HashList.new
end
def new; end

# GET /hash_lists/1/edit
def edit; end
Expand All @@ -28,7 +26,7 @@ def create
format.html { redirect_to hash_list_url(@hash_list), notice: "Hash list was successfully created." }
format.json { render :show, status: :created, location: @hash_list }
else
format.html { render :new, status: :unprocessable_entity }
format.html { render :new, status: :unprocessable_entity, error: "Hash list could not be created." }
format.json { render json: @hash_list.errors, status: :unprocessable_entity }
end
end
Expand Down Expand Up @@ -61,7 +59,7 @@ def destroy

# Only allow a list of trusted parameters through.
def hash_list_params
params.require(:hash_list).permit(:name, :description, :file, :line_count, :sensitive, :hash_mode)
params.require(:hash_list).permit(:name, :description, :file, :line_count, :sensitive, :project_id, :hash_type_id)
end

# Use callbacks to share common setup or constraints between actions.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/rule_lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def destroy

# Only allow a list of trusted parameters through.
def rule_list_params
params.require(:rule_list).permit(:name, :description, :file, :line_count, :sensitive)
params.require(:rule_list).permit(:name, :description, :file, :line_count, :sensitive, project_ids: [])
end

# Use callbacks to share common setup or constraints between actions.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/word_lists_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ def set_word_list

# Only allow a list of trusted parameters through.
def word_list_params
params.require(:word_list).permit(:name, :description, :file, :line_count, :sensitive)
params.require(:word_list).permit(:name, :description, :file, :line_count, :sensitive, project_ids: [])
end
end
23 changes: 19 additions & 4 deletions app/models/agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class Agent < ApplicationRecord

event :benchmarked do
transition pending: :active
transition any => same
end

event :deactivate do
Expand All @@ -85,17 +86,18 @@ class Agent < ApplicationRecord
end

event :check_benchmark_age do
transition active: same
transition active: :pending if ->(agent) { agent.hashcat_benchmarks.empty? }
transition active: :pending if ->(agent) { agent.last_benchmark_date >= ApplicationConfig.max_benchmark_age.ago }
# transition active: :pending if ->(agent) { agent.last_benchmark_date <= ApplicationConfig.max_benchmark_age.ago }
transition any => same
end

event :heartbeat do
# If the agent has been offline for more than 12 hours, we'll transition it to pending.
# This will require the agent to benchmark again.
transition offline: :pending if ->(agent) { agent.last_seen_at > ApplicationConfig.max_offline_time.ago }
transition offline: :pending if ->(agent) { agent.last_seen_at < ApplicationConfig.max_offline_time.ago }
# If the agent has only been offline for less than 12 hours, we'll keep it active.
transition offline: :active if ->(agent) { agent.last_seen_at <= ApplicationConfig.max_offline_time.ago }
transition offline: :active if ->(agent) { agent.last_seen_at >= ApplicationConfig.max_offline_time.ago }

transition any => same
end
Expand Down Expand Up @@ -139,6 +141,17 @@ def last_benchmark_date
end
end

# Returns the last benchmarks recorded for the agent.
#
# If there are no benchmarks available, it returns nil.
#
# @return [ActiveRecord::Relation, nil] The last benchmarks recorded for the agent, or nil if there are no benchmarks.
def last_benchmarks
return nil if hashcat_benchmarks.empty?
max = hashcat_benchmarks.maximum(:benchmark_date)
hashcat_benchmarks.where(benchmark_date: (max.all_day)).order(hash_type: :asc)
end

# Public: Finds or creates a new task for the agent.
#
# This method is responsible for assigning a new task to the agent. It follows a specific logic to determine which task to assign.
Expand All @@ -155,7 +168,9 @@ def new_task
# first we assign any tasks that are assigned to the agent and are incomplete.
if tasks.incomplete.any? && tasks.incomplete.where(agent_id: id).any?
incomplete_task = tasks.incomplete.where(agent_id: id).first
return incomplete_task if incomplete_task.present?

# If the task is incomplete and there are no errors for the task, we'll return the task.
return incomplete_task if incomplete_task.present? && !agent_errors.where([task_id: incomplete_task.id, severity: AgentError.severities[:fatal]]).any?
end

# Ok, so there's no existing tasks already assigned to the agent.
Expand Down
2 changes: 1 addition & 1 deletion app/models/hash_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class HashList < ApplicationRecord
validates :name, length: { maximum: 255 }
validates :separator, length: { is: 1, allow_blank: true }
validates :metadata_fields_count, numericality: { greater_than_or_equal_to: 0, only_integer: true }
validates :file, content_type: %w[text/plain], attached: ->(record) { record.processed? || record.file.attached? }
validates :file, attached: ->(record) { record.processed? || record.file.attached? }

broadcasts_refreshes unless Rails.env.test?

Expand Down
4 changes: 4 additions & 0 deletions app/models/hash_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,8 @@ class HashType < ApplicationRecord
instant_messaging: 20,
cryptocurrency: 21
}

def to_s
"#{hash_type.hashcat_mode} (#{hash_type.name})"
end
end
5 changes: 1 addition & 4 deletions app/views/agents/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
<%= show_for @agent do |s| %>
<%= turbo_stream_from @agent %>
<%= s.attribute :client_signature %>
<%= s.attribute :command_parameters %>
<%= s.attribute :cpu_only %>
<%= s.attribute :ignore_errors %>
<%= s.attribute :active %>
<%= s.attribute :state %>
<%= s.attribute :trusted %>
<%= s.attribute :last_ipaddress %>
<%= s.attribute :last_seen_at, format: :short %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/hash_lists/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<%= simple_form_for(@hash_list) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

<div class="form-inputs">
<%= f.input :name %>
<%= f.input :description %>
<%= f.input :file %>
<%= f.input :sensitive %>
<%= f.association :project, include_blank: false %>
<%= f.association :hash_type, include_blank: false %>
</div>

Expand Down
3 changes: 2 additions & 1 deletion app/views/rule_lists/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
<%= f.input :name %>
<%= f.input :description %>
<%= f.input :file %>
<%= f.input :line_count %>
<%= f.input :sensitive %>
<%= f.association :projects, as: :check_boxes, include_blank: false,
collection: Project.accessible_by(current_ability) %>
</div>

<div class="form-actions">
Expand Down

0 comments on commit dec1632

Please sign in to comment.