ronin-post_ex 0.1.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/.document +6 -0
  3. data/.github/workflows/ruby.yml +31 -0
  4. data/.gitignore +13 -0
  5. data/.rspec +1 -0
  6. data/.ruby-version +1 -0
  7. data/.yardopts +1 -0
  8. data/API_SPEC.md +235 -0
  9. data/COPYING.txt +165 -0
  10. data/ChangeLog.md +23 -0
  11. data/Gemfile +36 -0
  12. data/README.md +245 -0
  13. data/Rakefile +34 -0
  14. data/examples/bind_shell.rb +19 -0
  15. data/gemspec.yml +25 -0
  16. data/lib/ronin/post_ex/cli/shell_shell.rb +66 -0
  17. data/lib/ronin/post_ex/cli/system_shell.rb +811 -0
  18. data/lib/ronin/post_ex/remote_dir.rb +190 -0
  19. data/lib/ronin/post_ex/remote_file/stat.rb +174 -0
  20. data/lib/ronin/post_ex/remote_file.rb +417 -0
  21. data/lib/ronin/post_ex/remote_process.rb +170 -0
  22. data/lib/ronin/post_ex/resource.rb +144 -0
  23. data/lib/ronin/post_ex/sessions/bind_shell.rb +60 -0
  24. data/lib/ronin/post_ex/sessions/remote_shell_session.rb +48 -0
  25. data/lib/ronin/post_ex/sessions/reverse_shell.rb +67 -0
  26. data/lib/ronin/post_ex/sessions/rpc_session.rb +779 -0
  27. data/lib/ronin/post_ex/sessions/session.rb +73 -0
  28. data/lib/ronin/post_ex/sessions/shell_session.rb +618 -0
  29. data/lib/ronin/post_ex/system/fs.rb +650 -0
  30. data/lib/ronin/post_ex/system/process.rb +422 -0
  31. data/lib/ronin/post_ex/system/shell.rb +1037 -0
  32. data/lib/ronin/post_ex/system.rb +191 -0
  33. data/lib/ronin/post_ex/version.rb +26 -0
  34. data/lib/ronin/post_ex.rb +22 -0
  35. data/ronin-post_ex.gemspec +61 -0
  36. data/spec/sessions/bind_shell_spec.rb +31 -0
  37. data/spec/sessions/remote_shell_session_spec.rb +28 -0
  38. data/spec/sessions/reverse_shell_spec.rb +49 -0
  39. data/spec/sessions/rpc_session_spec.rb +500 -0
  40. data/spec/sessions/session_spec.rb +61 -0
  41. data/spec/sessions/shell_session_spec.rb +482 -0
  42. data/spec/spec_helper.rb +9 -0
  43. data/spec/system_spec.rb +66 -0
  44. metadata +155 -0
@@ -0,0 +1,422 @@
1
+ # frozen_string_literal: true
2
+ #
3
+ # ronin-post_ex - a Ruby API for Post-Exploitation.
4
+ #
5
+ # Copyright (c) 2007-2022 Hal Brodigan (postmodern.mod3 at gmail.com)
6
+ #
7
+ # ronin-post_ex is free software: you can redistribute it and/or modify
8
+ # it under the terms of the GNU Lesser General Public License as published
9
+ # by the Free Software Foundation, either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # ronin-post_ex is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU Lesser General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU Lesser General Public License
18
+ # along with ronin-post_ex. If not, see <https://www.gnu.org/licenses/>.
19
+ #
20
+
21
+ require 'ronin/post_ex/resource'
22
+ require 'ronin/post_ex/remote_process'
23
+
24
+ require 'time'
25
+
26
+ module Ronin
27
+ module PostEx
28
+ class System < Resource
29
+ #
30
+ # Provides access to the current process and managing child processes.
31
+ #
32
+ # # Supported Control Methods
33
+ #
34
+ # The Process resource uses the following post-exploitation API methods,
35
+ # defined by the {#session} object.
36
+ #
37
+ # * `process_getpid -> Integer`
38
+ # * `process_getppid -> Integer`
39
+ # * `process_getuid -> Integer`
40
+ # * `process_setuid(uid : Integer)`
41
+ # * `process_geteuid -> Integer`
42
+ # * `process_seteuid(euid : Integer)`
43
+ # * `process_getgid -> Integer`
44
+ # * `process_setgid(gid : Integer)`
45
+ # * `process_getegid -> Integer`
46
+ # * `process_setegid(egid : Integer)`
47
+ # * `process_getsid -> Integer`
48
+ # * `process_setsid(sid : Integer) -> Integer`
49
+ # * `process_environ -> Hash[String, String]`
50
+ # * `process_getenv(name : String) -> String | env`
51
+ # * `process_setenv(name : String, value : String)`
52
+ # * `process_unsetenv(name : String)`
53
+ # * `process_kill(pid : Integer, signal : Integer)`
54
+ # * `process_popen(command : String) -> Integer`
55
+ # * `process_read(fd : Integer, length : Integer) -> String`
56
+ # * `process_write(fd : Integer, data : String)`
57
+ # * `process_close(fd : Integer)`
58
+ # * `process_spawn(program : String, *arguments : Array[String]) -> Integer`
59
+ # * `process_exit`
60
+ #
61
+ class Process < Resource
62
+
63
+ #
64
+ # Gets the pid of the current process.
65
+ #
66
+ # @return [Integer]
67
+ # The current PID.
68
+ #
69
+ # @note
70
+ # Requires the `process_getpid` method be defined by the {#session}
71
+ # object.
72
+ #
73
+ def getpid
74
+ @session.process_getpid
75
+ end
76
+ resource_method :pid, [:process_getpid]
77
+
78
+ alias pid getpid
79
+
80
+ #
81
+ # Gets the pid of the parent process.
82
+ #
83
+ # @return [Integer]
84
+ # The parent PID.
85
+ #
86
+ # @note
87
+ # Requires the `process_getppid` method be defined by the {#session}
88
+ # object.
89
+ #
90
+ def getppid
91
+ @session.process_getppid
92
+ end
93
+ resource_method :ppid, [:process_getppid]
94
+
95
+ alias ppid getppid
96
+
97
+ #
98
+ # Gets the UID that the current process is running under.
99
+ #
100
+ # @return [Integer]
101
+ # The current UID.
102
+ #
103
+ # @note
104
+ # Requires the `process_getuid` method be defined by the {#session}
105
+ # object.
106
+ #
107
+ def getuid
108
+ @session.process_getuid
109
+ end
110
+ resource_method :uid, [:process_getuid]
111
+
112
+ alias uid getuid
113
+
114
+ #
115
+ # Attempts to set the UID of the current process.
116
+ #
117
+ # @param [Integer] new_uid
118
+ # The new UID.
119
+ #
120
+ # @note
121
+ # Requires the `process_setuid` method be defined by the {#session}
122
+ # object.
123
+ #
124
+ def setuid(new_uid)
125
+ @session.process_setuid(new_uid)
126
+ end
127
+ resource_method :uid=, [:process_setuid]
128
+
129
+ alias uid= setuid
130
+
131
+ #
132
+ # Gets the effective UID that the current process is running under.
133
+ #
134
+ # @return [Integer]
135
+ # The effective UID.
136
+ #
137
+ # @note
138
+ # Requires the `process_geteuid` method be defined by the {#session}
139
+ # object.
140
+ #
141
+ def geteuid
142
+ @session.process_geteuid
143
+ end
144
+ resource_method :euid, [:process_geteuid]
145
+
146
+ alias euid geteuid
147
+
148
+ #
149
+ # Attempts to set the effective UID of the current process.
150
+ #
151
+ # @param [Integer] new_euid
152
+ # The new effective UID.
153
+ #
154
+ # @note
155
+ # Requires the `process_seteuid` method be defined by the {#session}
156
+ # object.
157
+ #
158
+ def seteuid(new_euid)
159
+ @session.process_seteuid(new_euid)
160
+ end
161
+ resource_method :euid=, [:process_seteuid]
162
+
163
+ alias euid= seteuid
164
+
165
+ #
166
+ # Gets the GID that the current process is running under.
167
+ #
168
+ # @return [Integer]
169
+ # The current GID.
170
+ #
171
+ # @note
172
+ # Requires the `process_getgid` method be defined by the {#session}
173
+ # object.
174
+ #
175
+ def getgid
176
+ @session.process_getgid
177
+ end
178
+ resource_method :gid, [:process_getgid]
179
+
180
+ alias gid getgid
181
+
182
+ #
183
+ # Attempts to set the GID of the current process.
184
+ #
185
+ # @param [Integer] new_gid
186
+ # The new GID.
187
+ #
188
+ # @note
189
+ # Requires the `process_setgid` method be defined by the {#session}
190
+ # object.
191
+ #
192
+ def setgid(new_gid)
193
+ @session.process_setgid(new_gid)
194
+ end
195
+ resource_method :gid=, [:process_setgid]
196
+
197
+ alias gid= setgid
198
+
199
+ #
200
+ # Gets the effective GID that the current process is running under.
201
+ #
202
+ # @return [Integer]
203
+ # The effective GID.
204
+ #
205
+ # @note
206
+ # Requires the `process_getegid` method be defined by the {#session}
207
+ # object.
208
+ #
209
+ def getegid
210
+ @session.process_getegid
211
+ end
212
+ resource_method :egid, [:process_getegid]
213
+
214
+ alias egid getegid
215
+
216
+ #
217
+ # Attempts to set the effective GID of the current process.
218
+ #
219
+ # @param [Integer] new_egid
220
+ # The new effective GID.
221
+ #
222
+ # @note
223
+ # Requires the `process_setegid` method be defined by the {#session}
224
+ # object.
225
+ #
226
+ def setegid(new_egid)
227
+ @session.process_setegid(new_egid)
228
+ end
229
+ resource_method :egid=, [:process_setegid]
230
+
231
+ alias egid= setegid
232
+
233
+ #
234
+ # Gets the SID of the current process.
235
+ #
236
+ # @return [Integer]
237
+ # The current SID.
238
+ #
239
+ # @note
240
+ # Requires the `process_getsid` method be defined by the {#session}
241
+ # object.
242
+ #
243
+ def getsid
244
+ @session.process_getsid
245
+ end
246
+ resource_method :sid, [:process_getsid]
247
+
248
+ alias sid getsid
249
+
250
+ #
251
+ # Sets the SID of the current process.
252
+ #
253
+ # @note
254
+ # Requires the `process_setsid` method be defined by the {#session}
255
+ # object.
256
+ #
257
+ def setsid
258
+ @session.process_setsid
259
+ end
260
+ resource_method :setsid, [:process_setsid]
261
+
262
+ alias sid! setsid
263
+
264
+ #
265
+ # Retrieves the whole environment Hash.
266
+ #
267
+ # @return [Hash{String => String}]
268
+ # The Hash of environment variables.
269
+ #
270
+ # @note
271
+ # Requires the `process_environ` method be defined by the {#session}
272
+ # object.
273
+ #
274
+ # @api public
275
+ #
276
+ def environ
277
+ @session.process_environ
278
+ end
279
+ resource_method :environ, [:process_environ]
280
+
281
+ alias env environ
282
+
283
+ #
284
+ # Retrieves the value of a environment variable.
285
+ #
286
+ # @param [String] name
287
+ # The name of the environment variable.
288
+ #
289
+ # @return [String, nil]
290
+ # The value of the environment variable.
291
+ #
292
+ # @note
293
+ # Requires `process_getenv` or `process_environ` methods be defined by
294
+ # the {#session} object.
295
+ #
296
+ # @api public
297
+ #
298
+ def getenv(name)
299
+ if @session.respond_to?(:process_getenv)
300
+ @session.process_getenv(name)
301
+ elsif @session.respond_to?(:process_environ)
302
+ @session.process_environ[name]
303
+ else
304
+ raise(NoMethodError,"#{@session} does not define process_getenv or process_environ")
305
+ end
306
+ end
307
+ resource_method :getenv, [:process_getenv]
308
+
309
+ #
310
+ # Sets the value of a environment variable.
311
+ #
312
+ # @param [String] name
313
+ # The name of the environment variable.
314
+ #
315
+ # @param [String] value
316
+ # The new value for the environment variable.
317
+ #
318
+ # @note
319
+ # Requires the `process_setenv` method be defined by the {#session}
320
+ # object.
321
+ #
322
+ # @api public
323
+ #
324
+ def setenv(name,value)
325
+ @session.process_setenv(name,value)
326
+ end
327
+ resource_method :setenv, [:process_setenv]
328
+
329
+ #
330
+ # Unsets an environment variable.
331
+ #
332
+ # @param [String] name
333
+ # The name of the environment variable.
334
+ #
335
+ # @note
336
+ # Requires the `process_unsetenv` method be defined by the {#session}
337
+ # object.
338
+ #
339
+ # @api public
340
+ #
341
+ def unsetenv(name)
342
+ @session.process_unsetenv(name)
343
+ end
344
+ resource_method :unsetenv, [:process_unsetenv]
345
+
346
+ #
347
+ # Kills a process.
348
+ #
349
+ # @param [Integer] pid
350
+ # The PID of the process to kill.
351
+ #
352
+ # @param [String] signal
353
+ # The POSIX signal name to send to the process.
354
+ #
355
+ # @note
356
+ # Requires the `process_kill` method be defined by the {#session}
357
+ # object.
358
+ #
359
+ def kill(pid,signal='KILL')
360
+ @session.process_kill(pid,signal)
361
+ end
362
+ resource_method :kill, [:process_kill]
363
+
364
+ #
365
+ # Opens a new process.
366
+ #
367
+ # @param [String] command
368
+ # The command string to execute.
369
+ #
370
+ # @return [RemoteProcess]
371
+ # The newly opened remote process.
372
+ #
373
+ # @note
374
+ # Requires the `process_popen` method be defined by the {#session}
375
+ # object.
376
+ #
377
+ # @api public
378
+ #
379
+ def popen(command)
380
+ RemoteProcess.new(@session,command)
381
+ end
382
+ resource_method :spawn, [:process_popen]
383
+
384
+ #
385
+ # Executes a program as a separate child process.
386
+ #
387
+ # @param [String] program
388
+ # The name or path of the program.
389
+ #
390
+ # @param [Array<String>] arguments
391
+ # Additional arguments to execute the program with.
392
+ #
393
+ # @return [Integer]
394
+ # The pid of the new process.
395
+ #
396
+ # @note
397
+ # Requires the `process_spawn` method be defined by the {#session}
398
+ # object.
399
+ #
400
+ # @api public
401
+ #
402
+ def spawn(program,*arguments)
403
+ @session.process_spawn(program,*arguments)
404
+ end
405
+ resource_method :spawn, [:process_spawn]
406
+
407
+ #
408
+ # Exits the current running process.
409
+ #
410
+ # @note
411
+ # Requires the `process_exit` method be defined by the {#session}
412
+ # object.
413
+ #
414
+ def exit
415
+ @session.process_exit
416
+ end
417
+ resource_method :exit, [:process_exit]
418
+
419
+ end
420
+ end
421
+ end
422
+ end