Skip to content

Commit adb2743

Browse files
authored
Merge pull request #1 from iamOgunyinka/joshua
Joshua
2 parents d85cd7a + bcc4599 commit adb2743

File tree

8 files changed

+247
-168
lines changed

8 files changed

+247
-168
lines changed

.github/FUNDING.yml

Lines changed: 0 additions & 12 deletions
This file was deleted.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ wheels/
2323
*.egg-info/
2424
.installed.cfg
2525
*.egg
26+
*.test
2627
MANIFEST
2728

2829
# PyInstaller

Interlace/interlace.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
#!/usr/bin/python3
22
import sys
3+
34
from Interlace.lib.core.input import InputParser, InputHelper
45
from Interlace.lib.core.output import OutputHelper, Level
5-
from Interlace.lib.threader import Pool
6+
from Interlace.lib.threader import Pool, TaskBlock
7+
8+
9+
def print_command(level, command, message, output):
10+
if isinstance(command, TaskBlock):
11+
for c in command:
12+
print_command(level, c, message, output)
13+
else:
14+
output.terminal(Level.THREAD, command.name(), "Added to Queue")
615

716

817
def build_queue(arguments, output):
9-
queue = list()
10-
for command in InputHelper.process_commands(arguments):
11-
output.terminal(Level.THREAD, command, "Added to Queue")
12-
queue.append(command)
13-
return queue
18+
task_list = InputHelper.process_commands(arguments)
19+
for task in task_list:
20+
print_command(Level.THREAD, task, "Added to Queue", output)
21+
return task_list
1422

1523

1624
def main():

Interlace/lib/core/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '1.5.3'
1+
__version__ = '1.5.4'

Interlace/lib/core/input.py

Lines changed: 118 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
from argparse import ArgumentParser
2-
from netaddr import IPNetwork, IPRange, IPGlob
3-
from Interlace.lib.core.output import OutputHelper, Level
41
import os.path
5-
from os import access, W_OK
62
import sys
7-
from re import compile
8-
from random import sample
3+
from argparse import ArgumentParser
94
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
1010

1111

1212
class InputHelper(object):
@@ -62,135 +62,139 @@ def _get_cidr_to_ips(cidr_range):
6262
return ips
6363

6464
@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]
6976

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
73100

74-
tmp_commands.update(test)
75-
return tmp_commands
76-
77101
@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)
81121

82-
test = list()
122+
@staticmethod
123+
def _replace_variable_with_commands(commands, variable, replacements):
124+
foo = []
83125

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)
86129

87130
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
90146

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
93152

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]))
94158

95159
@staticmethod
96160
def process_commands(arguments):
97-
commands = set()
161+
commands = list()
98162
ranges = set()
99163
targets = set()
100164
exclusions_ranges = set()
101165
exclusions = set()
102-
final_commands = set()
103-
output = OutputHelper(arguments)
104166

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]
109169

110170
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)
120172

121173
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)
132175

133176
# process targets first
134177
if arguments.target:
135178
ranges.add(arguments.target)
136179
else:
137-
targetFile = arguments.target_list
180+
target_file = arguments.target_list
138181
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()])
143184

144185
# process exclusions first
145186
if arguments.exclusions:
146187
exclusions_ranges.add(arguments.exclusions)
147188
else:
148189
if arguments.exclusions_list:
149190
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)
173194

174195
# 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)
194198

195199
# difference operation
196200
targets -= exclusions
@@ -199,43 +203,38 @@ def process_commands(arguments):
199203
raise Exception("No target provided, or empty target list")
200204

201205
if arguments.command:
202-
commands.add(arguments.command.rstrip('\n'))
206+
commands.append(arguments.command.rstrip('\n'))
203207
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()
206210

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)
209213

210214
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)
212216

213217
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)
215219

216220
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])
218222

219223
if arguments.proto:
220224
if "," in arguments.proto:
221225
protocols = arguments.proto.split(",")
222226
else:
223227
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+
226230
# process proxies
227231
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
237235

238-
return final_commands
236+
InputHelper._replace_variable_array(commands, "_proxy_", proxy_list)
237+
return commands
239238

240239

241240
class InputParser(object):

Interlace/lib/core/output.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from colorclass import Color
2-
from colorclass import disable_all_colors, enable_all_colors, is_enabled
3-
from time import localtime, strftime
41
from enum import IntEnum
2+
from time import localtime, strftime
3+
4+
from colorclass import Color
5+
from colorclass import disable_all_colors
6+
57
from Interlace.lib.core.__version__ import __version__
68

79

@@ -40,7 +42,7 @@ def terminal(self, level, target, command, message=""):
4042
'target': target,
4143
'command': command,
4244
'message': message,
43-
'leader':leader
45+
'leader': leader
4446
}
4547

4648
if not self.silent:

0 commit comments

Comments
 (0)