1
- from argparse import ArgumentParser
2
- from netaddr import IPNetwork , IPRange , IPGlob
3
- from Interlace .lib .core .output import OutputHelper , Level
4
1
import os .path
5
- from os import access , W_OK
6
2
import sys
7
- from re import compile
8
- from random import sample
3
+ from argparse import ArgumentParser
9
4
from math import ceil
5
+ from os import access , W_OK
6
+ from random import sample
7
+
8
+ from netaddr import IPNetwork , IPRange , IPGlob
9
+ from Interlace .lib .threader import TaskBlock , Task
10
10
11
11
12
12
class InputHelper (object ):
@@ -62,135 +62,139 @@ def _get_cidr_to_ips(cidr_range):
62
62
return ips
63
63
64
64
@staticmethod
65
- def _replace_variable_for_commands (commands , variable , replacements ):
66
- tmp_commands = set ()
67
-
68
- test = list ()
65
+ def _process_port (port_type ):
66
+ if "," in port_type :
67
+ return port_type .split ("," )
68
+ elif "-" in port_type :
69
+ tmp = port_type .split ("-" )
70
+ begin_range = int (tmp [0 ])
71
+ end_range = int (tmp [1 ])
72
+ if begin_range >= end_range :
73
+ raise Exception ("Invalid range provided" )
74
+ return list (range (begin_range , end_range + 1 ))
75
+ return [port_type ]
69
76
70
- for replacement in replacements :
71
- for command in commands :
72
- test .append (str (command ).replace (variable , str (replacement )))
77
+ @staticmethod
78
+ def _pre_process_commands (command_list , task_name ):
79
+ task_block = TaskBlock (task_name )
80
+ parent_task = None
81
+ for command in command_list :
82
+ command = str (command ).strip ()
83
+ if not command :
84
+ continue
85
+ if command .startswith ('_block:' ) and command .endswith ('_' ):
86
+ new_task_name = command .split ('_block:' )[1 ][:- 1 ].strip ()
87
+ if task_name == new_task_name :
88
+ return task_block
89
+ task = InputHelper ._pre_process_commands (command_list , new_task_name )
90
+ else :
91
+ task = Task (command )
92
+ if command == '_blocker_' :
93
+ parent_task = task_block .last ()
94
+ parent_task .set_lock ()
95
+ continue
96
+ if parent_task :
97
+ task .wait_for (parent_task .get_lock ())
98
+ task_block .add_task (task )
99
+ return task_block
73
100
74
- tmp_commands .update (test )
75
- return tmp_commands
76
-
77
101
@staticmethod
78
- def _replace_variable_array (commands , variable , replacement ):
79
- tmp_commands = set ()
80
- counter = 0
102
+ def _pre_process_hosts (host_ranges , destination_set , arguments ):
103
+ for host in host_ranges :
104
+ host = host .replace (" " , "" )
105
+ for ips in host .split ("," ):
106
+ # check if it is a domain name
107
+ if ips .split ("." )[- 1 ][0 ].isalpha ():
108
+ destination_set .add (ips )
109
+ continue
110
+ # checking for CIDR
111
+ if not arguments .nocidr and "/" in ips :
112
+ destination_set .update (InputHelper ._get_cidr_to_ips (ips ))
113
+ # checking for IPs in a range
114
+ elif "-" in ips :
115
+ destination_set .update (InputHelper ._get_ips_from_range (ips ))
116
+ # checking for glob ranges
117
+ elif "*" in ips :
118
+ destination_set .update (InputHelper ._get_ips_from_glob (ips ))
119
+ else :
120
+ destination_set .add (ips )
81
121
82
- test = list ()
122
+ @staticmethod
123
+ def _replace_variable_with_commands (commands , variable , replacements ):
124
+ foo = []
83
125
84
- if not variable in sample (commands , 1 )[0 ]:
85
- return commands
126
+ def add_task (t ):
127
+ if t not in set (foo ):
128
+ foo .append (t )
86
129
87
130
for command in commands :
88
- test .append (str (command ).replace (variable , str (replacement [counter ])))
89
- counter += 1
131
+ is_task = not isinstance (command , TaskBlock )
132
+ for replacement in replacements :
133
+ if is_task and command .name ().find (variable ) != - 1 :
134
+ new_task = command .clone ()
135
+ new_task .replace (variable , replacement )
136
+ add_task (new_task )
137
+ elif is_task and command not in set (foo ):
138
+ add_task (command )
139
+ elif not is_task :
140
+ tasks = [task for task in command .get_tasks ()]
141
+ command .clear_tasks ()
142
+ for r in InputHelper ._replace_variable_with_commands (tasks , variable , replacements ):
143
+ command .add_task (r )
144
+ add_task (command )
145
+ return foo
90
146
91
- tmp_commands .update (test )
92
- return tmp_commands
147
+ @staticmethod
148
+ def _replace_variable_array (commands , variable , replacement ):
149
+ # TODO
150
+ if variable not in sample (commands , 1 )[0 ]:
151
+ return
93
152
153
+ for counter , command in enumerate (commands ):
154
+ if isinstance (command , TaskBlock ):
155
+ InputHelper ._replace_variable_array (command , variable , replacement )
156
+ else :
157
+ command .replace (variable , str (replacement [counter ]))
94
158
95
159
@staticmethod
96
160
def process_commands (arguments ):
97
- commands = set ()
161
+ commands = list ()
98
162
ranges = set ()
99
163
targets = set ()
100
164
exclusions_ranges = set ()
101
165
exclusions = set ()
102
- final_commands = set ()
103
- output = OutputHelper (arguments )
104
166
105
- # checking for whether output is writable and whether it exists
106
- if arguments .output :
107
- if not access (arguments .output , W_OK ):
108
- raise Exception ("Directory provided isn't writable" )
167
+ if arguments .output and arguments .output [- 1 ] == "/" :
168
+ arguments .output = arguments .output [:- 1 ]
109
169
110
170
if arguments .port :
111
- if "," in arguments .port :
112
- ports = arguments .port .split ("," )
113
- elif "-" in arguments .port :
114
- tmp_ports = arguments .port .split ("-" )
115
- if int (tmp_ports [0 ]) >= int (tmp_ports [1 ]):
116
- raise Exception ("Invalid range provided" )
117
- ports = list (range (int (tmp_ports [0 ]), int (tmp_ports [1 ]) + 1 ))
118
- else :
119
- ports = [arguments .port ]
171
+ ports = InputHelper ._process_port (arguments .port )
120
172
121
173
if arguments .realport :
122
- if "," in arguments .realport :
123
- real_ports = arguments .realport .split ("," )
124
- elif "-" in arguments .realport :
125
- tmp_ports = arguments .realport .split ("-" )
126
- if int (tmp_ports [0 ]) >= int (tmp_ports [1 ]):
127
- raise Exception ("Invalid range provided" )
128
- real_ports = list (range (int (tmp_ports [0 ]), int (tmp_ports [1 ]) + 1 ))
129
- else :
130
- real_ports = [arguments .realport ]
131
-
174
+ real_ports = InputHelper ._process_port (arguments .realport )
132
175
133
176
# process targets first
134
177
if arguments .target :
135
178
ranges .add (arguments .target )
136
179
else :
137
- targetFile = arguments .target_list
180
+ target_file = arguments .target_list
138
181
if not sys .stdin .isatty ():
139
- targetFile = sys .stdin
140
- for target in targetFile :
141
- if target .strip ():
142
- ranges .add (target .strip ())
182
+ target_file = sys .stdin
183
+ ranges .update ([target .strip () for target in target_file if target .strip ()])
143
184
144
185
# process exclusions first
145
186
if arguments .exclusions :
146
187
exclusions_ranges .add (arguments .exclusions )
147
188
else :
148
189
if arguments .exclusions_list :
149
190
for exclusion in arguments .exclusions_list :
150
- exclusions_ranges .add (target .strip ())
151
-
152
- # removing elements that may have spaces (helpful for easily processing comma notation)
153
- for target in ranges :
154
- target = target .replace (" " , "" )
155
-
156
- for ips in target .split ("," ):
157
-
158
- # check if it is a domain name
159
- if ips .split ("." )[- 1 ][0 ].isalpha ():
160
- targets .add (ips )
161
- continue
162
- # checking for CIDR
163
- if not arguments .nocidr and "/" in ips :
164
- targets .update (InputHelper ._get_cidr_to_ips (ips ))
165
- # checking for IPs in a range
166
- elif "-" in ips :
167
- targets .update (InputHelper ._get_ips_from_range (ips ))
168
- # checking for glob ranges
169
- elif "*" in ips :
170
- targets .update (InputHelper ._get_ips_from_glob (ips ))
171
- else :
172
- targets .add (ips )
191
+ exclusion = exclusion .strip ()
192
+ if exclusion :
193
+ exclusions .add (exclusion )
173
194
174
195
# removing elements that may have spaces (helpful for easily processing comma notation)
175
- for exclusion in exclusions_ranges :
176
- exclusion = exclusion .replace (" " , "" )
177
-
178
- for ips in exclusion .split ("," ):
179
- # check if it is a domain name
180
- if ips .split ("." )[- 1 ][0 ].isalpha ():
181
- targets .add (ips )
182
- continue
183
- # checking for CIDR
184
- if not arguments .nocidr and "/" in ips :
185
- exclusions .update (InputHelper ._get_cidr_to_ips (ips ))
186
- # checking for IPs in a range
187
- elif "-" in ips :
188
- exclusions .update (InputHelper ._get_ips_from_range (ips ))
189
- # checking for glob ranges
190
- elif "*" in ips :
191
- exclusions .update (InputHelper ._get_ips_from_glob (ips ))
192
- else :
193
- exclusions .add (ips )
196
+ InputHelper ._pre_process_hosts (ranges , targets , arguments )
197
+ InputHelper ._pre_process_hosts (exclusions_ranges , exclusions , arguments )
194
198
195
199
# difference operation
196
200
targets -= exclusions
@@ -199,43 +203,38 @@ def process_commands(arguments):
199
203
raise Exception ("No target provided, or empty target list" )
200
204
201
205
if arguments .command :
202
- commands .add (arguments .command .rstrip ('\n ' ))
206
+ commands .append (arguments .command .rstrip ('\n ' ))
203
207
else :
204
- for command in arguments .command_list :
205
- commands . add ( command . rstrip ( ' \n ' ) )
208
+ tasks = InputHelper . _pre_process_commands ( arguments .command_list , '' )
209
+ commands = tasks . get_tasks ( )
206
210
207
- final_commands = InputHelper ._replace_variable_for_commands (commands , "_target_" , targets )
208
- final_commands = InputHelper ._replace_variable_for_commands ( final_commands , "_host_" , targets )
211
+ commands = InputHelper ._replace_variable_with_commands (commands , "_target_" , targets )
212
+ commands = InputHelper ._replace_variable_with_commands ( commands , "_host_" , targets )
209
213
210
214
if arguments .port :
211
- final_commands = InputHelper ._replace_variable_for_commands ( final_commands , "_port_" , ports )
215
+ commands = InputHelper ._replace_variable_with_commands ( commands , "_port_" , ports )
212
216
213
217
if arguments .realport :
214
- final_commands = InputHelper ._replace_variable_for_commands ( final_commands , "_realport_" , real_ports )
218
+ commands = InputHelper ._replace_variable_with_commands ( commands , "_realport_" , real_ports )
215
219
216
220
if arguments .output :
217
- final_commands = InputHelper ._replace_variable_for_commands ( final_commands , "_output_" , [arguments .output ])
221
+ commands = InputHelper ._replace_variable_with_commands ( commands , "_output_" , [arguments .output ])
218
222
219
223
if arguments .proto :
220
224
if "," in arguments .proto :
221
225
protocols = arguments .proto .split ("," )
222
226
else :
223
227
protocols = arguments .proto
224
- final_commands = InputHelper ._replace_variable_for_commands ( final_commands , "_proto_" , protocols )
225
-
228
+ commands = InputHelper ._replace_variable_with_commands ( commands , "_proto_" , protocols )
229
+
226
230
# process proxies
227
231
if arguments .proxy_list :
228
- proxy_list = list ()
229
- for proxy in arguments .proxy_list :
230
- if proxy .strip ():
231
- proxy_list .append (proxy .strip ())
232
-
233
- if len (proxy_list ) < len (final_commands ):
234
- proxy_list = ceil (len (final_commands ) / len (proxy_list )) * proxy_list
235
-
236
- final_commands = InputHelper ._replace_variable_array (final_commands , "_proxy_" , proxy_list )
232
+ proxy_list = [proxy for proxy in arguments .proxy_list if proxy .strip ()]
233
+ if len (proxy_list ) < len (commands ):
234
+ proxy_list = ceil (len (commands ) / len (proxy_list )) * proxy_list
237
235
238
- return final_commands
236
+ InputHelper ._replace_variable_array (commands , "_proxy_" , proxy_list )
237
+ return commands
239
238
240
239
241
240
class InputParser (object ):
0 commit comments