Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rapid7
GitHub Repository: rapid7/metasploit-framework
Path: blob/master/lib/rex/post/io.rb
56687 views
1
# -*- coding: binary -*-
2
module Rex
3
module Post
4
5
##
6
# Base IO class that is modeled after the ruby IO class.
7
#
8
# This is an abstract base class that defines the interface for
9
# post-exploitation I/O operations. Subclasses must implement
10
# the actual I/O functionality.
11
##
12
class IO
13
protected
14
attr_accessor :filed, :mode
15
public
16
17
##
18
# Conditionals
19
##
20
21
# Checks if the end of file has been reached.
22
#
23
# @return [Boolean] true if at end of file
24
def eof?
25
return eof
26
end
27
28
# Checks if the I/O stream is closed.
29
#
30
# @raise [NotImplementedError] Must be implemented by subclass
31
#
32
# @return [Boolean] true if stream is closed
33
def closed?
34
raise NotImplementedError
35
end
36
37
# Checks if the I/O stream is a terminal device.
38
#
39
# @return [Boolean] true if stream is a TTY
40
def tty?
41
return isatty
42
end
43
44
##
45
# I/O operations
46
##
47
48
# Sets the stream to binary mode.
49
#
50
# @raise [NotImplementedError] Must be implemented by subclass
51
#
52
# @return [IO] self
53
def binmode
54
raise NotImplementedError
55
end
56
57
# Closes the I/O stream.
58
#
59
# @raise [NotImplementedError] Must be implemented by subclass
60
#
61
# @return [nil]
62
def close
63
raise NotImplementedError
64
end
65
66
# Closes the read end of a duplex I/O stream.
67
#
68
# @raise [NotImplementedError] Must be implemented by subclass
69
#
70
# @return [nil]
71
def close_read
72
raise NotImplementedError
73
end
74
75
# Closes the write end of a duplex I/O stream.
76
#
77
# @raise [NotImplementedError] Must be implemented by subclass
78
#
79
# @return [nil]
80
def close_write
81
raise NotImplementedError
82
end
83
84
# Iterates over each line in the stream.
85
#
86
# @param sep [String] line separator (default: $/)
87
# @yield [String] each line from the stream
88
#
89
# @raise [NotImplementedError] Must be implemented by subclass
90
#
91
# @return [void]
92
def each(sep = $/, &block)
93
raise NotImplementedError
94
end
95
96
# Alias for {#each}.
97
#
98
# @param sep [String] line separator (default: $/)
99
# @yield [String] each line from the stream
100
#
101
# @raise [NotImplementedError] Must be implemented by subclass
102
#
103
# @return [void]
104
def each_line(sep = $/, &block)
105
raise NotImplementedError
106
end
107
108
# Iterates over each byte in the stream.
109
#
110
# @yield [Integer] each byte from the stream
111
#
112
# @raise [NotImplementedError] Must be implemented by subclass
113
#
114
# @return [void]
115
def each_byte(&block)
116
raise NotImplementedError
117
end
118
119
# Checks if end of file has been reached.
120
#
121
# @raise [NotImplementedError] Must be implemented by subclass
122
#
123
# @return [Boolean] true if at end of file
124
def eof
125
raise NotImplementedError
126
end
127
128
# Performs low-level file control operation.
129
#
130
# @param cmd [Integer] control command
131
# @param arg [Integer, String] command argument
132
#
133
# @raise [NotImplementedError] Must be implemented by subclass
134
#
135
# @return [Integer] result of operation
136
def fcntl(cmd, arg)
137
raise NotImplementedError
138
end
139
140
# Flushes buffered data to the underlying I/O stream.
141
#
142
# @raise [NotImplementedError] Must be implemented by subclass
143
#
144
# @return [IO] self
145
def flush
146
raise NotImplementedError
147
end
148
149
# Synchronizes all buffered data with the storage device.
150
#
151
# @raise [NotImplementedError] Must be implemented by subclass
152
#
153
# @return [0] zero on success
154
def fsync
155
raise NotImplementedError
156
end
157
158
# Reads a single character from the stream.
159
#
160
# @raise [NotImplementedError] Must be implemented by subclass
161
#
162
# @return [Integer, nil] character code or nil at EOF
163
def getc
164
raise NotImplementedError
165
end
166
167
# Reads the next line from the stream.
168
#
169
# @param sep [String] line separator (default: $/)
170
#
171
# @raise [NotImplementedError] Must be implemented by subclass
172
#
173
# @return [String, nil] next line or nil at EOF
174
def gets(sep = $/)
175
raise NotImplementedError
176
end
177
178
# Performs low-level I/O control operation.
179
#
180
# @param cmd [Integer] control command
181
# @param arg [Integer, String] command argument
182
#
183
# @raise [NotImplementedError] Must be implemented by subclass
184
#
185
# @return [Integer] result of operation
186
def ioctl(cmd, arg)
187
raise NotImplementedError
188
end
189
190
# Checks if the stream is associated with a terminal device.
191
#
192
# @raise [NotImplementedError] Must be implemented by subclass
193
#
194
# @return [Boolean] true if stream is a TTY
195
def isatty
196
raise NotImplementedError
197
end
198
199
# Gets the current line number.
200
#
201
# @raise [NotImplementedError] Must be implemented by subclass
202
#
203
# @return [Integer] current line number
204
def lineno
205
raise NotImplementedError
206
end
207
208
# Gets the current file position.
209
#
210
# @raise [NotImplementedError] Must be implemented by subclass
211
#
212
# @return [Integer] current byte offset
213
def pos
214
raise NotImplementedError
215
end
216
217
# Writes a string to the stream.
218
#
219
# @raise [NotImplementedError] Must be implemented by subclass
220
#
221
# @return [Integer] number of bytes written
222
def print
223
raise NotImplementedError
224
end
225
226
# Writes a formatted string to the stream.
227
#
228
# @param fmt [String] format string
229
# @param args [Array] values to format
230
#
231
# @raise [NotImplementedError] Must be implemented by subclass
232
#
233
# @return [nil]
234
def printf(fmt, *args)
235
raise NotImplementedError
236
end
237
238
# Writes a character to the stream.
239
#
240
# @param obj [Integer] character code to write
241
#
242
# @raise [NotImplementedError] Must be implemented by subclass
243
#
244
# @return [Integer] the character code written
245
def putc(obj)
246
raise NotImplementedError
247
end
248
249
# Writes a string followed by newline to the stream.
250
#
251
# @param obj [String] data to write
252
#
253
# @raise [NotImplementedError] Must be implemented by subclass
254
#
255
# @return [nil]
256
def puts(obj)
257
raise NotImplementedError
258
end
259
260
# Reads data from the stream.
261
#
262
# @param length [Integer, nil] number of bytes to read (nil = read all)
263
# @param buffer [String, nil] buffer to read into
264
#
265
# @raise [NotImplementedError] Must be implemented by subclass
266
#
267
# @return [String, nil] data read or nil at EOF
268
def read(length = nil, buffer = nil)
269
raise NotImplementedError
270
end
271
272
# Reads a single character as a string.
273
#
274
# @raise [NotImplementedError] Must be implemented by subclass
275
#
276
# @return [String, nil] character or nil at EOF
277
def readchar
278
raise NotImplementedError
279
end
280
281
# Reads the next line from the stream.
282
#
283
# @param sep [String] line separator (default: $/)
284
#
285
# @raise [NotImplementedError] Must be implemented by subclass
286
#
287
# @return [String] next line
288
# @raise [EOFError] if at end of file
289
def readline(sep = $/)
290
raise NotImplementedError
291
end
292
293
# Reads all lines from the stream into an array.
294
#
295
# @param sep [String] line separator (default: $/)
296
#
297
# @raise [NotImplementedError] Must be implemented by subclass
298
#
299
# @return [Array<String>] array of lines
300
def readlines(sep = $/)
301
raise NotImplementedError
302
end
303
304
# Repositions the stream to the beginning.
305
#
306
# @raise [NotImplementedError] Must be implemented by subclass
307
#
308
# @return [0] zero on success
309
def rewind
310
raise NotImplementedError
311
end
312
313
# Repositions the file pointer.
314
#
315
# @param offset [Integer] byte offset
316
# @param whence [Integer] position reference (SEEK_SET, SEEK_CUR, SEEK_END)
317
#
318
# @raise [NotImplementedError] Must be implemented by subclass
319
#
320
# @return [0] zero on success
321
def seek(offset, whence = SEEK_SET)
322
raise NotImplementedError
323
end
324
325
# Gets file status information.
326
#
327
# @raise [NotImplementedError] Must be implemented by subclass
328
#
329
# @return [File::Stat] file status object
330
def stat
331
raise NotImplementedError
332
end
333
334
# Synchronizes the stream with the underlying storage.
335
#
336
# @raise [NotImplementedError] Must be implemented by subclass
337
#
338
# @return [IO] self
339
def sync
340
raise NotImplementedError
341
end
342
343
# Reads data at the system level.
344
#
345
# @param length [Integer] number of bytes to read
346
#
347
# @raise [NotImplementedError] Must be implemented by subclass
348
#
349
# @return [String] data read
350
def sysread(length)
351
raise NotImplementedError
352
end
353
354
# Repositions the file pointer at the system level.
355
#
356
# @param offset [Integer] byte offset
357
# @param whence [Integer] position reference (SEEK_SET, SEEK_CUR, SEEK_END)
358
#
359
# @raise [NotImplementedError] Must be implemented by subclass
360
#
361
# @return [Integer] new file offset
362
def sysseek(offset, whence = SEEK_SET)
363
raise NotImplementedError
364
end
365
366
# Writes data to the stream at the OS level.
367
#
368
# @param buf [String] data to write
369
#
370
# @raise [NotImplementedError] Must be implemented by subclass
371
#
372
# @return [Integer] number of bytes written
373
def syswrite(buf)
374
raise NotImplementedError
375
end
376
377
# Gets the current file position (alias for {#pos}).
378
#
379
# @return [Integer] current byte offset
380
def tell
381
return pos
382
end
383
384
# Pushes a character back onto the stream.
385
#
386
# @param val [Integer] character code to push back
387
#
388
# @raise [NotImplementedError] Must be implemented by subclass
389
#
390
# @return [nil]
391
def ungetc(val)
392
raise NotImplementedError
393
end
394
395
# Writes data to the stream.
396
#
397
# @param buf [String] data to write
398
#
399
# @raise [NotImplementedError] Must be implemented by subclass
400
#
401
# @return [Integer] number of bytes written
402
def write(buf)
403
raise NotImplementedError
404
end
405
406
end
407
408
end; end # Post/Rex
409
410