Module: PWN::AI::ModuleSkills
- Defined in:
- lib/pwn/ai/module_skills.rb
Overview
Generate and install one skill per PWN module, mirroring the constant
path under etc/default_skills/pwn (e.g. PWN::HTTP::TransparentBrowser
→ pwn/http/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 =
File.('..', __dir__)
- GEM_SKILLS =
File.('../../../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
- .authors ⇒ Object
- .drift(opts = {}) ⇒ Object
- .enumerate(opts = {}) ⇒ Object
- .help ⇒ Object
- .install(opts = {}) ⇒ Object
- .refresh!(opts = {}) ⇒ Object
- .relpath(opts = {}) ⇒ Object
- .render(opts = {}) ⇒ Object
-
.sync_namespaces(opts = {}) ⇒ Object
Refresh the bundled skill tree when it has drifted, then copy it into the operator skills directory and drop paths that no longer match a current module.
Class Method Details
.authors ⇒ Object
461 462 463 |
# File 'lib/pwn/ai/module_skills.rb', line 461 public_class_method def self. "AUTHOR(S):\n 0day Inc. <[email protected]>\n" end |
.drift(opts = {}) ⇒ Object
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 |
# File 'lib/pwn/ai/module_skills.rb', line 138 public_class_method def self.drift(opts = {}) dest = opts[:dest].to_s dest = File.('../../../etc/default_skills', __dir__) if dest.empty? dest = File.dirname(dest) if File.basename(dest) == 'pwn' lib = opts[:lib_root].to_s lib = File.('..', LIB_ROOT) if lib.empty? mods = enumerate(lib_root: lib) missing = [] stale = [] keep = {} mods.each do |rec| path = skill_abs(dest: dest, module: rec) dir = File.dirname(path) keep[path] = true if preserved_skill?(path: path) Dir.glob(File.join(dir, 'references', '*.md')).each { |p| keep[p] = true } next end if File.file?(path) stale << path if File.read(path) != render(module: rec) else missing << path end Array(rec[:references]).each do |ref| keep[File.join(dir, 'references', ref[:file])] = true end end orphans = [] root = File.join(dest, 'pwn') if Dir.exist?(root) Dir.glob(File.join(root, '**', '*')).each do |path| next unless File.file?(path) next if File.basename(path).start_with?('.') next if keep[path] orphans << path end end { missing: missing, stale: stale, orphans: orphans, modules: mods.length } end |
.enumerate(opts = {}) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pwn/ai/module_skills.rb', line 16 public_class_method def self.enumerate(opts = {}) lib = opts[:lib_root].to_s lib = File.('..', 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] }.each { |rec| enrich_live_methods!(rec: rec) } end |
.help ⇒ Object
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 |
# File 'lib/pwn/ai/module_skills.rb', line 465 public_class_method def self.help puts "USAGE: # Run enumerate and return its result #{self}.enumerate( lib_root: 'optional - lib root value consumed by #enumerate' ) # Run relpath and return its result #{self}.relpath( source: 'optional - source value consumed by #relpath', const: 'optional - const value consumed by #relpath' ) # Run render and return its result #{self}.render( module: 'optional - module value consumed by #render (defaults to opts)' ) # Run refresh and return its result #{self}.refresh!( dest: 'required - dest value consumed by #refresh!', lib_root: 'optional - lib root value consumed by #refresh!' ) # Run drift and return its result #{self}.drift( dest: 'required - dest value consumed by #drift', lib_root: 'optional - lib root value consumed by #drift' ) # Copy current module skills into an operator skills directory and prune old namespace paths. #{self}.install( pwn_skills_path: 'required - operator skills directory that will receive pwn/', source: 'optional - bundled skill tree (defaults to etc/default_skills/pwn)' ) # Refresh the bundled tree when namespaces drifted, then install that tree for the operator. #{self}.sync_namespaces( dest: 'optional - parent of the bundled pwn/ skill tree (defaults to etc/default_skills)', pwn_skills_path: 'optional - operator skills directory (defaults to ~/.pwn/skills)' ) # Print the AUTHOR(S) string for this module. #{self}.authors " constants.sort end |
.install(opts = {}) ⇒ Object
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 |
# File 'lib/pwn/ai/module_skills.rb', line 180 public_class_method def self.install(opts = {}) root = opts[:pwn_skills_path] root = PWN::Config::Mgmt.pwn_skills_path if root.to_s.empty? && defined?(PWN::Config::Mgmt) 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::AI::ModuleSkills] install failed: #{e.class}: #{e.}" [] end |
.refresh!(opts = {}) ⇒ Object
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 |
# File 'lib/pwn/ai/module_skills.rb', line 105 public_class_method def self.refresh!(opts = {}) dest = opts[:dest].to_s dest = File.('../../../etc/default_skills', __dir__) if dest.empty? dest = File.dirname(dest) if File.basename(dest) == 'pwn' lib = opts[:lib_root].to_s lib = File.('..', LIB_ROOT) if lib.empty? mods = enumerate(lib_root: lib) raise ArgumentError, 'Source tree contains no PWN modules; refusing to prune skills' if mods.empty? 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
29 30 31 32 33 34 35 36 37 |
# File 'lib/pwn/ai/module_skills.rb', line 29 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
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 |
# File 'lib/pwn/ai/module_skills.rb', line 39 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 |
.sync_namespaces(opts = {}) ⇒ Object
Refresh the bundled skill tree when it has drifted, then copy it into the operator skills directory and drop paths that no longer match a current module.
218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/pwn/ai/module_skills.rb', line 218 public_class_method def self.sync_namespaces(opts = {}) dest = opts[:dest] skills = opts[:pwn_skills_path] skills = PWN::Config::Mgmt.pwn_skills_path if skills.to_s.empty? && defined?(PWN::Config::Mgmt) refreshed = nil begin report = drift(dest: dest) drifted = !report[:missing].empty? || !report[:stale].empty? || !report[:orphans].empty? refreshed = refresh!(dest: dest) if drifted rescue SystemCallError => e warn "[PWN::AI::ModuleSkills] bundled skill refresh skipped: #{e.class}: #{e.}" end { refreshed: refreshed, installed: install(pwn_skills_path: skills) } end |