atomicshop 2.21.1__py3-none-any.whl → 3.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of atomicshop might be problematic. Click here for more details.

Files changed (29) hide show
  1. atomicshop/__init__.py +1 -1
  2. atomicshop/basics/multiprocesses.py +228 -30
  3. atomicshop/dns.py +2 -0
  4. atomicshop/mitm/config_static.py +2 -1
  5. atomicshop/mitm/engines/create_module_template.py +2 -7
  6. atomicshop/mitm/import_config.py +36 -44
  7. atomicshop/mitm/initialize_engines.py +9 -24
  8. atomicshop/mitm/mitm_main.py +187 -59
  9. atomicshop/networks.py +448 -0
  10. atomicshop/wrappers/ctyping/setup_device.py +466 -0
  11. atomicshop/wrappers/dockerw/dockerw.py +17 -21
  12. atomicshop/wrappers/mongodbw/mongodbw.py +1 -0
  13. atomicshop/wrappers/psutilw/{networks.py → psutil_networks.py} +3 -1
  14. atomicshop/wrappers/pywin32w/wmis/msft_netipaddress.py +76 -0
  15. atomicshop/wrappers/pywin32w/wmis/win32_networkadapterconfiguration.py +262 -0
  16. atomicshop/wrappers/pywin32w/wmis/win32networkadapter.py +51 -82
  17. atomicshop/wrappers/pywin32w/wmis/wmi_helpers.py +235 -0
  18. atomicshop/wrappers/socketw/accepter.py +15 -1
  19. atomicshop/wrappers/socketw/creator.py +7 -1
  20. atomicshop/wrappers/socketw/dns_server.py +33 -39
  21. atomicshop/wrappers/socketw/exception_wrapper.py +20 -11
  22. atomicshop/wrappers/socketw/socket_wrapper.py +29 -78
  23. atomicshop/wrappers/winregw/winreg_network.py +20 -0
  24. {atomicshop-2.21.1.dist-info → atomicshop-3.0.1.dist-info}/METADATA +2 -1
  25. {atomicshop-2.21.1.dist-info → atomicshop-3.0.1.dist-info}/RECORD +28 -24
  26. atomicshop/wrappers/pywin32w/wmis/helpers.py +0 -131
  27. {atomicshop-2.21.1.dist-info → atomicshop-3.0.1.dist-info}/LICENSE.txt +0 -0
  28. {atomicshop-2.21.1.dist-info → atomicshop-3.0.1.dist-info}/WHEEL +0 -0
  29. {atomicshop-2.21.1.dist-info → atomicshop-3.0.1.dist-info}/top_level.txt +0 -0
@@ -6,21 +6,59 @@ import os
6
6
 
7
7
  import atomicshop # Importing atomicshop package to get the version of the package.
8
8
 
9
- from .. import filesystem, dns, on_exit, print_api
9
+ from .. import filesystem, on_exit, print_api, networks, dns
10
10
  from ..permissions import permissions
11
11
  from ..python_functions import get_current_python_version_string, check_python_version_compliance
12
12
  from ..wrappers.socketw import socket_wrapper, dns_server, base
13
13
  from ..wrappers.loggingw import loggingw
14
14
  from ..wrappers.ctyping import win_console
15
15
 
16
- from .initialize_engines import ModuleCategory
17
16
  from .connection_thread_worker import thread_worker_main
18
17
  from . import config_static, recs_files
19
18
 
20
19
 
20
+ class NetworkSettings:
21
+ """
22
+ Class to store network settings.
23
+ """
24
+
25
+ def __init__(
26
+ self,
27
+ description: str | None = None,
28
+ interface_index: int | None = None,
29
+ is_dynamic: bool = False,
30
+ ipv4s: list[str] = None,
31
+ ipv6s: list[str] = None,
32
+ ipv4_subnet_masks: list[str] = None,
33
+ ipv6_prefixes: list[str] = None,
34
+ default_gateways: list[str] = None,
35
+ dns_gateways: list[str] = None
36
+ ):
37
+
38
+ self.description: str | None = description
39
+ self.interface_index: int | None = interface_index
40
+ self.is_dynamic: bool = is_dynamic
41
+ self.ipv4s: list[str] = ipv4s if ipv4s is not None else list()
42
+ self.ipv6s: list[str] = ipv6s if ipv6s is not None else list()
43
+ self.ipv4_subnet_masks: list[str] = ipv4_subnet_masks if ipv4_subnet_masks is not None else list()
44
+ self.ipv6_prefixes: list[str] = ipv6_prefixes if ipv6_prefixes is not None else list()
45
+ self.default_gateways: list[str] = default_gateways if default_gateways is not None else list()
46
+ self.dns_gateways: list[str] = dns_gateways if dns_gateways is not None else list()
47
+
48
+
49
+ # Global variables for setting the network interface to external IPs (eg: 192.168.0.1)
50
+ NETWORK_INTERFACE_SETTINGS: NetworkSettings = NetworkSettings()
51
+ CURRENT_IPV4S: list[str] = list()
52
+ CURRENT_IPV4_MASKS: list[str] = list()
53
+ IPS_TO_ASSIGN: list[str] = list()
54
+ MASKS_TO_ASSIGN: list[str] = list()
55
+
56
+ # Global variables for setting the network interface to localhost IPs (eg: 127.0.0.1), Only DNS gateway is set.
21
57
  NETWORK_INTERFACE_IS_DYNAMIC: bool = bool()
22
58
  NETWORK_INTERFACE_IPV4_ADDRESS_LIST: list[str] = list()
23
59
  IS_SET_DNS_GATEWAY: bool = False
60
+
61
+
24
62
  # noinspection PyTypeChecker
25
63
  RECS_PROCESS_INSTANCE: multiprocessing.Process = None
26
64
 
@@ -30,9 +68,6 @@ EXCEPTIONS_CSV_LOGGER_HEADER: str = 'time,exception'
30
68
  # noinspection PyTypeChecker
31
69
  MITM_ERROR_LOGGER: loggingw.ExceptionCsvLogger = None
32
70
 
33
- # Create request domain queue.
34
- DOMAIN_QUEUE: multiprocessing.Queue = multiprocessing.Queue()
35
-
36
71
  # Create logger's queue.
37
72
  NETWORK_LOGGER_QUEUE: multiprocessing.Queue = multiprocessing.Queue()
38
73
 
@@ -44,20 +79,39 @@ except win_console.NotWindowsConsoleError:
44
79
 
45
80
 
46
81
  def exit_cleanup():
47
- if permissions.is_admin() and IS_SET_DNS_GATEWAY:
48
- is_dns_dynamic, current_dns_gateway = dns.get_default_dns_gateway()
49
- status_string = 'Dynamic' if is_dns_dynamic else 'Static'
50
- print_api.print_api(f'Current DNS Gateway: {status_string}, {current_dns_gateway}')
82
+ if config_static.ENGINES_LIST[0].is_localhost:
83
+ if permissions.is_admin() and IS_SET_DNS_GATEWAY:
84
+ is_dns_dynamic, current_dns_gateway = dns.get_default_dns_gateway()
85
+ status_string = 'Dynamic' if is_dns_dynamic else 'Static'
86
+ print_api.print_api(f'Current DNS Gateway: {status_string}, {current_dns_gateway}')
87
+
88
+ if is_dns_dynamic != NETWORK_INTERFACE_IS_DYNAMIC or \
89
+ (not is_dns_dynamic and current_dns_gateway != NETWORK_INTERFACE_IPV4_ADDRESS_LIST):
90
+ if NETWORK_INTERFACE_IS_DYNAMIC:
91
+ dns.set_connection_dns_gateway_dynamic(use_default_connection=True)
92
+ else:
93
+ dns.set_connection_dns_gateway_static(
94
+ dns_servers=NETWORK_INTERFACE_IPV4_ADDRESS_LIST, use_default_connection=True)
51
95
 
52
- if is_dns_dynamic != NETWORK_INTERFACE_IS_DYNAMIC or \
53
- (not is_dns_dynamic and current_dns_gateway != NETWORK_INTERFACE_IPV4_ADDRESS_LIST):
54
- if NETWORK_INTERFACE_IS_DYNAMIC:
55
- dns.set_connection_dns_gateway_dynamic(use_default_connection=True)
56
- else:
57
- dns.set_connection_dns_gateway_static(
58
- dns_servers=NETWORK_INTERFACE_IPV4_ADDRESS_LIST, use_default_connection=True)
96
+ print_api.print_api("Returned default DNS gateway...", color='blue')
97
+ else:
98
+ # Get current network interface state.
99
+ default_network_adapter_config, default_network_adapter, default_adapter_info = networks.get_wmi_network_adapter_configuration(
100
+ use_default_interface=True, get_info_from_network_config=True)
59
101
 
60
- print_api.print_api("Returned default DNS gateway...", color='blue')
102
+ if NETWORK_INTERFACE_SETTINGS.is_dynamic:
103
+ # If the network interface was dynamic before the script started, we will return it to dynamic.
104
+ networks.set_dynamic_ip_for_adapter(default_network_adapter_config)
105
+ else:
106
+ networks.set_static_ip_for_adapter(
107
+ default_network_adapter,
108
+ ips=NETWORK_INTERFACE_SETTINGS.ipv4s,
109
+ masks=NETWORK_INTERFACE_SETTINGS.ipv4_subnet_masks,
110
+ gateways=NETWORK_INTERFACE_SETTINGS.default_gateways,
111
+ dns_gateways=NETWORK_INTERFACE_SETTINGS.dns_gateways
112
+ )
113
+
114
+ print_api.print_api("Returned network adapter settings...", color='blue')
61
115
 
62
116
  # The process will not be executed if there was an exception in the beginning.
63
117
  if RECS_PROCESS_INSTANCE is not None:
@@ -137,16 +191,12 @@ def startup_output(system_logger, script_version: str):
137
191
  f"{engine.recorder_class_object.__name__}")
138
192
  print_api.print_api(message, logger=system_logger)
139
193
  print_api.print_api(f"[*] Name: {engine.engine_name}", logger=system_logger)
140
- print_api.print_api(f"[*] Domains: {engine.domain_list}", logger=system_logger)
141
- print_api.print_api(f"[*] DNS Target: {engine.dns_target}", logger=system_logger)
142
- print_api.print_api(f"[*] TCP Listening Interfaces: {engine.tcp_listening_address_list}", logger=system_logger)
143
-
144
- if engine.no_sni.get_from_dns:
145
- print_api.print_api(f"[*] No SNI setting: Will fetch from DNS Server", logger=system_logger)
146
- if engine.no_sni.serve_domain_on_address_enable:
147
- print_api.print_api(
148
- f"[*] No SNI setting: The DNS Server will send the domains to interfaces [{engine.no_sni.serve_domain_on_address_dict}]",
149
- logger=system_logger)
194
+ print_api.print_api(f"[*] Domains: {list(engine.domain_target_dict.keys())}", logger=system_logger)
195
+ dns_targets: list = list()
196
+ for domain, ip_port in engine.domain_target_dict.items():
197
+ dns_targets.append(ip_port['ip'])
198
+ print_api.print_api(f"[*] DNS Targets: {dns_targets}", logger=system_logger)
199
+ # print_api.print_api(f"[*] TCP Listening Interfaces: {engine.tcp_listening_address_list}", logger=system_logger)
150
200
 
151
201
  if config_static.DNSServer.enable:
152
202
  print_api.print_api("DNS Server is enabled.", logger=system_logger)
@@ -178,6 +228,68 @@ def startup_output(system_logger, script_version: str):
178
228
  print_api.print_api("TCP Server is disabled.", logger=system_logger, color="yellow")
179
229
 
180
230
 
231
+ def get_ipv4s_for_tcp_server():
232
+ """
233
+ Function to get the IPv4 addresses for the default network adapter to set them to the adapter.
234
+ """
235
+
236
+ # Create a list of all the domains in all the engines.
237
+ domains_to_create_ips_for: list[str] = list()
238
+ for engine in config_static.ENGINES_LIST:
239
+ domains_to_create_ips_for += list(engine.domain_target_dict.keys())
240
+
241
+ engine_ips: list[str] = list()
242
+ # Check if we need the localhost ips (12.0.0.1) or external local ips (192.168.0.100).
243
+ if config_static.ENGINES_LIST[0].is_localhost:
244
+ create_ips: int = len(domains_to_create_ips_for)
245
+
246
+ # Generate the list of localhost ips.
247
+ for i in range(create_ips):
248
+ engine_ips.append(f"127.0.0.{i+1}")
249
+ else:
250
+ # Get current network interface state.
251
+ default_network_adapter_config, default_network_adapter, default_adapter_info = networks.get_wmi_network_adapter_configuration(
252
+ use_default_interface=True, get_info_from_network_config=True)
253
+
254
+ global NETWORK_INTERFACE_SETTINGS
255
+ NETWORK_INTERFACE_SETTINGS = NetworkSettings(
256
+ description=default_adapter_info['description'],
257
+ interface_index=default_adapter_info['interface_index'],
258
+ is_dynamic=default_adapter_info['is_dynamic'],
259
+ ipv4s=default_adapter_info['ipv4s'],
260
+ ipv6s=default_adapter_info['ipv6s'],
261
+ ipv4_subnet_masks=default_adapter_info['ipv4_subnet_masks'],
262
+ ipv6_prefixes=default_adapter_info['ipv6_prefixes'],
263
+ default_gateways=default_adapter_info['default_gateways'],
264
+ dns_gateways=default_adapter_info['dns_gateways']
265
+ )
266
+
267
+ # Adding IP addresses to the default network adapter.
268
+ current_ipv4s: list[str] = default_adapter_info['ipv4s']
269
+ current_ips_count: int = len(current_ipv4s)
270
+
271
+ # If the number of currently assigned IPs is smaller than the number of IPs to create,
272
+ # subtract the current IPs count from the number of IPs to create, to create only what is missing.
273
+ create_ips: int = len(domains_to_create_ips_for)
274
+ if current_ips_count <= create_ips:
275
+ create_ips -= current_ips_count
276
+
277
+ # Generate the IPs for the domains.
278
+ global CURRENT_IPV4S, CURRENT_IPV4_MASKS, IPS_TO_ASSIGN, MASKS_TO_ASSIGN
279
+ CURRENT_IPV4S, CURRENT_IPV4_MASKS, IPS_TO_ASSIGN, MASKS_TO_ASSIGN = networks.add_virtual_ips_to_default_adapter_by_current_setting(
280
+ number_of_ips=create_ips,
281
+ simulate_only=True)
282
+
283
+ engine_ips += CURRENT_IPV4S + IPS_TO_ASSIGN
284
+
285
+ # Add the ips to engines.
286
+ for engine in config_static.ENGINES_LIST:
287
+ for domain in engine.domain_target_dict.keys():
288
+ # If the domain is in the list of domains to create IPs for, add the IP to the engine.
289
+ if domain in domains_to_create_ips_for:
290
+ engine.domain_target_dict[domain]['ip'] = engine_ips.pop(0)
291
+
292
+
181
293
  def mitm_server(config_file_path: str, script_version: str):
182
294
  on_exit.register_exit_handler(exit_cleanup, at_exit=False, kill_signal=False)
183
295
 
@@ -192,6 +304,9 @@ def mitm_server(config_file_path: str, script_version: str):
192
304
  if result != 0:
193
305
  return result
194
306
 
307
+ # Get the IPs that will be set for the adapter and fill the engine configuration with the IPs.
308
+ get_ipv4s_for_tcp_server()
309
+
195
310
  global MITM_ERROR_LOGGER
196
311
  MITM_ERROR_LOGGER = loggingw.ExceptionCsvLogger(
197
312
  logger_name=EXCEPTIONS_CSV_LOGGER_NAME, directory_path=config_static.LogRec.logs_path)
@@ -253,7 +368,6 @@ def mitm_server(config_file_path: str, script_version: str):
253
368
  config_static.DNSServer.resolve_all_domains_to_ipv4_enable, config_static.DNSServer.target_ipv4),
254
369
  'offline_mode': config_static.MainConfig.offline,
255
370
  'cache_timeout_minutes': config_static.DNSServer.cache_timeout_minutes,
256
- 'request_domain_queue': DOMAIN_QUEUE,
257
371
  'logging_queue': NETWORK_LOGGER_QUEUE,
258
372
  'logger_name': network_logger_name
259
373
  },
@@ -331,7 +445,6 @@ def mitm_server(config_file_path: str, script_version: str):
331
445
  statistics_logs_directory=config_static.LogRec.logs_path,
332
446
  forwarding_dns_service_ipv4_list___only_for_localhost=[config_static.DNSServer.forwarding_dns_service_ipv4],
333
447
  skip_extension_id_list=config_static.SkipExtensions.SKIP_EXTENSION_ID_LIST,
334
- request_domain_from_dns_server_queue=DOMAIN_QUEUE,
335
448
  no_engine_usage_enable=config_static.TCPServer.no_engines_usage_to_listen_addresses_enable,
336
449
  no_engines_listening_address_list=config_static.TCPServer.no_engines_listening_address_list,
337
450
  engines_list=config_static.ENGINES_LIST
@@ -349,42 +462,57 @@ def mitm_server(config_file_path: str, script_version: str):
349
462
  network_logger_queue_listener.stop()
350
463
  return 1
351
464
 
352
- # Before we start the loop. we can set the default gateway if specified.
353
- set_dns_gateway = False
354
- dns_gateway_server_list = list()
465
+ # ----------------------- Get the default network adapter configuration. --------------------------
466
+ # This setting is needed only for the dns gateways configurations from the main config on localhost.
467
+ set_local_dns_gateway: bool = False
468
+ # Set the default gateway if specified.
355
469
  if config_static.DNSServer.set_default_dns_gateway:
356
470
  dns_gateway_server_list = config_static.DNSServer.set_default_dns_gateway
357
- set_dns_gateway = True
471
+ set_local_dns_gateway = True
358
472
  elif config_static.DNSServer.set_default_dns_gateway_to_localhost:
359
473
  dns_gateway_server_list = [base.LOCALHOST_IPV4]
360
- set_dns_gateway = True
474
+ set_local_dns_gateway = True
361
475
  elif config_static.DNSServer.set_default_dns_gateway_to_default_interface_ipv4:
362
476
  dns_gateway_server_list = [base.DEFAULT_IPV4]
363
- set_dns_gateway = True
364
-
365
- if set_dns_gateway:
366
- global IS_SET_DNS_GATEWAY
367
- IS_SET_DNS_GATEWAY = True
368
-
369
- # Get current network interface state.
370
- global NETWORK_INTERFACE_IS_DYNAMIC, NETWORK_INTERFACE_IPV4_ADDRESS_LIST
371
- NETWORK_INTERFACE_IS_DYNAMIC, NETWORK_INTERFACE_IPV4_ADDRESS_LIST = dns.get_default_dns_gateway()
372
-
373
- # Set the DNS gateway to the specified one only if the DNS gateway is dynamic, or it is static but different
374
- # from the one specified in the configuration file.
375
- if (NETWORK_INTERFACE_IS_DYNAMIC or (not NETWORK_INTERFACE_IS_DYNAMIC and
376
- NETWORK_INTERFACE_IPV4_ADDRESS_LIST != dns_gateway_server_list)):
377
- try:
378
- dns.set_connection_dns_gateway_static(
379
- dns_servers=dns_gateway_server_list,
380
- use_default_connection=True
381
- )
382
- except PermissionError as e:
383
- print_api.print_api(e, error_type=True, color="red", logger=system_logger)
384
- # Wait for the message to be printed and saved to file.
385
- time.sleep(1)
386
- network_logger_queue_listener.stop()
387
- return 1
477
+ set_local_dns_gateway = True
478
+ else:
479
+ dns_gateway_server_list = NETWORK_INTERFACE_SETTINGS.dns_gateways
480
+
481
+ if config_static.ENGINES_LIST[0].is_localhost:
482
+ if set_local_dns_gateway:
483
+ global IS_SET_DNS_GATEWAY
484
+ IS_SET_DNS_GATEWAY = True
485
+
486
+ # Get current network interface state.
487
+ global NETWORK_INTERFACE_IS_DYNAMIC, NETWORK_INTERFACE_IPV4_ADDRESS_LIST
488
+ NETWORK_INTERFACE_IS_DYNAMIC, NETWORK_INTERFACE_IPV4_ADDRESS_LIST = dns.get_default_dns_gateway()
489
+
490
+ # Set the DNS gateway to the specified one only if the DNS gateway is dynamic, or it is static but different
491
+ # from the one specified in the configuration file.
492
+ if (NETWORK_INTERFACE_IS_DYNAMIC or (not NETWORK_INTERFACE_IS_DYNAMIC and
493
+ NETWORK_INTERFACE_IPV4_ADDRESS_LIST != dns_gateway_server_list)):
494
+ try:
495
+ dns.set_connection_dns_gateway_static(
496
+ dns_servers=dns_gateway_server_list,
497
+ use_default_connection=True
498
+ )
499
+ except PermissionError as e:
500
+ print_api.print_api(e, error_type=True, color="red", logger=system_logger)
501
+ # Wait for the message to be printed and saved to file.
502
+ time.sleep(1)
503
+ network_logger_queue_listener.stop()
504
+ return 1
505
+ else:
506
+ # Change the adapter settings and add the virtual IPs.
507
+ try:
508
+ networks.add_virtual_ips_to_default_adapter_by_current_setting(
509
+ virtual_ipv4s_to_add=IPS_TO_ASSIGN, virtual_ipv4_masks_to_add=MASKS_TO_ASSIGN, dns_gateways=dns_gateway_server_list)
510
+ except PermissionError as e:
511
+ print_api.print_api(e, error_type=True, color="red", logger=system_logger)
512
+ # Wait for the message to be printed and saved to file.
513
+ time.sleep(1)
514
+ network_logger_queue_listener.stop()
515
+ return 1
388
516
 
389
517
  statistics_writer = socket_wrapper_instance.statistics_writer
390
518