Module: PWN::Plugins::Tor

Defined in:
lib/pwn/plugins/tor.rb

Overview

This plugin processes images into readable text

Class Method Summary collapse

Class Method Details

.authorsObject

Author(s)

0day Inc. <[email protected]>



201
202
203
204
205
# File 'lib/pwn/plugins/tor.rb', line 201

public_class_method def self.authors
  "AUTHOR(S):
    0day Inc. <[email protected]>
  "
end

.helpObject

Display Usage for this Module



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/pwn/plugins/tor.rb', line 209

public_class_method def self.help
  puts "USAGE:
    tor_obj = #{self}.start(
      ip: 'optional - IP address to listen (default: 127.0.0.1)',
      port: 'optional - socks port to listen (default: 9050)',
      ctrl_port: 'optional - tor control port to listen (default: 9051)',
      net: 'optional - CIDR notation to accept connections (default: 127.0.0.1/32)',
      data_dir: 'optional - directory to keep tor session data (default: /tmp/tor_pwn-TIMESTAMP)'
    )

    #{self}.switch_exit_node(
      tor_obj: 'required - tor_obj returned from #start method',
      response_timeout: 'optional - float in seconds to timeout (default: 3.0)'
    )

    #{self}.stop(
      tor_obj: 'required - tor_obj returned from #start method'
    )

    #{self}.authors
  "
end

.start(opts = {}) ⇒ Object

Supported Method Parameters

tor_obj = PWN::Plugins::Tor.start(

ip: 'optional - IP address to listen (default: 127.0.0.1)',
port: 'optional - socks port to listen (default: 1024-65535)',
ctrl_port: 'optional - tor control port to listen (default: 1024-65535)',
net: 'optional - CIDR notation to accept connections (default: 127.0.0.0.1/32)',
data_dir: 'optional - directory to keep tor session data (default: /tmp/tor_pwn-TIMESTAMP)'

)



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
137
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
# File 'lib/pwn/plugins/tor.rb', line 85

public_class_method def self.start(opts = {})
  ip = opts[:ip]
  ip ||= '127.0.0.1'
  port = opts[:port].to_i
  port = PWN::Plugins::Sock.get_random_unused_port if port.zero?
  ctrl_port = opts[:ctrl_port].to_i
  if ctrl_port.zero?
    loop do
      ctrl_port = PWN::Plugins::Sock.get_random_unused_port
      break if ctrl_port != port
    end
  end

  net = opts[:net]
  net ||= "#{ip}/32"
  acl_net = NetAddr.parse_net(net)

  timestamp = Time.now.strftime('%Y-%m-%d_%H-%M-%S.%N%z')
  data_dir = opts[:data_dir]
  data_dir ||= "/tmp/tor_pwn-#{timestamp}"
  FileUtils.mkdir_p(data_dir)

  socks_proxy = "#{ip}:#{port}"
  pid_file = "#{data_dir}/tor.pid"
  cookie_authn_file = "#{data_dir}/control_auth_cookie"
  session_log_path = "#{data_dir}/stdout-session.log"
  session_log = File.new(session_log_path, 'w')
  session_log.sync = true
  session_log.fsync

  fork_pid = Process.fork do
    pty = PTY.spawn(
      'tor',
      'DataDirectory',
      data_dir,
      'SocksPort',
      socks_proxy,
      'ControlPort',
      ctrl_port.to_s,
      'CookieAuthentication',
      '1',
      'SocksPolicy',
      "accept #{acl_net}",
      'SocksPolicy',
      'reject *'
    ) do |stdout, _stdin, pid|
      File.write(pid_file, pid)
      stdout.each do |line|
        session_log.puts line
      end
    end
  rescue StandardError => e
    puts 'Tor exiting with errors...'
    FileUtils.rm_rf(data_dir)
    raise e
  end
  Process.detach(fork_pid)

  loop do
    pid_ready = File.exist?(pid_file)
    cookie_authn_ready = File.exist?(cookie_authn_file)
    sleep 0.1
    break if pid_ready && cookie_authn_ready
  end

  cookie_authn = `hexdump -e '32/1 "%02x"' #{cookie_authn_file}`
  tor_obj = {
    parent_pid: fork_pid,
    child_pid: File.read(pid_file).to_i,
    ip: ip,
    port: port,
    ctrl_port: ctrl_port,
    data_dir: data_dir,
    cookie_authn: cookie_authn
  }
rescue StandardError, SystemExit => e
  stop(tor_obj) unless tor_obj.nil?
  raise e
end

.stop(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Tor.stop(

tor_obj: 'required - tor_obj returned from #start method'

)



188
189
190
191
192
193
194
195
196
197
# File 'lib/pwn/plugins/tor.rb', line 188

public_class_method def self.stop(opts = {})
  tor_obj = opts[:tor_obj]
  unless tor_obj.nil?
    FileUtils.rm_rf(tor_obj[:data_dir])
    Process.kill('TERM', tor_obj[:child_pid])
    Process.kill('TERM', tor_obj[:parent_pid])
  end
rescue StandardError => e
  raise e
end

.switch_exit_node(opts = {}) ⇒ Object

Supported Method Parameters

PWN::Plugins::Tor.switch_exit_node(

tor_obj: 'required - tor_obj returned from #start method',
response_timeout: 'optional - float in seconds to timeout (default: 3.0)'

)



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/pwn/plugins/tor.rb', line 171

public_class_method def self.switch_exit_node(opts = {})
  tor_obj = opts[:tor_obj]
  response_timeout = opts[:response_timeout]
  tor_control_cmd(
    tor_obj: tor_obj,
    cmd: 'SIGNAL NEWNYM',
    response_timeout: response_timeout
  )
rescue StandardError => e
  raise e
end