forked from rapid7/metasploit-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform.rb
More file actions
694 lines (617 loc) · 13 KB
/
Copy pathplatform.rb
File metadata and controls
694 lines (617 loc) · 13 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
# -*- coding: binary -*-
require 'abbrev'
#
# This is the definitions of which Platforms the framework knows about. The
# relative ranks are used to support ranges, and the Short names are used to
# allow for more convenient specification of the platforms....
#
class Msf::Module::Platform
Rank = 0
# actually, having an argument of '' is what to do for wanting 'all'
Short = "all"
class << self
attr_accessor :full_name
end
#
# Returns the "real" name of the module instance, accounting for potentially
# aliased class names.
#
def self.realname
# Use the cached version if one has been set
return full_name if (full_name)
# Otherwise, generate it and cache it
names = []
c = Msf::Module::Platform
name.split('::')[3 .. -1].each { |part|
c = c.const_get(part)
if (c.const_defined?('RealName') == true)
names << c.const_get('RealName')
else
names << part
end
}
full_name = names.join(' ')
end
#
# Calls the class method.
#
def find_children
self.class.find_children
end
#
# The magic to try to build out a Platform from a string.
#
def self.find_platform(str)
# remove any whitespace and downcase
str = str.gsub(' ', '').downcase
# Match known platforms first, then fall back to string matching
case str
when 'windows', 'win'
return Msf::Module::Platform::Windows
when 'linux'
return Msf::Module::Platform::Linux
when 'unix'
return Msf::Module::Platform::Unix
when 'osx'
return Msf::Module::Platform::OSX
when 'solaris'
return Msf::Module::Platform::Solaris
when 'freebsd'
return Msf::Module::Platform::FreeBSD
when 'netware'
return Msf::Module::Platform::Netware
when 'bsd'
return Msf::Module::Platform::BSD
when 'openbsd'
return Msf::Module::Platform::OpenBSD
when 'android'
return Msf::Module::Platform::Android
when 'java'
return Msf::Module::Platform::Java
when 'r'
return Msf::Module::Platform::R
when 'ruby'
return Msf::Module::Platform::Ruby
when 'cisco'
return Msf::Module::Platform::Cisco
when 'juniper'
return Msf::Module::Platform::Juniper
when 'unifi'
return Msf::Module::Platform::Unifi
when 'brocade'
return Msf::Module::Platform::Brocade
when 'mikrotik'
return Msf::Module::Platform::Mikrotik
when 'arista'
return Msf::Module::Platform::Arista
when 'bsdi'
return Msf::Module::Platform::BSDi
when 'netbsd'
return Msf::Module::Platform::NetBSD
when 'aix'
return Msf::Module::Platform::AIX
when 'hpux'
return Msf::Module::Platform::HPUX
when 'irix'
return Msf::Module::Platform::Irix
when 'php'
return Msf::Module::Platform::PHP
when 'javascript'
return Msf::Module::Platform::JavaScript
when 'python'
return Msf::Module::Platform::Python
when 'nodejs'
return Msf::Module::Platform::NodeJS
when 'firefox'
return Msf::Module::Platform::Firefox
when 'mainframe'
return Msf::Module::Platform::Mainframe
when 'multi'
return Msf::Module::Platform::Multi
when 'hardware'
return Msf::Module::Platform::Hardware
when 'apple_ios'
return Msf::Module::Platform::Apple_iOS
when 'unknown'
return Msf::Module::Platform::Unknown
end
# Start at the base platform module
mod = ::Msf::Module::Platform
# Scan forward, trying to find the end module
while str.length > 0
mod, str = find_portion(mod, str)
end
return mod
end
#
# Finds all inherited children from a given module.
#
def self.find_children
@subclasses ||= []
@subclasses.sort_by { |a| a::Rank }
end
def self.inherited(subclass)
@subclasses ||= []
@subclasses << subclass
end
#
# Builds the abbreviation set for every module starting from
# a given point.
#
def self.build_child_platform_abbrev(mod)
# Flush out any non-class and non-inherited children
children = mod.find_children
# No children to speak of?
return if (children.length == 0)
# Build the list of names & rankings
names = {}
ranked = {}
children.map { |c|
name = c.name.split('::')[-1].downcase
# If the platform has an alias, such as a portion that may
# start with an integer, use that as the name
if (c.const_defined?('Alias'))
als = c.const_get('Alias').downcase
names[als] = c
ranked[als] = c::Rank
# If the platform has more than one alias, process the list
elsif (c.const_defined?('Aliases'))
c.const_get('Aliases').each { |a|
a = a.downcase
names[a] = c
ranked[a] = c::Rank
}
end
names[name] = c
ranked[name] = c::Rank
}
# Calculate their abbreviations
abbrev = ::Abbrev::abbrev(names.keys)
# Set the ranked list and abbreviated list on this module,
# then walk the children
mod.const_set('Abbrev', abbrev)
mod.const_set('Ranks', ranked)
mod.const_set('Names', names)
end
#
# Finds the module that best matches the supplied string (or a portion of
# the string).
#
def self.find_portion(mod, str)
# Check to see if we've built the abbreviated cache
if (not (
mod.const_defined?('Abbrev') and
mod.const_defined?('Names') and
mod.const_defined?('Ranks')
) )
build_child_platform_abbrev(mod)
end
if (not mod.const_defined?('Names'))
elog("Failed to instantiate the platform list for module #{mod}")
raise "Failed to instantiate the platform list for module #{mod}"
end
abbrev = mod.const_get('Abbrev')
names = mod.const_get('Names')
ranks = mod.const_get('Ranks')
best = nil
bestlen = 0
bestmat = nil
bestrank = 0
# Walk through each abbreviation
abbrev.each { |a|
# If the abbreviation is too long, no sense in scanning it
next if (a[0].length > str.length)
# If the current abbreviation matches with the
# supplied string and is better than the previous
# best match length, use it, but only if it also
# has a higher rank than the previous match.
if ((a[0] == str[0, a[0].length]) and
(a[0].length > bestlen) and
(bestrank == nil or bestrank <= ranks[a[1]]))
best = [ names[a[1]], str[a[0].length .. -1] ]
bestlen = a[0].length
bestmat = a[0]
bestrank = ranks[a[1]]
end
}
# If we couldn't find a best match at this stage, it's time to warn.
if (best == nil)
raise ArgumentError, "No classes in #{mod} for #{str}!", caller
end
return best
end
private_class_method :build_child_platform_abbrev # :nodoc:
private_class_method :find_portion # :nodoc:
##
#
# Builtin platforms
#
##
#
# Unknown
#
# This is a special case for when we're completely unsure of the
# platform, such as a crash or default case in code. Only
# utilize this as a catch-all.
#
class Unknown < Msf::Module::Platform
Rank = 0 # safeguard with 0 since the platform is completely unknown
Alias = "unknown"
end
#
# Windows
#
class Windows < Msf::Module::Platform
Rank = 100
# Windows 95
class W95 < Windows
Rank = 100
Alias = "95"
RealName = "95"
end
# Windows 98
class W98 < Windows
Rank = 100
Alias = "98"
RealName = "98"
class FE < W98
Rank = 100
end
class SE < W98
Rank = 200
end
end
# Windows ME
class ME < Windows
Rank = 100
end
# Windows NT
class NT < Windows
Rank = 100
class SP0 < NT
Rank = 100
end
class SP1 < NT
Rank = 200
end
class SP2 < NT
Rank = 300
end
class SP3 < NT
Rank = 400
end
class SP4 < NT
Rank = 500
end
class SP5 < NT
Rank = 600
end
class SP6 < NT
Rank = 700
end
class SP6a < NT
Rank = 800
end
end
# Windows 2000
class W2000 < Windows
Rank = 200
Aliases = [ "2000", "2K" ]
RealName = "2000"
class SP0 < W2000
Aliases = [ "sp0-4", "sp0-sp4" ]
Rank = 100
end
class SP1 < W2000
Rank = 200
end
class SP2 < W2000
Rank = 300
end
class SP3 < W2000
Rank = 400
end
class SP4 < W2000
Rank = 500
end
end
# Windows XP
class XP < Windows
Rank = 300
class SP0 < XP
# It's not clear whether this should be assigned to the lower
# or higher bound of the range.
Aliases = [ "sp0-1", "sp0/1", "sp0-sp1", "sp0/sp1", "sp0-2", "sp0-sp2", "sp0-3", "sp0-sp3" ]
Rank = 100
end
class SP1 < XP
Rank = 200
end
class SP2 < XP
Rank = 300
end
class SP3 < XP
Rank = 400
end
end
# Windows 2003 Server
class W2003 < Windows
Rank = 400
Aliases = [ "2003", "2003 Server", "2K3" ]
RealName = "2003"
class SP0 < W2003
Rank = 100
end
class SP1 < W2003
Rank = 200
end
end
class Vista < Windows
Rank = 500
class SP0 < Vista
Aliases = [ "sp0-1", "sp0/1", "sp0-sp1", "sp0/sp1" ]
Rank = 100
end
class SP1 < Vista
Rank = 200
end
end
class W7 < Windows
Rank = 600
RealName = "7"
end
class W8 < Windows
Rank = 700
RealName = "8"
end
end
#
# NetWare
#
class Netware < Msf::Module::Platform
Rank = 100
Alias = "netware"
end
#
# Android
#
class Android < Msf::Module::Platform
Rank = 100
Alias = "android"
end
#
# Java
#
class Java < Msf::Module::Platform
Rank = 100
Alias = "java"
end
#
# R
#
class R < Msf::Module::Platform
Rank = 100
Alias = "r"
end
#
# Ruby
#
class Ruby < Msf::Module::Platform
Rank = 100
Alias = "ruby"
end
#
# Linux
#
class Linux < Msf::Module::Platform
Rank = 100
Alias = "linux"
end
#
# Cisco
#
class Cisco < Msf::Module::Platform
Rank = 100
Alias = "cisco"
end
#
# Juniper
#
class Juniper < Msf::Module::Platform
Rank = 100
Alias = "juniper"
end
#
# Ubiquiti Unifi
#
class Unifi < Msf::Module::Platform
Rank = 100
Alias = "unifi"
end
#
# Brocade
#
class Brocade < Msf::Module::Platform
Rank = 100
Alias = "brocade"
end
#
# MikroTik
#
class Mikrotik < Msf::Module::Platform
Rank = 100
Alias = "mikrotik"
end
#
# Arista
#
class Arista < Msf::Module::Platform
Rank = 100
Alias = "arista"
end
#
# Solaris
#
class Solaris < Msf::Module::Platform
Rank = 100
class V4
Rank = 100
Alias = "4"
end
class V5
Rank = 200
Alias = "5"
end
class V6
Rank = 300
Alias = "6"
end
class V7
Rank = 400
Alias = "7"
end
class V8
Rank = 500
Alias = "8"
end
class V9
Rank = 600
Alias = "9"
end
class V10
Rank = 700
Alias = "10"
end
class V11
Rank = 800
Alias = "11"
end
end
#
# OSX
#
class OSX < Msf::Module::Platform
Rank = 100
end
#
# Generic BSD
#
class BSD < Msf::Module::Platform
Rank = 100
end
#
# OpenBSD
#
class OpenBSD < Msf::Module::Platform
Rank = 100
end
#
# BSDi
#
class BSDi < Msf::Module::Platform
Rank = 100
end
#
# NetBSD
#
class NetBSD < Msf::Module::Platform
Rank = 100
end
#
# FreeBSD
#
class FreeBSD < Msf::Module::Platform
Rank = 100
end
#
# AIX
#
class AIX < Msf::Module::Platform
Rank = 100
Alias = "aix"
end
#
# HP-UX
#
class HPUX < Msf::Module::Platform
Rank = 100
Alias = "hpux"
end
#
# Irix
#
class Irix < Msf::Module::Platform
Rank = 100
Alias = "irix"
end
#
# Generic Unix
#
class Unix < Msf::Module::Platform
Rank = 100
Alias = "unix"
end
#
# Generic PHP
#
class PHP < Msf::Module::Platform
Rank = 100
Alias = "php"
end
#
# JavaScript
#
class JavaScript < Msf::Module::Platform
Rank = 100
Alias = "js"
end
#
# Python
#
class Python < Msf::Module::Platform
Rank = 100
Alias = "python"
end
#
# Node.js
#
class NodeJS < Msf::Module::Platform
Rank = 100
Alias = "nodejs"
end
#
# Firefox
#
class Firefox < Msf::Module::Platform
Rank = 100
Alias = "firefox"
end
#
# Mainframe
#
class Mainframe < Msf::Module::Platform
Rank = 100
Alias = "mainframe"
end
#
# Multi (for wildcard-style platform functions)
#
class Multi < Msf::Module::Platform
Rank = 100
Alias = "multi"
end
#
# Hardware
#
class Hardware < Msf::Module::Platform
Rank = 100
Alias = "hardware"
end
#
# Apple iOS
#
class Apple_iOS < Msf::Module::Platform
Rank = 100
Alias = "apple_ios"
end
end