Module: Net

Defined in:
lib/ronin/network/extensions/ssl/net.rb,
lib/ronin/network/extensions/tcp/net.rb,
lib/ronin/network/extensions/udp/net.rb,
lib/ronin/network/extensions/http/net.rb,
lib/ronin/network/extensions/imap/net.rb,
lib/ronin/network/extensions/pop3/net.rb,
lib/ronin/network/extensions/smtp/net.rb,
lib/ronin/network/extensions/esmtp/net.rb,
lib/ronin/network/extensions/telnet/net.rb

Class Method Summary collapse

Class Method Details

.esmtp_connect(host, options = {}) {|session| ... } ⇒ Net::SMTP

Creates a connection to the ESMTP server.

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :port (Integer) — default: Ronin::Network::SMTP.default_port

    The port to connect to.

  • :helo (String)

    The HELO domain.

  • :auth (Symbol)

    The type of authentication to use. Can be either :login, :plain, or :cram_md5.

  • :user (String)

    The user-name to authenticate with.

  • :password (String)

    The password to authenticate with.

Yields:

  • (session)

    If a block is given, it will be passed an ESMTP enabled session object.

Yield Parameters:

  • session (Net::SMTP)

    The ESMTP session.

Returns:

  • (Net::SMTP)

    The ESMTP enabled session.



69
70
71
72
73
74
75
# File 'lib/ronin/network/extensions/esmtp/net.rb', line 69

def Net.esmtp_connect(host,options={})
  session = Net.smtp_connect(host,options)
  session.esmtp = true

  yield session if block_given?
  return session
end

.esmtp_message(options = {}, &block) ⇒ Object



28
29
30
# File 'lib/ronin/network/extensions/esmtp/net.rb', line 28

def Net.esmtp_message(options={},&block)
  Net.smtp_message(options,&block)
end

.esmtp_session(host, options = {}) {|session| ... } ⇒ Object

Starts an ESMTP session with the ESMTP enabled server.

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (session)

    If a block is given, it will be passed an ESMTP enabled session object. After the block has returned, the session will be closed.

Yield Parameters:

  • session (Net::SMTP)

    The ESMTP session.

See Also:



97
98
99
100
101
102
103
# File 'lib/ronin/network/extensions/esmtp/net.rb', line 97

def Net.esmtp_session(host,options={})
  Net.smtp_session(host,options) do |session|
    session.esmtp = true

    yield session if block_given?
  end
end

.http_connect(options = {}) {|session| ... } ⇒ Net::HTTP

Starts a HTTP connection with the server.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options

  • :ssl (Hash)

    a customizable set of options

Options Hash (options):

  • :url (String, URI::HTTP)

    The full URL to request.

  • :host (String)

    The host the HTTP server is running on.

  • :port (Integer) — default: Net::HTTP.default_port

    The port the HTTP server is listening on.

  • :proxy (String, Hash) — default: Ronin::Network::HTTP.proxy

    A Hash of proxy settings to use when connecting to the HTTP server.

  • :user (String)

    The user to authenticate with when connecting to the HTTP server.

  • :password (String)

    The password to authenticate with when connecting to the HTTP server.

  • :ssl (Boolean, Hash)

    Enables SSL for the HTTP connection.

Yields:

  • (session)

    If a block is given, it will be passed the newly created HTTP session object.

Yield Parameters:

  • session (Net::HTTP)

    The newly created HTTP session.

Returns:

  • (Net::HTTP)

    The HTTP session object.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/ronin/network/extensions/http/net.rb', line 74

def Net.http_connect(options={},&block)
  options = Ronin::Network::HTTP.expand_options(options)

  host = options[:host].to_s
  port = options[:port]
  proxy = options[:proxy]
  proxy_host = if (proxy && proxy[:host])
                 proxy[:host].to_s
               end

  sess = Net::HTTP::Proxy(
    proxy_host,
    proxy[:port],
    proxy[:user],
    proxy[:password]
  ).new(host.to_s,port)

  if options[:ssl]
    sess.use_ssl = true
    sess.verify_mode = Ronin::Network::SSL::VERIFY[options[:ssl][:verify]]
  end

  sess.start()

  if block
    if block.arity == 2
      block.call(sess,options)
    else
      block.call(sess)
    end
  end

  return sess
end

.http_copy(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Copy request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



330
331
332
333
334
335
# File 'lib/ronin/network/extensions/http/net.rb', line 330

def Net.http_copy(options={})
  resp = Net.http_request(options.merge(:method => :copy))

  yield resp if block_given?
  return resp
end

.http_delete(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Delete request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/ronin/network/extensions/http/net.rb', line 357

def Net.http_delete(options={},&block)
  original_headers = options[:headers]

  # set the HTTP Depth header
  options[:headers] = {:depth => 'Infinity'}

  if original_headers
    options[:header].merge!(original_headers)
  end

  resp = Net.http_request(options.merge(:method => :delete))

  yield resp if block_given?
  return resp
end

.http_get(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Get request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



393
394
395
396
397
398
# File 'lib/ronin/network/extensions/http/net.rb', line 393

def Net.http_get(options={},&block)
  resp = Net.http_request(options.merge(:method => :get))

  yield resp if block_given?
  return resp
end

.http_get_body(options = {}) ⇒ String

Performs an HTTP Get request and returns the Respond Body.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Returns:

  • (String)

    The body of the HTTP response.

See Also:



432
433
434
# File 'lib/ronin/network/extensions/http/net.rb', line 432

def Net.http_get_body(options={})
  Net.http_get(options).body
end

.http_get_headers(options = {}) ⇒ Hash{String => Array<String>}

Performs an HTTP Get request and returns the Response Headers.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Returns:

See Also:

Since:

  • 0.2.0



415
416
417
# File 'lib/ronin/network/extensions/http/net.rb', line 415

def Net.http_get_headers(options={})
  Net.http_get(options).to_hash
end

.http_head(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Head request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



456
457
458
459
460
461
# File 'lib/ronin/network/extensions/http/net.rb', line 456

def Net.http_head(options={},&block)
  resp = Net.http_request(options.merge(:method => :head))

  yield resp if block_given?
  return resp
end

.http_lock(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Lock request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



483
484
485
486
487
488
# File 'lib/ronin/network/extensions/http/net.rb', line 483

def Net.http_lock(options={},&block)
  resp = Net.http_request(options.merge(:method => :lock))

  yield resp if block_given?
  return resp
end

.http_mkcol(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Mkcol request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



510
511
512
513
514
515
# File 'lib/ronin/network/extensions/http/net.rb', line 510

def Net.http_mkcol(options={},&block)
  resp = Net.http_request(options.merge(:method => :mkcol))

  yield resp if block_given?
  return resp
end

.http_move(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Move request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



537
538
539
540
541
542
# File 'lib/ronin/network/extensions/http/net.rb', line 537

def Net.http_move(options={},&block)
  resp = Net.http_request(options.merge(:method => :move))

  yield resp if block_given?
  return resp
end

.http_ok?(options = {}) ⇒ Boolean

Checks if the response has an HTTP OK status code.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :method (Symbol, String) — default: :head

    The method to use for the request.

Returns:

  • (Boolean)

    Specifies whether the response had an HTTP OK status code or not.

See Also:



262
263
264
# File 'lib/ronin/network/extensions/http/net.rb', line 262

def Net.http_ok?(options={})
  Net.http_status(options) == 200
end

.http_options(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Options request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



564
565
566
567
568
569
# File 'lib/ronin/network/extensions/http/net.rb', line 564

def Net.http_options(options={},&block)
  resp = Net.http_request(options.merge(:method => :options))

  yield resp if block_given?
  return resp
end

.http_post(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Post request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :form_data (Hash, String)

    The form data to send with the HTTP Post request.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



594
595
596
597
598
599
# File 'lib/ronin/network/extensions/http/net.rb', line 594

def Net.http_post(options={},&block)
  resp = Net.http_request(options.merge(:method => :post))

  yield resp if block_given?
  return resp
end

.http_post_body(options = {}) ⇒ String

Performs an HTTP Post request and returns the Response Body.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :form_data (Hash, String)

    The form data to send with the HTTP Post request.

Returns:

  • (String)

    The body of the HTTP response.

See Also:



639
640
641
# File 'lib/ronin/network/extensions/http/net.rb', line 639

def Net.http_post_body(options={})
  Net.http_post(options).body
end

.http_post_headers(options = {}) ⇒ Hash{String => Array<String>}

Performs an HTTP Post request and returns the Response Headers.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :form_data (Hash, String)

    The form data to send with the HTTP Post request.

Returns:

See Also:

Since:

  • 0.2.0



619
620
621
# File 'lib/ronin/network/extensions/http/net.rb', line 619

def Net.http_post_headers(options={})
  Net.http_post(options).to_hash
end

.http_powered_by(options = {}) ⇒ String

Sends an HTTP Head request and returns the HTTP X-Powered-By header.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :method (Symbol, String) — default: :get

    The method to use for the request.

Returns:

  • (String)

    The HTTP X-Powered-By header.

See Also:



304
305
306
307
308
# File 'lib/ronin/network/extensions/http/net.rb', line 304

def Net.http_powered_by(options={})
  options = {:method => :get}.merge(options)
  
  return Net.http_request(options)['x-powered-by']
end

.http_prop_find(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Propfind request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
# File 'lib/ronin/network/extensions/http/net.rb', line 663

def Net.http_prop_find(options={},&block)
  original_headers = options[:headers]

  # set the HTTP Depth header
  options[:headers] = {:depth => '0'}

  if original_headers
    options[:header].merge!(original_headers)
  end

  resp = Net.http_request(options.merge(:method => :propfind))

  yield resp if block_given?
  return resp
end

.http_prop_patch(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Proppatch request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



699
700
701
702
703
704
# File 'lib/ronin/network/extensions/http/net.rb', line 699

def Net.http_prop_patch(options={},&block)
  resp = Net.http_request(options.merge(:method => :proppatch))

  yield resp if block_given?
  return resp
end

.http_request(options = {}) {|request, (options)| ... } ⇒ Net::HTTP::Response

Connects to the HTTP server and sends an HTTP Request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :method (Symbol, String)

    The HTTP method to use in the request.

  • :path (String)

    The path to request from the HTTP server.

  • :headers (Hash)

    The Hash of the HTTP headers to send with the request. May contain either Strings or Symbols, lower-case or camel-case keys.

Yields:

  • (request, (options))

    If a block is given, it will be passed the HTTP request object. If the block has an arity of 2, it will also be passed the expanded version of the given options.

Yield Parameters:

  • request (Net::HTTP::Request)

    The HTTP request object to use in the request.

  • options (Hash)

    The expanded version of the given options.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/ronin/network/extensions/http/net.rb', line 202

def Net.http_request(options={},&block)
  resp = nil

  Net.http_session(options) do |http,expanded_options|
    req = Ronin::Network::HTTP.request(expanded_options)

    if block
      if block.arity == 2
        block.call(req,expanded_options)
      else
        block.call(req)
      end
    end

    resp = http.request(req)
  end

  return resp
end

.http_server(options = {}) ⇒ String

Sends a HTTP Head request and returns the HTTP Server header.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :method (Symbol, String) — default: :head

    The method to use for the request.

Returns:

  • (String)

    The HTTP Server header.

See Also:



282
283
284
285
286
# File 'lib/ronin/network/extensions/http/net.rb', line 282

def Net.http_server(options={})
  options = {:method => :head}.merge(options)

  return Net.http_request(options)['server']
end

.http_session(options = {}) {|session| ... } ⇒ nil

Creates a new temporary HTTP session with the server.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options

  • :ssl (Hash)

    a customizable set of options

Options Hash (options):

  • :url (String, URI::HTTP)

    The full URL to request.

  • :host (String)

    The host the HTTP server is running on.

  • :port (Integer) — default: Net::HTTP.default_port

    The port the HTTP server is listening on.

  • :user (String)

    The user to authenticate with when connecting to the HTTP server.

  • :password (String)

    The password to authenticate with when connecting to the HTTP server.

  • :proxy (String, Hash) — default: Ronin::Network::HTTP.proxy

    A Hash of proxy settings to use when connecting to the HTTP server.

  • :ssl (Boolean, Hash)

    Enables SSL for the HTTP connection.

Yields:

  • (session)

    If a block is given, it will be passed the newly created HTTP session object.

Yield Parameters:

  • session (Net::HTTP)

    The newly created HTTP session.

Returns:

  • (nil)

See Also:



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/ronin/network/extensions/http/net.rb', line 152

def Net.http_session(options={},&block)
  Net.http_connect(options) do |sess,expanded_options|
    if block
      if block.arity == 2
        block.call(sess,expanded_options)
      else
        block.call(sess)
      end
    end

    sess.finish
  end

  return nil
end

.http_status(options = {}) ⇒ Integer

Returns the Status Code of the Response.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :method (Symbol, String) — default: :head

    The method to use for the request.

Returns:

  • (Integer)

    The HTTP Response Status.

See Also:

Since:

  • 0.2.0



240
241
242
243
244
# File 'lib/ronin/network/extensions/http/net.rb', line 240

def Net.http_status(options={})
  options = {:method => :head}.merge(options)

  return Net.http_request(options).code.to_i
end

.http_trace(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Trace request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



726
727
728
729
730
731
# File 'lib/ronin/network/extensions/http/net.rb', line 726

def Net.http_trace(options={},&block)
  resp = Net.http_request(options.merge(:method => :trace))

  yield resp if block_given?
  return resp
end

.http_unlock(options = {}) {|response| ... } ⇒ Net::HTTP::Response

Performs an HTTP Unlock request.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (response)

    If a block is given, it will be passed the response received from the request.

Yield Parameters:

  • response (Net::HTTP::Response)

    The HTTP response object.

Returns:

  • (Net::HTTP::Response)

    The response of the HTTP request.

See Also:



753
754
755
756
757
758
# File 'lib/ronin/network/extensions/http/net.rb', line 753

def Net.http_unlock(options={},&block)
  resp = Net.http_request(options.merge(:method => :unlock))

  yield resp if block_given?
  return resp
end

.imap_connect(host, options = {}) {|session| ... } ⇒ Net::IMAP

Creates a connection to the IMAP server.

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :port (Integer) — default: IMAP.default_port

    The port the IMAP server is running on.

  • :certs (String)

    The path to the file containing CA certs of the server.

  • :auth (Symbol)

    The type of authentication to perform when connecting to the server. May be either :login or :cram_md5.

  • :user (String)

    The user to authenticate as when connecting to the server.

  • :password (String)

    The password to authenticate with when connecting to the server.

Yields:

  • (session)

    If a block is given, it will be passed the newly created IMAP session.

Yield Parameters:

  • session (Net::IMAP)

    The newly created IMAP session object.

Returns:

  • (Net::IMAP)

    The newly created IMAP session object.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ronin/network/extensions/imap/net.rb', line 63

def Net.imap_connect(host,options={})
  host = host.to_s
  port = (options[:port] || Ronin::Network::IMAP.default_port)
  certs = options[:certs]
  auth = options[:auth]
  user = options[:user]
  passwd = options[:password]

  if options[:ssl]
    ssl = true
    ssl_certs = options[:ssl][:certs]
    ssl_verify = Ronin::Network::SSL::VERIFY[options[:ssl][:verify]]
  else
    ssl = false
    ssl_verify = false
  end

  session = Net::IMAP.new(host,port,ssl,ssl_certs,ssl_verify)

  if user
    if auth == :cram_md5
      session.authenticate('CRAM-MD5',user,passwd)
    else
      session.authenticate('LOGIN',user,passwd)
    end
  end

  yield session if block_given?
  return session
end

.imap_session(host, options = {}) {|session| ... } ⇒ nil

Starts an IMAP session with the IMAP server.

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (session)

    If a block is given, it will be passed the newly created IMAP session. After the block has returned, the session will be closed.

Yield Parameters:

  • session (Net::IMAP)

    The newly created IMAP session object.

Returns:

  • (nil)

See Also:



116
117
118
119
120
121
122
123
124
125
# File 'lib/ronin/network/extensions/imap/net.rb', line 116

def Net.imap_session(host,options={})
  session = Net.imap_connect(host,options)

  yield session if block_given?

  session.logout if options[:user]
  session.close
  session.disconnect
  return nil
end

.pop3_connect(host, options = {}) {|session| ... } ⇒ Net::POP3

Creates a connection to the POP3 server.

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :port (Integer) — default: Ronin::Network::POP3.default_port

    The port the POP3 server is running on.

  • :user (String)

    The user to authenticate with when connecting to the POP3 server.

  • :password (String)

    The password to authenticate with when connecting to the POP3 server.

Yields:

  • (session)

    If a block is given, it will be passed the newly created POP3 session.

Yield Parameters:

  • session (Net::POP3)

    The newly created POP3 session.

Returns:

  • (Net::POP3)

    The newly created POP3 session.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ronin/network/extensions/pop3/net.rb', line 54

def Net.pop3_connect(host,options={})
  host = host.to_s
  port = (options[:port] || Ronin::Network::POP3.default_port)
  user = options[:user]
  password = options[:password]

  session = Net::POP3.start(host,port,user,password)

  yield session if block_given?
  return session
end

.pop3_session(host, options = {}) {|session| ... } ⇒ nil

Starts a session with the POP3 server.

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (session)

    If a block is given, it will be passed the newly created POP3 session. After the block has returned, the session will be closed.

Yield Parameters:

  • session (Net::POP3)

    The newly created POP3 session.

Returns:

  • (nil)


86
87
88
89
90
91
92
93
# File 'lib/ronin/network/extensions/pop3/net.rb', line 86

def Net.pop3_session(host,options={})
  session = Net.pop3_connect(host,options)

  yield session if block_given?

  session.finish
  return nil
end

.smtp_connect(host, options = {}) {|session| ... } ⇒ Net::SMTP

Creates a connection to the SMTP server.

Examples:

Net.smtp_connect('www.example.com', :user => 'joe')

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :port (Integer) — default: Ronin::Network::SMTP.default_port

    The port to connect to.

  • :helo (String)

    The HELO domain.

  • :auth (Symbol)

    The type of authentication to use. Can be either :login, :plain, or :cram_md5.

  • :user (String)

    The user-name to authenticate with.

  • :password (String)

    The password to authenticate with.

Yields:

  • (session)

    If a block is given, it will be passed an SMTP session object.

Yield Parameters:

  • session (Net::SMTP)

    The SMTP session.

Returns:

  • (Net::SMTP)

    The SMTP session.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ronin/network/extensions/smtp/net.rb', line 85

def Net.smtp_connect(host,options={})
  host = host.to_s
  port = (options[:port] || Ronin::Network::SMTP.default_port)

  helo = options[:helo]

  auth = options[:auth]
  user = options[:user]
  password = options[:password]

  session = Net::SMTP.start(host,port,helo,user,password,auth)

  yield session if block_given?
  return session
end

.smtp_message(options = {}) {|email| ... } ⇒ Object

Creates a new email message.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options for the email.

Yields:

  • (email)

    The given block will be passed the new email.

Yield Parameters:

See Also:

  • Ronin::Network::SMTP::Email.new


42
43
44
# File 'lib/ronin/network/extensions/smtp/net.rb', line 42

def Net.smtp_message(options={},&block)
  Ronin::Network::SMTP::Email.new(options,&block)
end

.smtp_send_message(host, options = {}) {|email| ... } ⇒ Object

Examples:

Net.smtp_send_message 'www.example.com', :to => '[email protected]',
                                         :from => '[email protected]',
                                         :subject => 'Hello',
                                         :message_id => 'XXXX',
                                         :body => 'Hello'

Using the block.

Net.smtp_send_message('www.example.com') do |email|
  email.to = '[email protected]'
  email.from '[email protected]'
  email.subject = 'Hello'
  email.message_id = 'XXXXXXXXXX'
  email.body << 'Hello!'
end

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional SMTP and Email options.

Yields:

  • (email)

    The given block will be passed the new email to be sent.

Yield Parameters:

See Also:

Since:

  • 0.2.0

  • 0.2.0



172
173
174
175
176
177
178
# File 'lib/ronin/network/extensions/smtp/net.rb', line 172

def Net.smtp_send_message(host,options={},&block)
  email = Net.smtp_message(options,&block)

  Net.smtp_session(host,options) do |smtp|
    smtp.send_message(email.to_s, email.from, email.to)
  end
end

.smtp_session(host, options = {}) {|session| ... } ⇒ Object

Starts a session with the SMTP server.

Examples:

Net.smtp_session('www.example.com', :user => 'joe') do |smtp|
  # ...
end

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (session)

    If a block is given, it will be passed an SMTP session object. After the block has returned, the session will be closed.

Yield Parameters:

  • session (Net::SMTP)

    The SMTP session.

See Also:



126
127
128
129
130
131
132
133
# File 'lib/ronin/network/extensions/smtp/net.rb', line 126

def Net.smtp_session(host,options={})
  session = Net.smtp_connect(host,options)

  yield session if block_given?

  session.finish
  return nil
end

.ssl_connect(host, port, options = {}) {|ssl_socket| ... } ⇒ OpenSSL::SSL::SSLSocket

Establishes a SSL connection.

Examples:

socket = Net.ssl_connect('twitter.com',443)

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :local_host (String)

    The local host to bind to.

  • :local_port (Integer)

    The local port to bind to.

  • :verify (Symbol)

    Specifies whether to verify the SSL certificate. May be one of the following:

    • :none
    • :peer
    • :client_once
    • :fail_if_no_peer_cert
  • :cert (String)

    The path to the SSL certificate.

  • :key (String)

    The path to the SSL key.

Yields:

  • (ssl_socket)

    The given block will be passed the new SSL Socket.

Yield Parameters:

  • ssl_socket (OpenSSL::SSL::SSLSocket)

    The new SSL Socket.

Returns:

  • (OpenSSL::SSL::SSLSocket)

    the new SSL Socket.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ronin/network/extensions/ssl/net.rb', line 76

def Net.ssl_connect(host,port,options={})
  local_host = options[:local_host]
  local_port = options[:local_port]

  socket = Net.tcp_connect(host,port,local_host,local_port)

  ssl_context = OpenSSL::SSL::SSLContext.new()
  ssl_context.verify_mode = Ronin::Network::SSL::VERIFY[options[:verify]]

  if options[:cert]
    cert_file = File.new(options[:cert])
    ssl_context.cert = OpenSSL::X509::Certificate.new(cert_file)
  end

  if options[:key]
    key_file = File.new(options[:key])
    ssl_context.key = OpenSSL::PKey::RSA.new(key_file)
  end

  ssl_socket = OpenSSL::SSL::SSLSocket.new(socket,ssl_context)
  ssl_socket.sync_close = true
  ssl_socket.connect

  yield ssl_socket if block_given?
  return ssl_socket
end

.ssl_session(host, port) {|ssl_socket| ... } ⇒ nil

Creates a new temporary SSL connection.

Examples:

Net.ssl_session('twitter.com',443) do |sock|
  sock.write("GET /\n\n")

  sock.each_line { |line| puts line }
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • options (Hash)

    Additional options.

Yields:

  • (ssl_socket)

    The given block will be passed the temporary SSL Socket.

Yield Parameters:

  • ssl_socket (OpenSSL::SSL::SSLSocket)

    The temporary SSL Socket.

Returns:

  • (nil)


147
148
149
150
151
152
153
154
# File 'lib/ronin/network/extensions/ssl/net.rb', line 147

def Net.ssl_session(host,port)
  ssl_socket = Net.ssl_connect(host,port)

  yield ssl_socket if block_given?

  ssl_socket.close
  return nil
end

.tcp_banner(host, port, local_host = nil, local_port = nil) {|banner| ... } ⇒ String

Reads the banner from the service running on the given host and port.

Examples:

Net.tcp_banner('pop.gmail.com',25)
# => "220 mx.google.com ESMTP c20sm3096959rvf.1"

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (banner)

    If a block is given, it will be passed the grabbed banner.

Yield Parameters:

  • banner (String)

    The grabbed banner.

Returns:

  • (String)

    The grabbed banner.



172
173
174
175
176
177
178
179
180
181
# File 'lib/ronin/network/extensions/tcp/net.rb', line 172

def Net.tcp_banner(host,port,local_host=nil,local_port=nil)
  banner = nil

  Net.tcp_session(host,port,local_host,local_port) do |sock|
    banner = sock.readline.strip
  end

  yield banner if block_given?
  return banner
end

.tcp_connect(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ TCPSocket

Creates a new TCPSocket object connected to a given host and port.

Examples:

Net.tcp_connect('www.hackety.org',80) # => TCPSocket
Net.tcp_connect('www.wired.com',80) do |sock|
  sock.write("GET /\n\n")
  puts sock.readlines
  sock.close
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.

Returns:

  • (TCPSocket)

    The newly created TCPSocket object.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ronin/network/extensions/tcp/net.rb', line 59

def Net.tcp_connect(host,port,local_host=nil,local_port=nil)
  host = host.to_s
  local_host = if local_host
                 local_host.to_s
               end

  sock = TCPSocket.new(host,port,local_host,local_port)

  yield sock if block_given?
  return sock
end

.tcp_connect_and_send(data, host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ Object

Creates a new TCPSocket object, connected to a given host and port. The given data will then be written to the newly created TCPSocket.

Parameters:

  • data (String)

    The data to send through the connection.

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.



98
99
100
101
102
103
104
# File 'lib/ronin/network/extensions/tcp/net.rb', line 98

def Net.tcp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
  sock = Net.tcp_connect(host,port,local_host,local_port)
  sock.write(data)

  yield sock if block_given?
  return sock
end

.tcp_send(data, host, port, local_host = nil, local_port = nil) ⇒ true

Connects to a specified host and port, sends the given data and then closes the connection.

Examples:

buffer = "GET /" + ('A' * 4096) + "\n\r"
Net.tcp_send(buffer,'victim.com',80)
# => true

Parameters:

  • data (String)

    The data to send through the connection.

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Returns:

  • (true)

    The data was successfully sent.



212
213
214
215
216
217
218
# File 'lib/ronin/network/extensions/tcp/net.rb', line 212

def Net.tcp_send(data,host,port,local_host=nil,local_port=nil)
  Net.tcp_session(host,port,local_host,local_port) do |sock|
    sock.write(data)
  end

  return true
end

.tcp_server(port, host = '0.0.0.0') {|server| ... } ⇒ TCPServer

Creates a new TCPServer listening on a given host and port.

Examples:

Net.tcp_server(1337)

Parameters:

  • port (Integer)

    The local port to listen on.

  • host (String) (defaults to: '0.0.0.0')

    ('0.0.0.0') The host to bind to.

Yields:

  • (server)

Returns:

  • (TCPServer)

    The new TCP server.



237
238
239
240
241
242
243
244
245
# File 'lib/ronin/network/extensions/tcp/net.rb', line 237

def Net.tcp_server(port,host='0.0.0.0')
  host = host.to_s

  server = TCPServer.new(host,port)
  server.listen(3)

  yield server if block_given?
  return server
end

.tcp_server_session(port, host = '0.0.0.0') {|server| ... } ⇒ nil

Creates a new temporary TCPServer listening on a host and port.

Examples:

Net.tcp_server_session(1337) do |server|
  client1 = server.accept
  client2 = server.accept

  client2.write(server.read_line)

  client1.close
  client2.close
end

Parameters:

  • port (Integer)

    The local port to bind to.

  • host (String) (defaults to: '0.0.0.0')

    ('0.0.0.0') The host to bind to.

Yields:

  • (server)

    The block which will be called after the server has been created. After the block has finished, the server will be closed.

Yield Parameters:

  • server (TCPServer)

    The newly created TCP server.

Returns:

  • (nil)


278
279
280
281
282
# File 'lib/ronin/network/extensions/tcp/net.rb', line 278

def Net.tcp_server_session(port,host='0.0.0.0',&block)
  server = Net.tcp_server(port,host,&block)
  server.close()
  return nil
end

.tcp_session(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ nil

Creates a new temporary TCPSocket object, connected to the given host and port.

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. After the block has returned, the socket will then be closed.

Yield Parameters:

  • socket (TCPsocket)

    The newly created TCPSocket object.

Returns:

  • (nil)


133
134
135
136
137
138
139
140
# File 'lib/ronin/network/extensions/tcp/net.rb', line 133

def Net.tcp_session(host,port,local_host=nil,local_port=nil)
  sock = Net.tcp_connect(host,port,local_host,local_port)

  yield sock if block_given?

  sock.close
  return nil
end

.tcp_single_server(port, host = '0.0.0.0') {|client| ... } ⇒ nil

Creates a new TCPServer listening on a given host and port, accepts only one client and then stops listening.

Examples:

Net.tcp_single_server(1337) do |client|
  client.puts 'lol'
end

Parameters:

  • port (Integer)

    After the block has finished, the client and the server will be closed.

Yield Parameters:

  • client (TCPSocket)

    The newly connected client.

Returns:

  • (nil)


304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/ronin/network/extensions/tcp/net.rb', line 304

def Net.tcp_single_server(port,host='0.0.0.0')
  host = host.to_s

  server = TCPServer.new(host,port)
  server.listen(1)

  client = server.accept

  yield client if block_given?

  client.close
  server.close
  return nil
end

.telnet_connect(host, options = {}) {|session| ... } ⇒ Net::Telnet

Creates a new Telnet connection.

Examples:

Net.telnet_connect('towel.blinkenlights.nl')
# => #<Net::Telnet: ...>

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :port (Integer) — default: Ronin::Network::Telnet.default_port

    The port to connect to.

  • :binmode (Boolean)

    Indicates that newline substitution shall not be performed.

  • :output_log (String)

    The name of the file to write connection status messages and all received traffic to.

  • :dump_log (String)

    Similar to the :output_log option, but connection output is also written in hexdump format.

  • :prompt (Regexp) — default: Ronin::Network::Telnet.default_prompt

    A regular expression matching the host command-line prompt sequence, used to determine when a command has finished.

  • :telnet (Boolean) — default: true

    Indicates that the connection shall behave as a telnet connection.

  • :plain (Boolean)

    Indicates that the connection shall behave as a normal TCP connection.

  • :timeout (Integer) — default: Ronin::Network::Telnet.default_timeout

    The number of seconds to wait before timing out both the initial attempt to connect to host, and all attempts to read data from the host.

  • :wait_time (Integer)

    The amount of time to wait after seeing what looks like a prompt.

  • :proxy (Net::Telnet, IO) — default: Ronin::Network::Telnet.proxy

    A proxy object to used instead of opening a direct connection to the host.

  • :user (String)

    The user to login as.

  • :password (String)

    The password to login with.

Yields:

  • (session)

    If a block is given, it will be passed the newly created Telnet session.

Yield Parameters:

  • session (Net::Telnet)

    The newly created Telnet session.

Returns:

  • (Net::Telnet)

    The Telnet session



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
# File 'lib/ronin/network/extensions/telnet/net.rb', line 93

def Net.telnet_connect(host,options={})
  host = host.to_s
  telnet_options = {}

  telnet_options['Host'] = host
  telnet_options['Port'] = (options[:port] || Ronin::Network::Telnet.default_port)
  telnet_options['Binmode'] = options[:binmode]
  telnet_options['Output_log'] = options[:output_log]
  telnet_options['Dump_log'] = options[:dump_log]
  telnet_options['Prompt'] = (options[:prompt] || Ronin::Network::Telnet.default_prompt)

  if (options[:telnet] && !options[:plain])
    telnet_options['Telnetmode'] = true
  end

  telnet_options['Timeout'] = (options[:timeout] || Ronin::Network::Telnet.default_timeout)
  telnet_options['Waittime'] = options[:wait_time]
  telnet_options['Proxy'] = (options[:proxy] || Ronin::Network::Telnet.proxy)

  user = options[:user]
  passwd = options[:passwd]

  session = Net::Telnet.new(telnet_options)
  session.(user,passwd) if user

  yield session if block_given?
  return session
end

.telnet_session(host, options = {}) {|session| ... } ⇒ nil

Starts a new Telnet session.

Examples:

Net.telnet_session('towel.blinkenlights.nl') do |movie|
  movie.each_line { |line| puts line }
end

Parameters:

  • host (String)

    The host to connect to.

  • options (Hash) (defaults to: {})

    Additional options.

Yields:

  • (session)

    If a block is given, it will be passed the newly created Telnet session. After the block has returned, the Telnet session will be closed.

Yield Parameters:

  • session (Net::Telnet)

    The newly created Telnet session.

Returns:

  • (nil)

See Also:



150
151
152
153
154
155
156
157
# File 'lib/ronin/network/extensions/telnet/net.rb', line 150

def Net.telnet_session(host,options={})
  session = Net.telnet_connect(host,options)

  yield session if block_given?

  session.close
  return nil
end

.udp_banner(host, port, local_host = nil, local_port = nil) {|banner| ... } ⇒ String

Reads the banner from the service running on the given host and port.

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (banner)

    If a block is given, it will be passed the grabbed banner.

Yield Parameters:

  • banner (String)

    The grabbed banner.

Returns:

  • (String)

    The grabbed banner.



170
171
172
173
174
175
176
177
178
179
# File 'lib/ronin/network/extensions/udp/net.rb', line 170

def Net.udp_banner(host,port,local_host=nil,local_port=nil)
  banner = nil

  Net.udp_session(host,port,local_host,local_port) do |sock|
    banner = sock.readline
  end

  yield banner if block_given?
  return banner
end

.udp_connect(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ UDPSocket

Creates a new UDPSocket object connected to a given host and port.

Examples:

Net.udp_connect('www.hackety.org',80)
# => UDPSocket
Net.udp_connect('www.wired.com',80) do |sock|
  puts sock.readlines
end

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

Returns:

  • (UDPSocket)

    The newly created UDPSocket object.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ronin/network/extensions/udp/net.rb', line 58

def Net.udp_connect(host,port,local_host=nil,local_port=nil)
  host = host.to_s
  local_host = if local_host
                 local_host.to_s
               end

  sock = UDPSocket.new(host,port,local_host,local_port)

  yield sock if block_given?
  return sock
end

.udp_connect_and_send(data, host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ UDPSocket

Creates a new UDPSocket object, connected to a given host and port. The given data will then be written to the newly created UDPSocket.

Parameters:

  • data (String)

    The data to send through the connection.

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

Returns:

  • (UDPSocket)

    The newly created UDPSocket object.



100
101
102
103
104
105
106
# File 'lib/ronin/network/extensions/udp/net.rb', line 100

def Net.udp_connect_and_send(data,host,port,local_host=nil,local_port=nil)
  sock = Net.udp_connect(host,port,local_host,local_port)
  sock.write(data)

  yield sock if block_given?
  return sock
end

.udp_server(port, host = '0.0.0.0') {|server| ... } ⇒ UDPServer

Creates a new UDPServer listening on a given host and port.

Examples:

Net.udp_server(1337)

Parameters:

  • port (Integer)

    The local port to listen on.

  • host (String) (defaults to: '0.0.0.0')

    ('0.0.0.0') The host to bind to.

Yields:

  • (server)

Returns:

  • (UDPServer)

    The new UDP server.



198
199
200
201
202
203
204
# File 'lib/ronin/network/extensions/udp/net.rb', line 198

def Net.udp_server(port,host='0.0.0.0')
  host = host.to_s
  server = UDPServer.new(host,port)

  yield server if block_given?
  return server
end

.udp_server_session(port, host = '0.0.0.0') {|server| ... } ⇒ nil

Creates a new temporary UDPServer listening on a given host and port.

Examples:

Net.udp_server_session(1337) do |server|
  data, sender = server.recvfrom(1024)
end

Parameters:

  • port (Integer)

    The local port to bind to.

  • host (String) (defaults to: '0.0.0.0')

    ('0.0.0.0') The host to bind to.

Yields:

  • (server)

    The block which will be called after the server has been created. After the block has finished, the server will be closed.

Yield Parameters:

  • server (UDPServer)

    The newly created UDP server.

Returns:

  • (nil)


231
232
233
234
235
# File 'lib/ronin/network/extensions/udp/net.rb', line 231

def Net.udp_server_session(port,host='0.0.0.0',&block)
  server = Net.udp_server(port,host,&block)
  server.close()
  return nil
end

.udp_session(host, port, local_host = nil, local_port = nil) {|socket| ... } ⇒ nil

Creates a new temporary UDPSocket object, connected to the given host and port.

Parameters:

  • host (String)

    The host to connect to.

  • port (Integer)

    The port to connect to.

  • local_host (String) (defaults to: nil)

    (nil) The local host to bind to.

  • local_port (Integer) (defaults to: nil)

    (nil) The local port to bind to.

Yields:

  • (socket)

    If a block is given, it will be passed the newly created socket. After the block has returned, the socket will then be closed.

Yield Parameters:

  • socket (UDPsocket)

    The newly created UDPSocket object.

Returns:

  • (nil)


135
136
137
138
139
140
141
142
# File 'lib/ronin/network/extensions/udp/net.rb', line 135

def Net.udp_session(host,port,local_host=nil,local_port=nil)
  sock = Net.udp_connect(host,port,local_host,local_port)

  yield sock if block_given?

  sock.close
  return nil
end