10
10
11
11
12
12
import typer
13
+ og_print = print
13
14
from rich import print
14
15
15
16
__VERSION__ = '' # this is updated with make release which rebuilds
@@ -37,7 +38,7 @@ def get_ip(host:str, verbose=True, loading_scope=False) -> ipaddress.IPv4Address
37
38
38
39
39
40
40
- def build_targets (scope_file ) -> set :
41
+ def build_scope (scope_file , showing : bool = False ) -> set :
41
42
targets = set ()
42
43
try :
43
44
f = open (scope_file )
@@ -69,29 +70,32 @@ def build_targets(scope_file) -> set:
69
70
print ('is an ip ' , line )
70
71
targets .add (ipaddress .IPv4Address (line ))
71
72
elif re .search (r'\d\/\d*$' , line ): # for cidr networks e.g. 10.0.0.0/8
72
- if VERBOSE :
73
- print ( 'is a cidr' , line )
74
- ip_net = ipaddress . ip_network ( line )
73
+
74
+ ip_net = ipaddress . ip_network ( line , strict = False )
75
+
75
76
net = [ip for ip in ip_net ] # add all the ips to the set...
77
+ if VERBOSE :
78
+ print (f'is a cidr { line } with { len (net )} IPs starting at { net [0 ]} and ending at { net [- 1 ]} ' )
76
79
targets .update (net )
77
80
elif re .search (r'\w' , line ): # is a hostname... we need the ip
81
+
82
+ ip = get_ip (line , loading_scope = True )
78
83
if VERBOSE :
79
- print ('is a hostname' , line )
80
- ip = get_ip (line , loading_scope = True )
84
+ print (f'is a hostname "{ line } " with ip { ip } ' )
81
85
targets .add (ip ) # add the ip to the set
82
86
targets .add (line ) # and the hostname...
83
87
try :
84
88
targets .remove (None ) # remove erroneous entries to the set
85
89
except KeyError as e :
86
90
pass
87
91
88
- if VERBOSE :
89
- num_targets = len (targets )
90
- if num_targets < 512 :
91
- print (targets ) # rich.print is slow on large collections.
92
- print (f'Total in scope IPS: { num_targets } ' )
93
- print ('-' * 30 )
94
-
92
+ # if VERBOSE and showing == False :
93
+ # num_targets = len(targets)
94
+ # if num_targets < 512:
95
+ # print(targets) # rich.print is slow on large collections.
96
+ # print(f'Total in scope IPS: {num_targets}')
97
+ # print('-'*30)
98
+
95
99
return targets
96
100
97
101
def check (in_scope_targets , potential_targets , verbose = False ):
@@ -120,18 +124,32 @@ def check(in_scope_targets, potential_targets, verbose=False):
120
124
121
125
122
126
@app .command ()
123
- def in_scope (targets :List [str ]= typer .Argument (None ), target_file :Path = None , scope_file :Path = f'./in_scope.txt' , verbose :bool = typer .Option (False , '-v' ), version :bool = typer .Option (False , '--version' )):
127
+ def in_scope (targets :List [str ]= typer .Argument (None ), target_file :Path = None , scope_file :Path = f'./in_scope.txt' , verbose :bool = typer .Option (False , '-v' ), show : bool = typer . Option ( False , '--show-ips' , '-s' ), version :bool = typer .Option (False , '--version' )):
124
128
"""Checks to see if one or more host IPs are in the scope file; """
125
129
if version :
126
130
print (__VERSION__ )
127
131
sys .exit ()
128
-
129
132
if verbose == True :
130
133
global VERBOSE
131
134
VERBOSE = True
132
135
133
- in_scope_ips = build_targets (scope_file )
134
-
136
+ in_scope_targets = build_scope (scope_file , showing = show )
137
+
138
+ if show :
139
+ in_scope_host : ipaddress .IPv4Address
140
+ # for in_scope_host in in_scope_targets: # 10 seconds for example scope; 65k ips
141
+ # if type(in_scope_host) == ipaddress.IPv4Address:
142
+
143
+ # print(str(in_scope_host))
144
+
145
+ ips = [str (x ) for x in in_scope_targets ]
146
+ print (sorted (ips )) # 5 seconds ; 65k ips
147
+ if VERBOSE :
148
+ print (f'Total in scope hosts (IPs and hostnames): { len (in_scope_targets )} ' )
149
+ sys .exit ()
150
+
151
+
152
+
135
153
if len (targets ) == 0 and target_file == None :
136
154
# print('using stdin')
137
155
targets = sys .stdin .read ().splitlines (keepends = False )
@@ -140,7 +158,7 @@ def in_scope(targets:List[str]=typer.Argument(None), target_file:Path=None, scop
140
158
targets = f .read ().splitlines (keepends = False )
141
159
142
160
143
- with WorkerPool (shared_objects = in_scope_ips ) as pool :
161
+ with WorkerPool (shared_objects = in_scope_targets ) as pool :
144
162
# https://slimmer-ai.github.io/mpire/v2.1.0/usage/worker_pool.html#shared-objects
145
163
# pool.set_shared_objects(in_scope_ips) # another way to do this inside of
146
164
pool .map (check , targets )
0 commit comments