Changeset 5105

Show
Ignore:
Timestamp:
09/13/07 11:15:56 (1 year ago)
Author:
hdm
Message:

Support pattern_create() lengths larger than the maximum set size

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • framework3/tags/framework-3.0/lib/rex/text.rb

    r4883 r5105  
    210210        #  
    211211        def self.to_unicode(str='', type = 'utf-16le', mode = '', size = '') 
     212                return '' if not str 
    212213                case type  
    213214                when 'utf-16le' 
     
    329330                        } 
    330331                        return string 
     332                when 'uhwtfms-half' # suggested name from HD :P 
     333                        load_codepage() 
     334                        string = '' 
     335                        # overloading mode as codepage 
     336                        if mode == '' 
     337                                mode = 1252 # ANSI - Latan 1, default for US installs of MS products 
     338                        else 
     339                raise TypeError, "Invalid codepage #{mode}, only 1252 supported for uhwtfms_half" 
     340                        end 
     341                        str.each_byte {|byte| 
     342                if ((byte >= 33 && byte <= 63) || (byte >= 96 && byte <= 126)) 
     343                    string += "\xFF" + [byte ^ 32].pack('C') 
     344                elsif (byte >= 64 && byte <= 95) 
     345                    string += "\xFF" + [byte ^ 96].pack('C') 
     346                else 
     347                    char = [byte].pack('C') 
     348                                        possible = $codepage_map_cache[mode]['data'][char] 
     349                                        if possible.nil? 
     350                                                raise TypeError, "codepage #{mode} does not provide an encoding for 0x#{char.unpack('H*')[0]}" 
     351                                        end 
     352                                        string += possible[ rand(possible.length) ] 
     353                end 
     354                        } 
     355                        return string 
    331356                else  
    332357                        raise TypeError, 'invalid utf type' 
     
    372397                        end 
    373398                        return res               
     399        when 'u-half' 
     400            return str.gsub(all) { |s| Rex::Text.to_hex(Rex::Text.to_unicode(s, 'uhwtfms-half'), '%u', 2) } 
    374401        else 
    375402            raise TypeError, 'invalid mode' 
     
    428455                         
    429456                        chunk.unpack("C*").each do |c| 
    430                                 if (c > 0x19 and c < 0x80) 
     457                                if (c > 0x1f and c < 0x80) 
    431458                                        buf << c.chr 
    432459                                else 
     
    631658        end 
    632659 
     660        # Generate random bytes of numeric data 
     661        def self.rand_text_numeric(len, bad='') 
     662                foo = ('0' .. '9').to_a 
     663                rand_base(len, bad, *foo ) 
     664        end 
     665         
    633666        # Generate random bytes of english-like data 
    634667        def self.rand_text_english(len, bad='') 
     
    658691                        end 
    659692                end 
    660  
     693                 
     694                # Maximum permutations reached, but we need more data 
     695                if (buf.length < length) 
     696                        buf = buf * (length / buf.length.to_f).ceil 
     697                end 
     698                 
    661699                buf[0,length] 
    662700        end