|
| 1 | +# frozen_string_literal: true |
| 2 | +# SPDX-License-Identifier: Proprietary |
| 3 | +# |
| 4 | +# ASRFacet-Rb: Attack Surface Reconnaissance Framework |
| 5 | +# Copyright (c) 2026 voltsparx |
| 6 | +# |
| 7 | +# Author: voltsparx |
| 8 | +# Repository: https://github.com/voltsparx/ASRFacet-Rb |
| 9 | +# Contact: voltsparx@gmail.com |
| 10 | +# License: See LICENSE file in the project root |
| 11 | +# |
| 12 | +# This file is part of ASRFacet-Rb and is subject to the terms |
| 13 | +# and conditions defined in the LICENSE file. |
| 14 | + |
| 15 | +module ASRFacet |
| 16 | + module Output |
| 17 | + class ChartDataBuilder |
| 18 | + def initialize(result_store) |
| 19 | + @store = result_store |
| 20 | + end |
| 21 | + |
| 22 | + def build |
| 23 | + { |
| 24 | + severity_distribution: severity_distribution, |
| 25 | + subdomain_source_share: subdomain_source_share, |
| 26 | + port_frequency: port_frequency, |
| 27 | + service_breakdown: service_breakdown, |
| 28 | + finding_timeline: finding_timeline, |
| 29 | + ip_class_distribution: ip_class_distribution |
| 30 | + } |
| 31 | + end |
| 32 | + |
| 33 | + def severity_distribution |
| 34 | + counts = Hash.new(0) |
| 35 | + Array(@store.findings).each do |finding| |
| 36 | + severity = finding[:severity]&.to_s&.downcase&.capitalize |
| 37 | + severity = "Informational" if severity.empty? |
| 38 | + counts[severity] += 1 |
| 39 | + end |
| 40 | + counts.map { |label, value| { label: label, value: value } } |
| 41 | + end |
| 42 | + |
| 43 | + def port_frequency |
| 44 | + frequency = Hash.new(0) |
| 45 | + @store.ports.each_value do |ports| |
| 46 | + Array(ports).each { |port| frequency[port[:port].to_s] += 1 } |
| 47 | + end |
| 48 | + frequency.sort_by { |_port, count| -count }.first(10).map do |port, count| |
| 49 | + { port: port, count: count } |
| 50 | + end |
| 51 | + end |
| 52 | + |
| 53 | + def service_breakdown |
| 54 | + counts = Hash.new(0) |
| 55 | + @store.ports.each_value do |ports| |
| 56 | + Array(ports).each do |port| |
| 57 | + service = port[:service].to_s.downcase |
| 58 | + service = "unknown" if service.empty? |
| 59 | + counts[service] += 1 |
| 60 | + end |
| 61 | + end |
| 62 | + counts.sort_by { |_service, count| -count }.first(8).map do |service, count| |
| 63 | + { label: service, value: count } |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + def subdomain_source_share |
| 68 | + counts = Hash.new(0) |
| 69 | + Array(@store.respond_to?(:subdomains_with_sources) ? @store.subdomains_with_sources : []).each do |entry| |
| 70 | + source = entry[:source].to_s.capitalize |
| 71 | + source = "Unknown" if source.empty? |
| 72 | + counts[source] += 1 |
| 73 | + end |
| 74 | + return [{ label: "All Sources", value: @store.subdomains.size }] if counts.empty? |
| 75 | + |
| 76 | + counts.map { |label, value| { label: label, value: value } } |
| 77 | + end |
| 78 | + |
| 79 | + def finding_timeline |
| 80 | + grouped = Array(@store.findings).group_by do |finding| |
| 81 | + stamp = finding[:found_at] |
| 82 | + stamp.respond_to?(:strftime) ? stamp.strftime("%H:%M") : "N/A" |
| 83 | + end.transform_values(&:size) |
| 84 | + grouped.map { |time, count| { time: time || "N/A", count: count } }.sort_by { |entry| entry[:time] } |
| 85 | + end |
| 86 | + |
| 87 | + def ip_class_distribution |
| 88 | + counts = { "Private" => 0, "Class A" => 0, "Class B" => 0, "Class C" => 0, "Other" => 0 } |
| 89 | + Array(@store.ips).each { |ip| counts[classify_ip(ip)] += 1 } |
| 90 | + counts.reject { |_label, value| value.zero? }.map { |label, value| { label: label, value: value } } |
| 91 | + end |
| 92 | + |
| 93 | + private |
| 94 | + |
| 95 | + def classify_ip(ip) |
| 96 | + octets = ip.to_s.split(".").map(&:to_i) |
| 97 | + return "Other" unless octets.size == 4 |
| 98 | + |
| 99 | + first = octets[0] |
| 100 | + return "Private" if first == 10 |
| 101 | + return "Private" if first == 172 && octets[1].between?(16, 31) |
| 102 | + return "Private" if first == 192 && octets[1] == 168 |
| 103 | + return "Class A" if first.between?(1, 126) |
| 104 | + return "Class B" if first.between?(128, 191) |
| 105 | + return "Class C" if first.between?(192, 223) |
| 106 | + |
| 107 | + "Other" |
| 108 | + rescue ASRFacet::Error |
| 109 | + "Other" |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | +end |
0 commit comments