1
+
2
+ module Msf
3
+ module Exploit ::AutoTarget
4
+
5
+ # Checks to see if the auto-generated Automatic Targeting
6
+ # has been selected. If the module had an already defined
7
+ # Automatic target, then we let the module handle the targeting
8
+ # itself.
9
+ #
10
+ # @return [Boolean] whether or not to use our automatic targeting routine
11
+ def auto_target?
12
+ selected_target = targets [ target_index ]
13
+ return false if selected_target . nil?
14
+ if selected_target . name =~ /Automatic/ && selected_target [ 'AutoGenerated' ] == true
15
+ true
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ # Returns the Target Index of the automatically selected Target from
22
+ # our Automatic Targeting routine.
23
+ #
24
+ # @return [Integer] the index of the selected Target
25
+ # @return [nil] if no target could be selected
26
+ def auto_targeted_index
27
+ selected_target = select_target
28
+ return nil if selected_target . nil?
29
+ targets . each_with_index do |target , index |
30
+ return index if target == selected_target
31
+ end
32
+ nil
33
+ end
34
+
35
+ # Chooses the best possible Target for what we know about
36
+ # the targeted host.
37
+ #
38
+ # @return [Msf::Module::Target] the Target that our automatic routine selected
39
+ def select_target
40
+ return nil unless auto_target?
41
+ host_record = target_host
42
+ return nil if host_record . nil?
43
+ filtered_targets = filter_by_os ( host_record )
44
+ filtered_targets . first
45
+ end
46
+
47
+ # Finds an <Mdm::Host> for the RHOST if one exists
48
+ #
49
+ # @return [Mdm:Host] the Host record if one exists
50
+ # @return [nil] if no Host record is present, or the DB is not active
51
+ def target_host
52
+ return nil unless self . respond_to? ( :rhost )
53
+ return nil unless framework . db . active
54
+ current_workspace = framework . db . find_workspace ( self . workspace )
55
+ current_workspace . hosts . where ( address : rhost ) . first
56
+ end
57
+
58
+ # Returns the best matching Targets based on the target host's
59
+ # OS information. It looks at the OS Family, OS Name, and OS SP.
60
+ #
61
+ # @param host_record [Mdm::Host] the target host record
62
+ # @return [Array<Msf::Module::Target>] an array of matching targets
63
+ def filter_by_os ( host_record )
64
+ filtered_by_family = filter_by_os_family ( host_record )
65
+ filtered_by_name = filter_by_os_name ( filtered_by_family , host_record )
66
+ # If Filtering by name gave us no results, then we reset back to the family filter group
67
+ filtered_by_name = filtered_by_family if filtered_by_name . empty?
68
+ filtered_by_sp = filter_by_os_sp ( filtered_by_name , host_record )
69
+ # If Filtering by SP was a bust, revert back one level
70
+ filtered_by_sp = filtered_by_name if filtered_by_sp . empty?
71
+ filtered_by_sp
72
+ end
73
+
74
+ # Returns all Targets that match the target host's OS Family
75
+ # e.g Windows, Linux, OS X, etc
76
+ #
77
+ # @param host_record [Mdm::Host] the target host record
78
+ # @return [Array<Msf::Module::Target>] an array of matching targets
79
+ def filter_by_os_family ( host_record )
80
+ return [ ] if host_record . os_family . blank?
81
+ filtered_targets = targets . collect do |target |
82
+ if target . name =~ /#{ host_record . os_family } /
83
+ target
84
+ else
85
+ nil
86
+ end
87
+ end
88
+ filtered_targets . compact
89
+ end
90
+
91
+ # Returns all Targets that match the target host's OS Name
92
+ # e.g Windows 7, Windows XP, Windows Vista, etc
93
+ #
94
+ # @param potential_targets [Array<Msf::Module::Target>] the filtered targets that we wish to filter further
95
+ # @param host_record [Mdm::Host] the target host record
96
+ # @return [Array<Msf::Module::Target>] an array of matching targets
97
+ def filter_by_os_name ( potential_targets , host_record )
98
+ return [ ] if host_record . os_name . blank?
99
+ filtered_targets = [ ]
100
+ potential_targets . each do |target |
101
+ filtered_targets << target if target . name =~ /#{ host_record . os_name } /
102
+ end
103
+ filtered_targets
104
+ end
105
+
106
+ # Returns all Targets that match the target host's OS SP
107
+ #
108
+ # @param potential_targets [Array<Msf::Module::Target>] the filtered targets that we wish to filter further
109
+ # @param host_record [Mdm::Host] the target host record
110
+ # @return [Array<Msf::Module::Target>] an array of matching targets
111
+ def filter_by_os_sp ( potential_targets , host_record )
112
+ return [ ] if host_record . os_sp . blank?
113
+ filtered_targets = [ ]
114
+ potential_targets . each do |target |
115
+ filtered_targets << target if target . name =~ /#{ host_record . os_sp } /
116
+ end
117
+ filtered_targets
118
+ end
119
+ end
120
+ end
0 commit comments