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
|
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)}"
response = Typhoeus.get(url)
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,
full_path: file,
}
}
}
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
end
|