Class: EvilFaker::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/evil_faker/base.rb

Overview

Base class for every namespace: reads its corpus from data/groups and exposes .all/.sample, plus .subtype for namespaces split into sub-groups.

Constant Summary collapse

DATA_DIR =
File.expand_path("data/groups", __dir__)

Class Method Summary collapse

Class Method Details

.allObject



29
30
31
32
33
34
35
# File 'lib/evil_faker/base.rb', line 29

def all
  return @all ||= @subtypes.flat_map { |s| send(s) } if @subtypes

  @all ||= Dir.glob(File.join(DATA_DIR, "*", "#{@group}.txt")).sort.flat_map do |path|
    File.readlines(path, chomp: true)
  end
end

.group(name) ⇒ Object

Declares this namespace's corpus, read from groups//.txt.



11
12
13
# File 'lib/evil_faker/base.rb', line 11

def group(name)
  @group = name
end

.sample(count = nil) ⇒ Object



37
38
39
40
41
# File 'lib/evil_faker/base.rb', line 37

def sample(count = nil)
  return all.sample(count) if count

  all.sample
end

.subtype(name) ⇒ Object

Declares a sub-group living in groups///.txt. Defines a method name that reads and concatenates it across every source that has a file at that path (multiple sources can contribute strings to the same subtype, e.g. blns/injection/sql.txt and payloads_all_the_things/injection/sql.txt both feed Injection.sql).



20
21
22
23
24
25
26
27
# File 'lib/evil_faker/base.rb', line 20

def subtype(name)
  (@subtypes ||= []) << name
  define_singleton_method(name) do
    subtype_cache[name] ||= Dir.glob(
      File.join(DATA_DIR, "*", @group.to_s, "#{name}.txt")
    ).sort.flat_map { |path| File.readlines(path, chomp: true) }
  end
end