Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix datetime.utcfromtimestamp() is deprecated in Python 3.12 #277

Merged
merged 7 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions regipy/plugins/software/winver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
from datetime import datetime, timezone

from regipy.hive_types import SOFTWARE_HIVE_TYPE
from regipy.plugins.plugin import Plugin
from regipy.utils import convert_wintime
from regipy.exceptions import RegistryKeyNotFoundException
import datetime

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,10 +56,8 @@ def run(self):
for val in key.iter_values():
if val.name in os_list:
if val.name == "InstallDate":
self.entries[WIN_VER_PATH][val.name] = (
datetime.datetime.utcfromtimestamp(val.value).strftime(
"%Y-%m-%d %H:%M:%S"
)
)
self.entries[WIN_VER_PATH][val.name] = datetime.fromtimestamp(
val.value, timezone.utc
).strftime("%Y-%m-%d %H:%M:%S")
else:
self.entries[WIN_VER_PATH][val.name] = val.value
171 changes: 105 additions & 66 deletions regipy/plugins/system/network_data.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import datetime
from regipy.exceptions import RegistryKeyNotFoundException

import logging
from datetime import datetime, timezone

from regipy.exceptions import RegistryKeyNotFoundException
from regipy.hive_types import SYSTEM_HIVE_TYPE
from regipy.plugins.plugin import Plugin
from regipy.utils import convert_wintime
Expand All @@ -21,60 +20,92 @@ def get_network_info(self, subkey, interfaces=None):
if interfaces is None:
interfaces = []

for interface in subkey.iter_subkeys():
entries = {
"interface_name": interface.name,
"last_modified": convert_wintime(
interface.header.last_modified, as_json=self.as_json
),
"dhcp_enabled": interface.get_value("EnableDHCP") == 1, # Boolean value
}

if entries["dhcp_enabled"]:
entries.update(
{
"dhcp_server": interface.get_value("DhcpServer"),
"dhcp_ip_address": interface.get_value("DhcpIPAddress"),
"dhcp_subnet_mask": interface.get_value("DhcpSubnetMask"),
"dhcp_default_gateway": interface.get_value(
"DhcpDefaultGateway"
),
"dhcp_name_server": interface.get_value("DhcpNameServer"),
"dhcp_domain": interface.get_value("DhcpDomain"),
}
)

lease_obtained_time = interface.get_value("LeaseObtainedTime")
if lease_obtained_time is not None:
lease_obtained_time_str = datetime.datetime.utcfromtimestamp(
lease_obtained_time
).strftime("%Y-%m-%d %H:%M:%S")
entries["dhcp_lease_obtained_time"] = lease_obtained_time_str

lease_terminates_time = interface.get_value("LeaseTerminatesTime")
if lease_terminates_time is not None:
lease_terminates_time_str = datetime.datetime.utcfromtimestamp(
lease_terminates_time
).strftime("%Y-%m-%d %H:%M:%S")
entries["dhcp_lease_terminates_time"] = lease_terminates_time_str

else:
entries.update(
{
"ip_address": interface.get_value("IPAddress"),
"subnet_mask": interface.get_value("SubnetMask"),
"default_gateway": interface.get_value("DefaultGateway"),
"name_server": interface.get_value("NameServer"),
"domain": interface.get_value("Domain"),
}
)

if interface.subkey_count:
sub_interfaces = []
self.get_network_info(self, interface, sub_interfaces)
entries["sub_interface"] = sub_interfaces

interfaces.append(entries)
try:
for interface in subkey.iter_subkeys():
entries = {
"interface_name": interface.name,
"last_modified": convert_wintime(
interface.header.last_modified, as_json=self.as_json
),
"incomplete_data": False, # New key to indicate incomplete data
"dhcp_enabled": interface.get_value("EnableDHCP")
== 1, # Boolean value
}

if entries["dhcp_enabled"]:
entries.update(
{
"dhcp_server": interface.get_value("DhcpServer"),
"dhcp_ip_address": interface.get_value("DhcpIPAddress"),
"dhcp_subnet_mask": interface.get_value("DhcpSubnetMask"),
"dhcp_default_gateway": interface.get_value(
"DhcpDefaultGateway"
),
"dhcp_name_server": interface.get_value("DhcpNameServer"),
"dhcp_domain": interface.get_value("DhcpDomain"),
}
)

# Lease Obtained Time
lease_obtained_time = interface.get_value("LeaseObtainedTime")
if lease_obtained_time is not None:
try:
lease_obtained_time_str = datetime.fromtimestamp(
lease_obtained_time, timezone.utc
).strftime("%Y-%m-%d %H:%M:%S")
entries["dhcp_lease_obtained_time"] = (
lease_obtained_time_str
)
except (OSError, ValueError) as e:
logger.error(
f"Error converting DHCP lease obtained time for interface {interface.name}: {e}"
)
entries["incomplete_data"] = True

# Lease Terminates Time
lease_terminates_time = interface.get_value("LeaseTerminatesTime")
if lease_terminates_time is not None:
try:
lease_terminates_time_str = datetime.fromtimestamp(
lease_terminates_time, timezone.utc
).strftime("%Y-%m-%d %H:%M:%S")
entries["dhcp_lease_terminates_time"] = (
lease_terminates_time_str
)
except (OSError, ValueError) as e:
logger.error(
f"Error converting DHCP lease terminates time for interface {interface.name}: {e}"
)
entries["incomplete_data"] = True

else:
entries.update(
{
"ip_address": interface.get_value("IPAddress"),
"subnet_mask": interface.get_value("SubnetMask"),
"default_gateway": interface.get_value("DefaultGateway"),
"name_server": interface.get_value("NameServer"),
"domain": interface.get_value("Domain"),
}
)

try:
if interface.subkey_count:
sub_interfaces = []
sub_interfaces = self.get_network_info(
interface, sub_interfaces
)
entries["sub_interface"] = sub_interfaces
except Exception as e:
logger.error(
f"Error processing sub-interfaces for interface {interface.name}: {e}"
)
entries["incomplete_data"] = True

interfaces.append(entries)

except Exception as e:
logger.error(f"Error iterating over subkeys in {subkey.path}: {e}")

return interfaces

Expand All @@ -87,14 +118,22 @@ def run(self):
try:
subkey = self.registry_hive.get_key(control_set_interfaces_path)
except RegistryKeyNotFoundException as ex:
logger.error(ex)
continue
logger.error(
f"Registry key not found at path {control_set_interfaces_path}: {ex}"
)
continue # Skip to the next path if the key is not found

self.entries[control_set_interfaces_path] = {
"timestamp": convert_wintime(
subkey.header.last_modified, as_json=self.as_json
try:
self.entries[control_set_interfaces_path] = {
"timestamp": convert_wintime(
subkey.header.last_modified, as_json=self.as_json
)
}
interfaces = []
interfaces = self.get_network_info(subkey, interfaces)
self.entries[control_set_interfaces_path]["interfaces"] = interfaces
except Exception as ex:
logger.error(
f"Error processing registry key {control_set_interfaces_path}: {ex}"
)
}
interfaces = []
interfaces = self.get_network_info(subkey, interfaces)
self.entries[control_set_interfaces_path]["interfaces"] = interfaces
self.entries[control_set_interfaces_path]["incomplete_data"] = True
10 changes: 5 additions & 5 deletions regipy/plugins/system/previous_winver.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging
from datetime import datetime, timezone
import re

from regipy.hive_types import SYSTEM_HIVE_TYPE
from regipy.plugins.plugin import Plugin
from regipy.exceptions import RegistryKeyNotFoundException
import datetime
import re

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -53,7 +53,7 @@ def run(self):
r"Updated on (\d{1,2})/(\d{1,2})/(\d{4}) (\d{2}):(\d{2}):(\d{2})",
sk.name,
)
dt = datetime.datetime(
dt = datetime(
int(old_date.group(3)),
int(old_date.group(1)),
int(old_date.group(2)),
Expand All @@ -66,8 +66,8 @@ def run(self):
for val in sk.iter_values():
if val.name in os_list:
if val.name == "InstallDate":
temp_dict[val.name] = datetime.datetime.utcfromtimestamp(
val.value
temp_dict[val.name] = datetime.fromtimestamp(
val.value, timezone.utc
).strftime("%Y-%m-%d %H:%M:%S")
else:
temp_dict[val.name] = val.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class NetworkDataPluginValidationCase(ValidationCase):
{
"interface_name": "{698E50A9-4F58-4D86-B61D-F42E58DCACF6}",
"last_modified": "2011-09-17T13:43:23.770078+00:00",
"incomplete_data": False,
"dhcp_enabled": False,
"ip_address": ["10.3.58.5"],
"subnet_mask": ["255.255.255.0"],
Expand All @@ -22,6 +23,7 @@ class NetworkDataPluginValidationCase(ValidationCase):
{
"interface_name": "{6AAFC9A9-0542-4DB2-8760-CCFFA953737C}",
"last_modified": "2011-09-17T13:43:23.770078+00:00",
"incomplete_data": False,
"dhcp_enabled": False,
"ip_address": ["192.168.1.123"],
"subnet_mask": ["255.255.255.0"],
Expand All @@ -32,6 +34,7 @@ class NetworkDataPluginValidationCase(ValidationCase):
{
"interface_name": "{e29ac6c2-7037-11de-816d-806e6f6e6963}",
"last_modified": "2011-09-17T13:43:23.770078+00:00",
"incomplete_data": False,
"dhcp_enabled": False,
"ip_address": None,
"subnet_mask": None,
Expand All @@ -47,6 +50,7 @@ class NetworkDataPluginValidationCase(ValidationCase):
{
"interface_name": "{698E50A9-4F58-4D86-B61D-F42E58DCACF6}",
"last_modified": "2011-09-17T13:43:23.770078+00:00",
"incomplete_data": False,
"dhcp_enabled": False,
"ip_address": ["10.3.58.5"],
"subnet_mask": ["255.255.255.0"],
Expand All @@ -57,6 +61,7 @@ class NetworkDataPluginValidationCase(ValidationCase):
{
"interface_name": "{6AAFC9A9-0542-4DB2-8760-CCFFA953737C}",
"last_modified": "2011-09-17T13:43:23.770078+00:00",
"incomplete_data": False,
"dhcp_enabled": False,
"ip_address": ["192.168.1.123"],
"subnet_mask": ["255.255.255.0"],
Expand All @@ -67,6 +72,7 @@ class NetworkDataPluginValidationCase(ValidationCase):
{
"interface_name": "{e29ac6c2-7037-11de-816d-806e6f6e6963}",
"last_modified": "2011-09-17T13:43:23.770078+00:00",
"incomplete_data": False,
"dhcp_enabled": False,
"ip_address": None,
"subnet_mask": None,
Expand Down
Loading