Skip to content

Commit 3cacddf

Browse files
authored
Merge pull request #231 from ninp0/master
PWN::Plugins::Sock module - implement #get_random_unused_port method …
2 parents bb7c4b7 + 33bf915 commit 3cacddf

File tree

6 files changed

+58
-18
lines changed

6 files changed

+58
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ $ rvm use ruby-3.2.2@pwn
3737
$ rvm list gemsets
3838
$ gem install --verbose pwn
3939
$ pwn
40-
pwn[v0.4.636]:001 >>> PWN.help
40+
pwn[v0.4.637]:001 >>> PWN.help
4141
```
4242

4343
[![Installing the pwn Security Automation Framework](https://raw.githubusercontent.com/0dayInc/pwn/master/documentation/pwn_install.png)](https://youtu.be/G7iLUY4FzsI)
@@ -52,7 +52,7 @@ $ rvm use ruby-3.2.2@pwn
5252
$ gem uninstall --all --executables pwn
5353
$ gem install --verbose pwn
5454
$ pwn
55-
pwn[v0.4.636]:001 >>> PWN.help
55+
pwn[v0.4.637]:001 >>> PWN.help
5656
```
5757

5858

lib/pwn/plugins/baresip.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ module BareSIP
8484
raise "no http_listen value found in #{config}." if http_list_entry.empty?
8585

8686
# Update http_listen value in respective config with random available port
87-
random_port = -1
88-
port_in_use = true
89-
while port_in_use
90-
random_port = Random.rand(1024..65_535)
91-
port_in_use = PWN::Plugins::Sock.check_port_in_use(port: random_port)
92-
end
87+
# random_port = -1
88+
# port_in_use = true
89+
# while port_in_use
90+
# random_port = Random.rand(1024..65_535)
91+
# port_in_use = PWN::Plugins::Sock.check_port_in_use(port: random_port)
92+
# end
93+
random_port = PWN::Plugins::Sock.get_random_unused_port
9394
http_listen_ip_port = "127.0.0.1:#{random_port}"
9495

9596
updated_config_content = ''

lib/pwn/plugins/burp_suite.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,11 @@ module BurpSuite
4040
burp_obj = {}
4141
burp_obj[:pid] = Process.spawn(burp_cmd_string)
4242
rest_browser = PWN::Plugins::TransparentBrowser.open(browser_type: :rest)
43-
burp_obj[:mitm_proxy] = '127.0.0.1:8080'
44-
burp_obj[:burpbuddy_api] = '127.0.0.1:8001'
43+
random_mitm_port = PWN::Plugins::Sock.get_random_unused_port
44+
random_bb_port = random_mitm_port
45+
random_bb_port = PWN::Plugins::Sock.get_random_unused_port while random_bb_port == random_mitm_port
46+
burp_obj[:mitm_proxy] = "127.0.0.1:#{random_mitm_port}"
47+
burp_obj[:burpbuddy_api] = "127.0.0.1:#{random_bb_port}"
4548
burp_obj[:rest_browser] = rest_browser
4649

4750
# Proxy always listens on localhost...use SSH tunneling if remote access is required
@@ -54,7 +57,7 @@ module BurpSuite
5457

5558
# Wait for TCP 8001 to open prior to returning burp_obj
5659
loop do
57-
s = TCPSocket.new('127.0.0.1', 8001)
60+
s = TCPSocket.new('127.0.0.1', random_bb_port)
5861
s.close
5962
break
6063
rescue Errno::ECONNREFUSED

lib/pwn/plugins/owasp_zap.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ module OwaspZap
7575
# api_key: 'required - api key for API authorization',
7676
# zap_bin_path: 'optional - path to zap.sh file'
7777
# headless: 'optional - run zap headless if set to true',
78-
# proxy: 'optional - change local zap proxy listener (defaults to http://127.0.0.1:8080)',
78+
# proxy: 'optional - change local zap proxy listener (defaults to http://127.0.0.1:<Random 1024-65535>)',
7979
# )
8080

8181
public_class_method def self.start(opts = {})
@@ -118,7 +118,8 @@ module OwaspZap
118118
proxy_uri = URI.parse(proxy)
119119
owasp_zap_cmd = "#{owasp_zap_cmd} -host #{proxy_uri.host} -port #{proxy_uri.port}"
120120
else
121-
proxy = 'http://127.0.0.1:8080'
121+
random_port = PWN::Plugins::Sock.get_random_unused_port
122+
proxy = "http://127.0.0.1:#{random_port}"
122123
proxy_uri = URI.parse(proxy)
123124
end
124125
zap_obj[:host] = proxy_uri.host.to_s.scrub
@@ -499,7 +500,7 @@ module OwaspZap
499500
api_key: 'required - api key for API authorization',
500501
zap_bin_path: 'optional - path to zap.sh file',
501502
headless: 'optional - run zap headless if set to true',
502-
proxy: 'optional - change local zap proxy listener (defaults to http://127.0.0.1:8080)'
503+
proxy: 'optional - change local zap proxy listener (defaults to http://127.0.0.1:<Random 1024-65535>)'
503504
)
504505
puts zap_obj.public_methods
505506

lib/pwn/plugins/sock.rb

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,40 @@ module Sock
4747
raise e
4848
end
4949

50+
# Supported Method Parameters::
51+
# PWN::Plugins::Sock.get_random_unused_port(
52+
# server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
53+
# protocol: 'optional - :tcp || :udp (defaults to tcp)'
54+
# )
55+
56+
public_class_method def self.get_random_unused_port(opts = {})
57+
server_ip = opts[:server_ip]
58+
server_ip ||= '127.0.0.1'
59+
port = -1
60+
protocol = opts[:protocol]
61+
protocol ||= :tcp
62+
63+
port_in_use = true
64+
while port_in_use
65+
port = Random.rand(1024..65_535)
66+
port_in_use = check_port_in_use(
67+
server_ip: server_ip,
68+
port: port,
69+
protocol: protocol
70+
)
71+
end
72+
73+
port
74+
rescue Errno::ECONNREFUSED,
75+
Errno::EHOSTUNREACH,
76+
Errno::ETIMEDOUT
77+
false
78+
end
79+
5080
# Supported Method Parameters::
5181
# PWN::Plugins::Sock.check_port_in_use(
52-
# port: 'required - target port',
5382
# server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
83+
# port: 'required - target port',
5484
# protocol: 'optional - :tcp || :udp (defaults to tcp)'
5585
# )
5686

@@ -163,9 +193,14 @@ module Sock
163193
tls: 'optional - boolean connect to target socket using TLS (defaults to false)'
164194
)
165195
166-
#{self}.check_port_availability(
167-
port: 'required - target port',
196+
port = #{self}.get_random_unused_port(
197+
server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
198+
protocol: 'optional - :tcp || :udp (defaults to tcp)'
199+
)
200+
201+
#{self}.check_port_in_use(
168202
server_ip: 'optional - target host or ip to check (Defaults to 127.0.0.1)',
203+
port: 'required - target port',
169204
protocol: 'optional - :tcp || :udp (defaults to tcp)'
170205
)
171206

lib/pwn/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module PWN
4-
VERSION = '0.4.636'
4+
VERSION = '0.4.637'
55
end

0 commit comments

Comments
 (0)