Module: PWN::Plugins::Jobs
- Defined in:
- lib/pwn/plugins/jobs.rb
Overview
Background jobs that survive a pwn-ai turn (scans, fuzz campaigns).
Constant Summary collapse
- JOBS_DIR =
File.join(Dir.home, '.pwn', 'jobs')
Class Method Summary collapse
- .authors ⇒ Object
- .harvest(opts = {}) ⇒ Object
- .help ⇒ Object
- .job_kill(opts = {}) ⇒ Object
- .job_run(opts = {}) ⇒ Object
- .job_status(opts = {}) ⇒ Object
- .job_tail(opts = {}) ⇒ Object
- .list(opts = {}) ⇒ Object
- .required_bins ⇒ Object
- .result(opts = {}) ⇒ Object
- .run(opts = {}) ⇒ Object
- .start(opts = {}) ⇒ Object
- .status(opts = {}) ⇒ Object
- .stop(opts = {}) ⇒ Object
- .tail(opts = {}) ⇒ Object
- .watch(opts = {}) ⇒ Object
Class Method Details
.authors ⇒ Object
135 136 137 |
# File 'lib/pwn/plugins/jobs.rb', line 135 public_class_method def self. "AUTHOR(S):\n 0day Inc. <[email protected]>\n" end |
.harvest(opts = {}) ⇒ Object
113 114 115 |
# File 'lib/pwn/plugins/jobs.rb', line 113 public_class_method def self.harvest(opts = {}) result(opts) end |
.help ⇒ Object
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 179 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 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/pwn/plugins/jobs.rb', line 139 public_class_method def self.help puts "USAGE: # List host binaries this module expects to be installed. #{self}.required_bins # Run start and return its result #{self}.start( command: 'required - command value consumed by #start (defaults to opts[:cmd])', cmd: 'required - command string to run', session_id: 'optional - session id value consumed by #start', max_runtime: 'optional - seconds after which a live job is killed (TIMEOUT)' ) # Run status and return its result #{self}.status # Run tail and return its result #{self}.tail( lines: 'optional - lines value consumed by #tail' ) # Return status plus log tail for a job id. #{self}.result( id: 'required - job id from #start', lines: 'optional - tail line count (defaults to 80)' ) # Alias of result for harvesting a detached job from a later session. #{self}.harvest( id: 'required - job id from #start', lines: 'optional - tail line count (defaults to 80)' ) # List job metadata rows under ~/.pwn/jobs. #{self}.list( limit: 'optional - unused cap reserved for callers' ) # Run stop and return its result #{self}.stop # Tail a job log for a regex (AFL new-crash lines). #{self}.watch( id: 'required - job id from #start', pattern: 'required - regex to match in the job log' ) # Alias of start that returns a job handle immediately. #{self}.run( command: 'required - command string to run', cmd: 'optional - alias for command', session_id: 'optional - pwn-ai session id', max_runtime: 'optional - seconds after which a live job is killed' ) # Alias of #run. #{self}.job_run( command: 'required - command string to run', cmd: 'optional - alias for command' ) # Status by handle or id. #{self}.job_status( handle: 'optional - job handle from #run', id: 'optional - job id alias for handle' ) # Tail a job log by handle. #{self}.job_tail( handle: 'optional - job handle from #run', id: 'optional - job id alias for handle', lines: 'optional - number of trailing lines' ) # Kill a job by handle. #{self}.job_kill( handle: 'optional - job handle from #run', id: 'optional - job id alias for handle' ) # Print the AUTHOR(S) string for this module. #{self}.authors " constants.sort end |
.job_kill(opts = {}) ⇒ Object
59 60 61 |
# File 'lib/pwn/plugins/jobs.rb', line 59 public_class_method def self.job_kill(opts = {}) stop(id: opts[:handle] || opts[:id]) end |
.job_run(opts = {}) ⇒ Object
47 48 49 |
# File 'lib/pwn/plugins/jobs.rb', line 47 public_class_method def self.job_run(opts = {}) start(opts) end |
.job_status(opts = {}) ⇒ Object
51 52 53 |
# File 'lib/pwn/plugins/jobs.rb', line 51 public_class_method def self.job_status(opts = {}) status(id: opts[:handle] || opts[:id]) end |
.job_tail(opts = {}) ⇒ Object
55 56 57 |
# File 'lib/pwn/plugins/jobs.rb', line 55 public_class_method def self.job_tail(opts = {}) tail(id: opts[:handle] || opts[:id], lines: opts[:lines]) end |
.list(opts = {}) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/pwn/plugins/jobs.rb', line 117 public_class_method def self.list(opts = {}) _limit = opts[:limit] Dir[File.join(JOBS_DIR, '*.json')].filter_map do |path| row = JSON.parse(File.read(path), symbolize_names: true) status(id: row[:id]) rescue StandardError nil end end |
.required_bins ⇒ Object
15 16 17 |
# File 'lib/pwn/plugins/jobs.rb', line 15 public_class_method def self.required_bins [] end |
.result(opts = {}) ⇒ Object
108 109 110 111 |
# File 'lib/pwn/plugins/jobs.rb', line 108 public_class_method def self.result(opts = {}) row = status(opts) row.merge(tail: tail(opts.merge(lines: opts[:lines] || 80))) end |
.run(opts = {}) ⇒ Object
43 44 45 |
# File 'lib/pwn/plugins/jobs.rb', line 43 public_class_method def self.run(opts = {}) start(opts) end |
.start(opts = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/pwn/plugins/jobs.rb', line 19 public_class_method def self.start(opts = {}) cmd = opts[:command] || opts[:cmd] raise 'ERROR: command is required' if cmd.to_s.empty? FileUtils.mkdir_p(JOBS_DIR) id = SecureRandom.hex(6) log = File.join(JOBS_DIR, "#{id}.log") = File.join(JOBS_DIR, "#{id}.json") pid = spawn(cmd.to_s, %i[out err] => [log, 'a'], pgroup: true) Process.detach(pid) row = { id: id, pid: pid, command: cmd.to_s, log: log, session_id: opts[:session_id], max_runtime: opts[:max_runtime].to_i, started_at: Time.now.utc.iso8601 } File.write(, JSON.generate(row)) PWN::Plugins::ArtifactRegistry.register(session_id: opts[:session_id], path: log, kind: 'job-log') if opts[:session_id] && defined?(PWN::Plugins::ArtifactRegistry) && File.file?(log) row end |
.status(opts = {}) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/pwn/plugins/jobs.rb', line 76 public_class_method def self.status(opts = {}) row = load_job(opts) alive = begin Process.kill(0, row[:pid].to_i) true rescue Errno::ESRCH, Errno::EPERM false end max = row[:max_runtime].to_i if alive && max.positive? started = begin Time.parse(row[:started_at].to_s) rescue StandardError Time.now end if Time.now - started > max Process.kill('TERM', row[:pid].to_i) alive = false row[:status] = 'TIMEOUT' end end row.merge(alive: alive, status: row[:status] || (alive ? 'RUNNING' : 'COMPLETED')) end |
.stop(opts = {}) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/pwn/plugins/jobs.rb', line 127 public_class_method def self.stop(opts = {}) row = load_job(opts) Process.kill('TERM', row[:pid].to_i) true rescue Errno::ESRCH false end |
.tail(opts = {}) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/pwn/plugins/jobs.rb', line 100 public_class_method def self.tail(opts = {}) row = load_job(opts) n = (opts[:lines] || 40).to_i return '' unless File.file?(row[:log]) File.readlines(row[:log]).last(n).join end |
.watch(opts = {}) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/pwn/plugins/jobs.rb', line 63 public_class_method def self.watch(opts = {}) row = load_job(opts) pattern = opts[:pattern].to_s raise 'ERROR: pattern is required' if pattern.empty? log = row[:log].to_s return { id: row[:id], hits: [] } unless File.file?(log) rx = Regexp.new(pattern) hits = File.readlines(log).grep(rx).last(20) { id: row[:id], hits: hits.map(&:chomp), pattern: pattern } end |