Class: Rex::Proto::Ftp::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/proto/ftp/server.rb

Overview

Anonymous-only, in-memory FTP server. Designed for use as a fetch-payload delivery server.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(port = 21, host = '0.0.0.0', context = {}) ⇒ Server

Returns a new instance of Server.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rex/proto/ftp/server.rb', line 24

def initialize(port = 21, host = '0.0.0.0', context = {})
  self.listen_port = port
  self.listen_host = host
  self.context = context
  self.files = {}
  self.serve_once = {}
  self.files_mutex = Mutex.new
  self.shutting_down = false
  self.listener = nil
  self.monitor_thread = nil
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def context
  @context
end

#filesObject

Returns the value of attribute files.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def files
  @files
end

#files_mutexObject

Returns the value of attribute files_mutex.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def files_mutex
  @files_mutex
end

#listen_hostObject

Returns the value of attribute listen_host.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def listen_host
  @listen_host
end

#listen_portObject

Returns the value of attribute listen_port.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def listen_port
  @listen_port
end

#listenerObject

Returns the value of attribute listener.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def listener
  @listener
end

#monitor_threadObject

Returns the value of attribute monitor_thread.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def monitor_thread
  @monitor_thread
end

#serve_onceObject

Returns the value of attribute serve_once.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def serve_once
  @serve_once
end

#shutting_downObject

Returns the value of attribute shutting_down.



75
76
77
# File 'lib/rex/proto/ftp/server.rb', line 75

def shutting_down
  @shutting_down
end

Class Method Details

.hardcore_alias(port, host, *_rest) ⇒ Object



16
17
18
# File 'lib/rex/proto/ftp/server.rb', line 16

def self.hardcore_alias(port, host, *_rest)
  "#{host}-#{port}"
end

Instance Method Details

#aliasObject



20
21
22
# File 'lib/rex/proto/ftp/server.rb', line 20

def alias
  @alias || 'FTP Server'
end

#deregister_file(filename) ⇒ Object



43
44
45
46
47
48
# File 'lib/rex/proto/ftp/server.rb', line 43

def deregister_file(filename)
  files_mutex.synchronize do
    files.delete(filename)
    serve_once.delete(filename)
  end
end

#register_file(filename, data, serve_once: true) ⇒ Object



36
37
38
39
40
41
# File 'lib/rex/proto/ftp/server.rb', line 36

def register_file(filename, data, serve_once: true)
  files_mutex.synchronize do
    files[filename] = data
    self.serve_once[filename] = serve_once
  end
end

#startObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/rex/proto/ftp/server.rb', line 50

def start
  self.listener = Rex::Socket::TcpServer.create(
    'LocalHost' => listen_host,
    'LocalPort' => listen_port,
    'Context' => context
  )
  self.monitor_thread = Rex::ThreadFactory.spawn("FTPServerMonitor-#{listen_host}-#{listen_port}", false) do
    monitor_socket
  end
end

#stopObject



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rex/proto/ftp/server.rb', line 61

def stop
  self.shutting_down = true
  begin
    monitor_thread&.kill
  rescue StandardError
    nil
  end
  begin
    listener&.close
  rescue StandardError
    nil
  end
end