forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ips_filter.rb
119 lines (96 loc) · 2.4 KB
/
ips_filter.rb
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#
# $Id$
# $Revision$
#
module Msf
###
#
# This class hooks all sockets created by a running exploit
# and prevents data from being sent that matches a known IPS
# signature.
#
###
class Plugin::IPSFilter < Msf::Plugin
###
#
# This class implements a socket communication logger
#
###
class IPSSocketEventHandler
include Rex::Socket::Comm::Events
def on_before_socket_create(comm, param)
end
def on_socket_created(comm, sock, param)
# Sockets created by the exploit have MsfExploit set and MsfPayload not set
if (param.context['MsfExploit'] and (! param.context['MsfPayload'] ))
sock.extend(IPSFilter::SocketTracer)
sock.context = param.context
end
end
end
def initialize(framework, opts)
super
@ips_eh = IPSSocketEventHandler.new
Rex::Socket::Comm::Local.register_event_handler(@ips_eh)
end
def cleanup
Rex::Socket::Comm::Local.deregister_event_handler(@ips_eh)
end
def name
"ips_filter"
end
def desc
"Scans all outgoing data to see if it matches a known IPS signature"
end
protected
end
end
# This module extends the captured socket instance
module IPSFilter
module SocketTracer
attr_accessor :context
# Hook the write method
def write(buf, opts = {})
if (ips_match(buf))
print_error "Outbound write blocked due to possible signature match"
return 0
end
super(buf, opts)
end
# Hook the read method
def read(length = nil, opts = {})
r = super(length, opts)
if (ips_match(r))
print_error "Incoming read may match a known signature"
end
return r
end
def close(*args)
super(*args)
end
def ips_match(data)
lp = localport
rp = peerport
SIGS.each do |s|
begin
r = Regexp.new(s[1])
if (data.match(r))
print_error "Matched IPS signature #{s[0]}"
return true
end
rescue ::Exception => e
print_error "Compiled error: #{s[1]}"
end
end
return false
end
# Extend this as needed :-)
SIGS =
[
['DCOM.C', ".*\\\x5c\x00\\\x5c\x00\x46\x00\x58\x00\x4e\x00\x42\x00\x46\x00\x58\x00\x46\x00\x58\x00.*\xcc\xe0\xfd\x7f.*"],
['BLASTER', ".*\\\x5c\x00\\\x5c\x00\x46\x00\x58\x00\x4e\x00\x42\x00\x46\x00\x58\x00\x46\x00\x58\x00.*\xcc\xe0\xfd\x7f.*"],
['REMACT', ".*\xb8\x4a\x9f\x4d\x1c\\}\xcf\x11\x86\x1e\x00\x20\xaf\x6e.*"],
['x86 NOP SLED', "\x90\x90"],
]
end
end