Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/core/auxiliary/fuzzer.rb
Views: 11784
# -*- coding: binary -*-1module Msf23###4#5# This module provides methods useful for developing fuzzers6#7###8module Auxiliary::Fuzzer91011def initialize(info = {})12super13register_advanced_options([14OptString.new('FuzzTracer', [ true, 'Sets the magic string to embed into fuzzer string inputs', 'MSFROCKS']),15OptString.new('FuzzChar', [ true, 'Sets the character to use for generating long strings', 'X'])16], Msf::Auxiliary::Fuzzer)17end181920# Will return or yield numbers based on the presence of a block.21#22# @return [Array<Array>] Returns an array of arrays of numbers if there is no block given23# @yield [Array<Integer>] Yields an array of numbers if there is a block given24# @see #fuzzer_number_power22526def fuzz_numbers27res = []28self.methods.sort.grep(/^fuzzer_number/).each do |m|29@last_fuzzer_input = m30block_given? ? self.send(m) {|x| yield(x) } : (res << self.send(m))31end32res33end343536# Will return or yield a string based on the presence of a block37#38# @return [Array] Returns and array of arrays of strings if there is no block given39# @yield [Array] Yields array of strings if there is a block given4041def fuzz_strings42res = []43self.methods.sort.grep(/^fuzzer_string/).each do |m|44@last_fuzzer_input = m45block_given? ? self.send(m) {|x| yield(x) } : (res << self.send(m))46end47res48end4950# Modifies each byte of the string from beginning to end, packing each element as an 8 bit character.51#52# @param str [String] The string the mutation will be based on.53# @param max [Integer, NilClass] Max string size.54# @return [Array] Returns an array of an array of strings55# @see #fuzzer_string_format5657def fuzz_string_corrupt_byte(str,max=nil)58res = []590.upto(max ? [max,str.length-1].min : (str.length - 1)) do |offset|600.upto(255) do |val|61@last_fuzzer_input = "fuzz_string_corrupt_byte offset:#{offset}/#{str.length} byte:#{val}"62buf = str.dup63buf[offset,1] = [val].pack('C')64block_given? ? yield(buf) : (res << buf)65end66end67res68end6970# Modifies each byte of the string from beginning to end, packing each element as an 8 bit character.71#72# @param str [String] The string the mutation will be based on.73# @param max [Integer, NilClass] Max string size.74# @return [Array] Returns an array of an array of strings75# @see fuzzer_string_format7677def fuzz_string_corrupt_byte_reverse(str,max=nil)78res = []79(max ? [max,str.length-1].min : (str.length - 1)).downto(0) do |offset|800.upto(255) do |val|81@last_fuzzer_input = "fuzz_string_corrupt_byte_reverse offset:#{offset}/#{str.length} byte:#{val}"82buf = str.dup83buf[offset,1] = [val].pack('C')84block_given? ? yield(buf) : (res << buf)85end86end87res88end8990# Useful generators (many derived from AxMan)91#92# @return [Array] Returns and array of strings.9394def fuzzer_string_format95res = %W{ %s %p %n %x %@ %.257d %.65537d %.2147483648d %.257f %.65537f %.2147483648f}96block_given? ? res.each { |n| yield(n) } : res97end9899# Reserved filename array100# Useful generators (many derived from AxMan)101#102# @return [Array] Returns and array of reserved filenames in Windows.103104def fuzzer_string_filepath_dos105res = %W{ aux con nul com1 com2 com3 com4 lpt1 lpt2 lp3 lpt4 prn }106block_given? ? res.each { |n| yield(n) } : res107end108109# Fuzzer Numbers by Powers of Two110#111# @return [Array] Returns an array with pre-set values112113def fuzzer_number_power2114res = [1150x100000000,1160x80000000,1170x40000000,1180x20000000,1190x10000000,1200x01000000,1210x00100000,1220x00010000,1230x00001000,1240x00000100,1250x00000010,1260x00000001127]128block_given? ? res.each { |n| yield(n) } : res129end130131# Powers of two by some fuzzing factor.132#133# @return [Array] Returns and array of integers.134135def fuzzer_number_power2_plus136res = []137fuzzer_number_power2 do |num|138res << num + 1139res << num + 2140res << num - 1141res << num - 2142res << num * -1143res << (num + 1) * -1144res << (num + 2) * -1145end146block_given? ? res.each { |n| yield(n) } : res147end148149# Generates a fuzz string If no block is set, it will retrieve characters from the150# FuzzChar datastore option.151#152# @param len [Integer] String size.153# @return [String] Returns a string of size 1024 * 512 specified by the user154155def fuzzer_gen_string(len)156@gen_string_block ||= datastore['FuzzChar'][0,1] * (1024 * 512)157res = ''158while (res.length < len)159res += @gen_string_block160end161res[0,len]162end163164# Creates a smaller fuzz string starting from length 16 -> 512 bytes long165#166# @return [Array] Returns an array of characters167def fuzzer_string_small168res = []16916.step(512,16) do |len|170buf = fuzzer_gen_string(len)171block_given? ? yield(buf) : (res << buf)172end173res174end175176# Creates a longer fuzz string from length 64 -> 8192 bytes long177#178# @return [Array] Returns an array of characters179def fuzzer_string_long180res = []18164.step(8192,64) do |len|182buf = fuzzer_gen_string(len)183buf[len / 2, datastore['FuzzTracer'].length] = datastore['FuzzTracer']184block_given? ? yield(buf) : (res << buf)185end186res187end188189# Creates a giant fuzz string from length 512 -> 131,064 bytes long190#191# @return [Array] Returns an array of characters192def fuzzer_string_giant193res = []194512.step(65532 * 2, 512) do |len|195buf = fuzzer_gen_string(len)196buf[len / 2, datastore['FuzzTracer'].length] = datastore['FuzzTracer']197block_given? ? yield(buf) : (res << buf)198end199res200end201202# Various URI types203#204# @return [Array] Returns an array of strings205def fuzzer_string_uri_types206res = %W{207aaa aaas about acap adiumxtra afp aim apt aw bolo callto cap chrome cid208content crid cvs data dav designates dict disk dns doi ed2k example examples209fax feed file finger fish ftp gg gizmoproject go gopher h323 hcp http https210iax2 icap im imap info ipp irc ircs iris iris.beep iris.lws iris.xpc iris.xpcs211itms jar javascript keyparc lastfm ldap ldaps lsid magnet mailto mid mms modem212ms-help msnim msrp msrps mtqp mupdate mvn news nfs nntp notes opaquelocktoken213over pop pres prospero psyc res rlogin rmi rsync rtsp secondlife service sftp214sgn shell shttp sip sips skype smb sms snews snmp soap.beep soap.beeps soldat215ssh steam svn tag teamspeak tel telephone telnet tftp thismessage tip tv unreal216urn ut2004 vbscript vemmi ventrilo view-source wais webcal worldwind wtai wyciwyg217wysiwyg xfire xmlrpc.beep xmpp xri ymsgr z39.50r z39.50s218}219block_given? ? res.each { |n| yield(n) } : res220end221222# Generator for common URI dividers223#224# @return [Array] Returns an array of strings225226def fuzzer_string_uri_dividers227res = %W{ : :// }228block_given? ? res.each { |n| yield(n) } : res229end230231# Generator for common path prefixes232#233# @return [Array] Returns an array of strings234235def fuzzer_string_path_prefixes236res = %W{ C:\\ \\\\localhost\\ / }237block_given? ? res.each { |n| yield(n) } : res238end239240# Generates various small URI string types241#242# @return [Array] Returns an array of strings243244def fuzzer_string_uris_small245res = []246fuzzer_string_uri_types do |proto|247fuzzer_string_uri_dividers do |div|248fuzzer_string_small do |str|249buf = proto + div + str250block_given? ? yield(buf) : (res << buf)251end252end253end254res255end256257# Generates various long URI string types258#259# @return [Array] Returns an array of strings260261def fuzzer_string_uris_long262res = []263fuzzer_string_uri_types do |proto|264fuzzer_string_uri_dividers do |div|265fuzzer_string_long do |str|266buf = proto + div + str267block_given? ? yield(buf) : (res << buf)268end269end270end271res272end273274# Generates various giant URI string types275#276# @return [Array] Returns an array of strings277278def fuzzer_string_uris_giant279res = []280fuzzer_string_uri_types do |proto|281fuzzer_string_uri_dividers do |div|282fuzzer_string_giant do |str|283buf = proto + div + str284block_given? ? yield(buf) : (res << buf)285end286end287end288res289end290291# Format for the URI string generator292#293# @return [Array] Returns an array of strings294295def fuzzer_string_uris_format296res = []297fuzzer_string_uri_types do |proto|298fuzzer_string_uri_dividers do |div|299fuzzer_string_format do |str|300buf = proto + div + str301block_given? ? yield(buf) : (res << buf)302end303end304end305res306end307308309# Generates various small strings310#311# @return [Array] Returns an array of strings312313def fuzzer_string_uris_dos314res = []315fuzzer_string_uri_types do |proto|316fuzzer_string_uri_dividers do |div|317fuzzer_string_filepath_dos do |str|318buf = proto + div + str319block_given? ? yield(buf) : (res << buf)320end321end322end323res324end325326327# Generates various small strings328#329# @return [Array] Returns an array of strings330331def fuzzer_string_paths_small332res = []333fuzzer_string_path_prefixes do |pre|334fuzzer_string_small do |str|335buf = pre + str336block_given? ? yield(buf) : (res << buf)337end338end339res340end341342343# Generates various small strings344#345# @return [Array] Returns an array of strings346347def fuzzer_string_paths_long348res = []349fuzzer_string_path_prefixes do |pre|350fuzzer_string_long do |str|351buf = pre + str352block_given? ? yield(buf) : (res << buf)353end354end355res356end357358359# Generates various giant strings360#361# @return [Array] Returns an array of strings362363def fuzzer_string_paths_giant364res = []365fuzzer_string_path_prefixes do |pre|366fuzzer_string_giant do |str|367buf = pre + str368block_given? ? yield(buf) : (res << buf)369end370end371res372end373374375# Format for the path generator376#377# @return [Array] Returns an array of strings378379def fuzzer_string_paths_format380res = []381fuzzer_string_path_prefixes do |pre|382fuzzer_string_format do |str|383buf = pre + str384block_given? ? yield(buf) : (res << buf)385end386end387res388end389390391# Generates fuzzer strings using path prefixes392#393# @return [Array] Returns an array of strings394395def fuzzer_string_paths_dos396res = []397fuzzer_string_path_prefixes do |pre|398fuzzer_string_filepath_dos do |str|399buf = pre + str400block_given? ? yield(buf) : (res << buf)401end402end403res404end405406end407end408409410