summaryrefslogtreecommitdiff
path: root/lib/fake_agent.rb
blob: 675df10c2615b59a9d83aae1848ff571f87fc900 (plain)
1
2
3
4
5
6
7
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
68
69
70
71
72
73
74
75
76
require 'socket'

class FakeAgent
  attr_reader :id, :endpoint

  def initialize(id, endpoint)
    @id = id
    @endpoint = endpoint
  end

  def watch(directory)
    listener = Listen.to(directory, debug: true) do |modified, added, removed|
      publish_event(:modified, modified)
      publish_event(:added, added)
      publish_event(:removed, removed)
    end

    listener.start
    sleep
  end

  def scan(directory)
    Dir["**/**/*"].each do |file|
      next unless File.file?(file)
      url = "#{endpoint}/agents/#{id}/files/#{fingerprint_for(file)}"
      body = {
        path: File.expand_path(file)
      }
      response = Typhoeus.get(url, body: body)
      body = JSON.parse(response.body)
      puts body.inspect
      case body["state"]
      when "malicious"
        publish_event(:quarantined, [file])
      when "unknown"
        puts "file is unknown"
      end
    end
  end

  private

  def publish_event(event, files)
    files.each do |file|
      fingerprint = fingerprint_for(file)
      url = "#{endpoint}/agents/#{id}/events/"
      body = {
        event: {
          agent_id: id,
          name: event,
          data: {
            fingerprint: fingerprint,
            path: file,
            hostname: Socket.gethostname,
            ip_addresses: ip_addresses,
          }
        }
      }
      puts [url, body].inspect
      Typhoeus.post(url, body: body)
    end
  rescue => e
    puts "#{e.message} #{e.backtrace.join(' ')}"
  end

  def fingerprint_for(file)
    return nil unless File.exist?(file)
    result = `shasum -a 256 #{file}`
    sha, * = result.split(' ')
    sha
  end

  def ip_addresses
    Socket.ip_address_list.find_all { |x| x.ipv4? }.map { |x| x.ip_address }
  end
end