Module: PWN::ModuleSkills

Defined in:
lib/pwn/module_skills.rb

Overview

Generate and install one skill per PWN module, mirroring the constant path under etc/default_skills/pwn (e.g. PWN::Plugins::TransparentBrowser → pwn/plugins/transparent_browser/SKILL.md). refresh! rewrites the gem tree when a module changes. install overwrites the operator copy so pwn setup --migrate stays current.

Constant Summary collapse

LIB_ROOT =
__dir__
GEM_SKILLS =
File.expand_path('../../etc/default_skills/pwn', __dir__)
ARTIFACT_RX =
%r{
  -----BEGIN\ [A-Z ]*PRIVATE\ KEY----- |
  Bearer\s+[A-Za-z0-9\-._~+/]+=* |
  \beyJ[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+ |
  \b(?:sk|rk|pk|xai|xox[baprs]|ghp|gho|ghu|ghs|ghr|glpat|AKIA|ASIA|ya29)[-_][A-Za-z0-9\-_]{8,} |
  «redacted:[^»]*» |
  \#\{[^\}]+\} |
  /home/[A-Za-z0-9._-]+
}ix

Class Method Summary collapse

Class Method Details

.authorsObject



364
365
366
# File 'lib/pwn/module_skills.rb', line 364

public_class_method def self.authors
  "AUTHOR(S):\n  0day Inc. <support@0dayinc.com>\n"
end

.enumerate(opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pwn/module_skills.rb', line 15

public_class_method def self.enumerate(opts = {})
  lib = opts[:lib_root].to_s
  lib = File.expand_path('..', LIB_ROOT) if lib.empty?
  found = []
  paths = [File.join(lib, 'pwn.rb')] + Dir.glob(File.join(lib, 'pwn', '**', '*.rb'))
  paths.uniq.each do |path|
    next unless File.file?(path)

    found.concat(parse_file(path: path, lib_root: lib))
  end
  found.uniq { |h| h[:source] }
end

.helpObject



368
369
370
371
372
373
374
375
376
# File 'lib/pwn/module_skills.rb', line 368

public_class_method def self.help
  puts <<~USAGE
    USAGE:
      #{self}.enumerate
      #{self}.refresh!
      #{self}.install(pwn_skills_path: '~/.pwn/skills')
      #{self}.authors
  USAGE
end

.install(opts = {}) ⇒ Object



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
# File 'lib/pwn/module_skills.rb', line 135

public_class_method def self.install(opts = {})
  root = opts[:pwn_skills_path]
  root = PWN::Config.pwn_skills_path if root.to_s.empty? && defined?(PWN::Config)
  src = opts[:source].to_s
  src = GEM_SKILLS if src.empty?
  return [] unless root.to_s != '' && Dir.exist?(src)

  dest_root = File.join(root, 'pwn')
  FileUtils.mkdir_p(dest_root)
  copied = []
  keep = {}
  froms = Dir.glob(File.join(src, 'SKILL.md'))
  froms.concat(Dir.glob(File.join(src, '**', 'SKILL.md')))
  froms.concat(Dir.glob(File.join(src, '**', 'references', '*.md')))
  froms.uniq.each do |from|
    next unless File.file?(from)

    rel = from.delete_prefix("#{src}/")
    to = File.join(dest_root, rel)
    FileUtils.mkdir_p(File.dirname(to))
    body = File.read(from)
    prev = File.file?(to) ? File.read(to) : nil
    if prev != body
      File.write(to, body)
      copied << { name: "pwn/#{File.dirname(rel)}", path: to } if File.basename(from) == 'SKILL.md'
    end
    keep[to] = true
  end
  prune_orphans(root: dest_root, keep: keep)
  copied
rescue StandardError => e
  warn "[PWN::ModuleSkills] install failed: #{e.class}: #{e.message}"
  []
end

.refresh!(opts = {}) ⇒ Object



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
# File 'lib/pwn/module_skills.rb', line 104

public_class_method def self.refresh!(opts = {})
  dest = opts[:dest].to_s
  dest = File.expand_path('../../etc/default_skills', __dir__) if dest.empty?
  dest = File.dirname(dest) if File.basename(dest) == 'pwn'
  lib = opts[:lib_root].to_s
  lib = File.expand_path('..', LIB_ROOT) if lib.empty?
  mods = enumerate(lib_root: lib)
  FileUtils.mkdir_p(File.join(dest, 'pwn'))
  written = []
  keep = {}
  mods.each do |rec|
    skill_path = skill_abs(dest: dest, module: rec)
    dir = File.dirname(skill_path)
    FileUtils.mkdir_p(dir)
    keep[skill_path] = true
    if preserved_skill?(path: skill_path)
      Dir.glob(File.join(dir, 'references', '*.md')).each { |p| keep[p] = true }
    else
      body = render(module: rec)
      prev = File.file?(skill_path) ? File.read(skill_path) : nil
      if prev != body
        File.write(skill_path, body)
        written << skill_path
      end
      write_references(dir: dir, rec: rec, keep: keep, written: written)
    end
  end
  removed = prune_orphans(root: File.join(dest, 'pwn'), keep: keep)
  { modules: mods.length, written: written, removed: removed, dest: dest }
end

.relpath(opts = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/pwn/module_skills.rb', line 28

public_class_method def self.relpath(opts = {})
  src = opts[:source].to_s.sub(/\.rb\z/, '')
  return "#{src}/SKILL.md" unless src.empty?

  const = opts[:const].to_s
  parts = const.split('::').map { |p| snake(name: p) }
  parts.shift if parts.first == 'pwn'
  File.join('pwn', *parts, 'SKILL.md')
end

.render(opts = {}) ⇒ Object



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
# File 'lib/pwn/module_skills.rb', line 38

public_class_method def self.render(opts = {})
  rec = opts[:module] || opts
  const = rec[:const].to_s
  methods = Array(rec[:methods]).uniq
  src = rec[:source].to_s
  blurb = sanitize_text(text: rec[:blurb].to_s.gsub(/\s+/, ' ').strip)
  blurb = "Public API for #{const}." if blurb.empty?
  slug = const.downcase.tr(':', '-').gsub(/-+/, '-')
  list = methods.map { |m| "- `#{m}`" }
  list = ['- _(no public class methods parsed)_'] if list.empty?
  refs = Array(rec[:references])
  ref_block = if refs.empty?
                ''
              else
                items = refs.map { |r| "- `references/#{r[:file]}` — #{r[:title]}" }
                "\n## References\n\n#{items.join("\n")}\n"
              end
  <<~MD
    ---
    name: #{slug}
    description: Drive #{const} from pwn_eval.
    license: MIT
    allowed-tools: [pwn, pwn_eval]
    metadata:
      bundled: true
      generated: true
      module: #{const}
      source: #{src}
    ---

    # #{const}

    #{blurb}

    ## When to use

    Call `#{const}` from `pwn_eval` when the task needs this module.
    Do not reimplement it in shell.

    ## Methodologies

    Generated from `#{src}`. Prefer the public class methods below.
    Class methods take `(opts = {})` and read `opts`.

    ## How to call

    ```ruby
    #{const}.help
    #{example_call(const: const, methods: methods)}
    ```

    ## Public methods

    #{list.join("\n")}
    #{ref_block}
    ## Source

    `#{src}`

    ## Verification

    `#{const}.respond_to?(:#{methods.first || 'help'})` after the
    module is loaded. Read the source for parameter names.
  MD
end