Module: HEITT::Formatter

Defined in:
lib/heitt/formatter.rb

Class Method Summary collapse

Class Method Details

.json(groups, extended: false, show_regex_match: false) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/heitt/formatter.rb', line 69

def self.json(groups, extended: false, show_regex_match: false)
  visible_groups = groups.select do |group|
    has_non_extended = group[:candidates].any? {|c| c[:extended] || extended}
    has_non_regex =  group[:candidates].any? {|c| c[:confidence] != "regex-match" || show_regex_match}
    has_non_extended && has_non_regex
  end
  #Renumber after filtering
  renumbered_groups = visible_groups.each_with_index.map { |group, index| group.merge(cluster_id: index+1)}

  JSON.pretty_generate(
    renumbered_groups.map do |group|
      visible_candidates = group[:candidates].select do |c|
        (!c[:extended] || extended)  && (c[:confidence] != "regex-match" || show_regex_match)
      end
      {
       cluster_id: group[:cluster_id],
       count: group[:count],
        hashes: group[:hashes],
        candidates: visible_candidates.map do |candidate|
          {
            name: candidate[:name],
            hashcat: candidate[:hashcat],
            john: candidate[:john],
            confidence: candidate[:confidence],
            extended: candidate[:extended],
            description: candidate[:description],
            notes: candidate[:notes]
          }
        end
      }
    end
  )
end

.render_tree(items, prefix = "", parent_is_last = true, is_root = true) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/heitt/formatter.rb', line 104

def self.render_tree(items, prefix = "", parent_is_last=true, is_root=true)
  result = ""

  items.each_with_index do |node, i|
    is_last_item = (i == items.length - 1)

    line = if is_root
      "#{node[:text]}\n"
    else
      "#{HEITT::Color.colorize(prefix, :blue)}#{HEITT::Color.colorize((is_last_item ? '└── ' : '├── '), :blue)}#{node[:text]}\n"
    end

    child_prefix = if is_root
      ""
    else
      "#{HEITT::Color.colorize(prefix, :bold, :blue)}#{HEITT::Color.colorize((is_last_item ? "    " : ""), :bold, :blue)}"
    end
    result += line 
    result += render_tree(node[:children], child_prefix, is_last_item, false) if node[:children].any?
  
    if is_last_item && !is_root and !node[:children].any?
      result += "#{HEITT::Color.colorize(prefix, :bold, :blue)}  \n"
    end
  end
  result
end

.tree(groups, verbose: false, extended: false, show_regex_match: false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/heitt/formatter.rb', line 8

def self.tree(groups, verbose: false, extended: false, show_regex_match: false)
  result = ""

  #Filter out groups with extended candidates as true
  visible_groups = groups.select do |group|
    has_non_extended = group[:candidates].any? {|c| !c[:extended] || extended}
    has_non_regex = group[:candidates].any? {|c| c[:confidence] != "regex-match" || show_regex_match}
    has_non_extended && has_non_regex
  end
  #Renumber after filtering
  renumbered_groups= visible_groups.each_with_index.map { |group, index| group.merge(cluster_id: index + 1) }

  root = {
    text: "#{HEITT::Color.colorize("\n\n[", :bold, :blue)}#{HEITT::Color.colorize("CLUSTERED HASHES", :green)}#{HEITT::Color.colorize("]", :bold, :blue)}", 
    children: renumbered_groups.map do |group|
      {
        text: HEITT::Color.colorize("HASH CLUSTER #{group[:cluster_id]}", :magenta, :bold),
        children: group[:hashes].map{|h| {text: h, children: []}}
      }
    end
  }

  result += render_tree([root])

  renumbered_groups.each do |group|
    result += "#{HEITT::Color.colorize("\n\n[", :bold, :blue)}#{HEITT::Color.colorize("HASH CLUSTER #{group[:cluster_id]}", :white, :bold)}#{HEITT::Color.colorize("]\n", :bold, :blue)}"#, children: []}
    candidate_nodes = (group[:candidates]).each_with_index.map do |candidate, idx|
      next if candidate.nil?
      next if candidate[:name].nil?
      next if candidate[:extended] && !extended
      next if candidate[:confidence] == "regex-match" && !show_regex_match
      confidence = candidate[:confidence] ? " — CONFIDENCE: #{candidate[:confidence].upcase}" : ""

      children = [
        {text: "Hashcat Mode: #{candidate[:hashcat] || "--"}", children: []},
        {text: "John Format: #{candidate[:john] || "--"}", children: []}
      ]

      if extended
        children << {text: "Extended: #{candidate[:extended]}", children: []}
      end

      if verbose
        if candidate[:description] && !candidate[:description].empty?
          children << {text: "Description: #{candidate[:description]}", children: []}
        end

        if candidate[:notes] && !candidate[:notes].empty?
          children << {text: "Notes:", children: candidate[:notes].map {|note| {text: note, children: []}}}
        end
      end
      {
        text: "#{HEITT::Color.colorize("[", :bold, :blue)}#{HEITT::Color.colorize("CANDIDATE #{idx + 1}: ", :bold, :cyan)}#{HEITT::Color.colorize("#{candidate[:name]}#{confidence}", :bold, :cyan)}#{HEITT::Color.colorize("]", :bold, :blue)}",
        children: children
      }
    end.compact
    result += render_tree(candidate_nodes, "", false, false) unless candidate_nodes.nil? || candidate_nodes.empty?
  end
  result
end