Skip to content

Commit a1f4f16

Browse files
committed
fix: different python versions have different behaviors on ipaddress library
1 parent 2ef117e commit a1f4f16

File tree

2 files changed

+127
-3
lines changed

2 files changed

+127
-3
lines changed

pocsuite3/lib/core/common.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,16 +387,31 @@ def get_file_items(filename, comment_prefix='#', unicode=True, lowercase=False,
387387
def parse_target(address, additional_ports=[]):
388388
# parse IPv4/IPv6 CIDR
389389
targets = OrderedSet()
390+
is_ipv6 = False
390391
try:
391-
for ip in ip_network(address, strict=False).hosts():
392+
hosts = list(ip_network(address, strict=False).hosts())
393+
'''
394+
fix https://github.com/knownsec/pocsuite3/issues/319
395+
different python versions have different behaviors on ipaddress library
396+
'''
397+
try:
398+
t = ip_address(address.replace('/32', '').replace('/128', ''))
399+
if t not in hosts:
400+
hosts.append(t)
401+
except ValueError:
402+
pass
403+
404+
for ip in hosts:
392405

393406
if ip.version == 6:
407+
is_ipv6 = True
408+
if is_ipv6 and 'ipv6' in conf:
394409
conf.ipv6 = True
395410

396411
targets.add(str(ip))
397412

398413
for port in additional_ports:
399-
targets.add(f'[{ip}]:{port}' if conf.ipv6 else f'{ip}:{port}')
414+
targets.add(f'[{ip}]:{port}' if is_ipv6 else f'{ip}:{port}')
400415

401416
return targets
402417

@@ -406,6 +421,8 @@ def parse_target(address, additional_ports=[]):
406421
# URL
407422
try:
408423
if ip_address(urlparse(address).hostname).version == 6:
424+
is_ipv6 = True
425+
if is_ipv6 and 'ipv6' in conf:
409426
conf.ipv6 = True
410427
except ValueError:
411428
pass
@@ -415,7 +432,7 @@ def parse_target(address, additional_ports=[]):
415432
try:
416433
pr = urlparse(address)
417434
for port in additional_ports:
418-
netloc = f'[{pr.hostname}]:{port}' if conf.ipv6 else f'{pr.hostname}:{port}'
435+
netloc = f'[{pr.hostname}]:{port}' if is_ipv6 else f'{pr.hostname}:{port}'
419436
t = pr._replace(netloc=netloc).geturl()
420437
if t.startswith('tcp://'):
421438
t = t.lstrip('tcp://')

tests/test_parse_target.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import unittest
5+
from pocsuite3.lib.core.common import parse_target
6+
from pocsuite3.lib.core.common import OrderedSet
7+
8+
9+
class TestCase(unittest.TestCase):
10+
def setUp(self):
11+
pass
12+
13+
def tearDown(self):
14+
pass
15+
16+
def test_domain(self):
17+
result = OrderedSet()
18+
result.add('example.com')
19+
self.assertEqual(parse_target('example.com'), result)
20+
21+
def test_domain_url(self):
22+
result = OrderedSet()
23+
result.add('https://example.com/cgi-bin/test.cgi?a=b&c=d')
24+
self.assertEqual(parse_target('https://example.com/cgi-bin/test.cgi?a=b&c=d'), result)
25+
26+
def test_domain_url_with_additional_ports(self):
27+
result = OrderedSet()
28+
result.add('https://example.com/cgi-bin/test.cgi?a=b&c=d')
29+
result.add('https://example.com:8080/cgi-bin/test.cgi?a=b&c=d')
30+
result.add('https://example.com:8443/cgi-bin/test.cgi?a=b&c=d')
31+
self.assertEqual(parse_target('https://example.com/cgi-bin/test.cgi?a=b&c=d', [8080, 8443]), result)
32+
33+
def test_ipv4_url(self):
34+
result = OrderedSet()
35+
result.add('172.16.218.1/cgi-bin')
36+
self.assertEqual(parse_target('172.16.218.1/cgi-bin'), result)
37+
38+
def test_ipv6_url(self):
39+
result = OrderedSet()
40+
result.add('https://[fd12:3456:789a:1::f0]:8443/test')
41+
self.assertEqual(parse_target('https://[fd12:3456:789a:1::f0]:8443/test'), result)
42+
43+
def test_ipv4(self):
44+
result = OrderedSet()
45+
result.add('192.168.1.1')
46+
self.assertEqual(parse_target('192.168.1.1'), result)
47+
48+
def test_ipv4_cidr(self):
49+
result = OrderedSet()
50+
result.add('192.168.1.0')
51+
result.add('192.168.1.1')
52+
self.assertEqual(parse_target('192.168.1.1/31'), result)
53+
54+
def test_ipv4_cidr_with_host_32(self):
55+
result = OrderedSet()
56+
result.add('192.168.1.1')
57+
self.assertEqual(parse_target('192.168.1.1/32'), result)
58+
59+
def test_ipv4_with_additional_ports(self):
60+
result = OrderedSet()
61+
result.add('172.16.218.0')
62+
result.add('172.16.218.0:8080')
63+
result.add('172.16.218.0:8443')
64+
result.add('172.16.218.1')
65+
result.add('172.16.218.1:8080')
66+
result.add('172.16.218.1:8443')
67+
self.assertEqual(parse_target('172.16.218.1/31', [8080, 8443]), result)
68+
69+
def test_ipv6(self):
70+
result = OrderedSet()
71+
result.add('fd12:3456:789a:1::1')
72+
self.assertEqual(parse_target('fd12:3456:789a:1::1'), result)
73+
74+
def test_ipv6_cidr(self):
75+
result = OrderedSet()
76+
result.add('fd12:3456:789a:1::1')
77+
result.add('fd12:3456:789a:1::2')
78+
result.add('fd12:3456:789a:1::3')
79+
self.assertEqual(parse_target('fd12:3456:789a:1::/126'), result)
80+
81+
def test_ipv6_cidr_with_host_128(self):
82+
result = OrderedSet()
83+
result.add('fd12:3456:789a:1::')
84+
self.assertEqual(parse_target('fd12:3456:789a:1::/128'), result)
85+
86+
def test_ipv6_with_additional_ports(self):
87+
result = OrderedSet()
88+
result.add('fd12:3456:789a:1::1')
89+
result.add('[fd12:3456:789a:1::1]:8080')
90+
result.add('[fd12:3456:789a:1::1]:8443')
91+
result.add('fd12:3456:789a:1::2')
92+
result.add('[fd12:3456:789a:1::2]:8080')
93+
result.add('[fd12:3456:789a:1::2]:8443')
94+
result.add('fd12:3456:789a:1::3')
95+
result.add('[fd12:3456:789a:1::3]:8080')
96+
result.add('[fd12:3456:789a:1::3]:8443')
97+
self.assertEqual(parse_target('fd12:3456:789a:1::/126', [8080, 8443]), result)
98+
99+
def test_localhost(self):
100+
result = OrderedSet()
101+
result.add('localhost')
102+
self.assertEqual(parse_target('localhost'), result)
103+
104+
def test_random_str(self):
105+
result = OrderedSet()
106+
result.add('!@#$%^&*()_-+=:::::<>""{}[]:::8080')
107+
self.assertEqual(parse_target('!@#$%^&*()_-+=:::::<>""{}[]:::8080'), result)

0 commit comments

Comments
 (0)