2022-07-18 23:59:14 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2016-06-04 05:42:26 +00:00
|
|
|
import os
|
2020-07-30 13:14:31 +00:00
|
|
|
import errno
|
2016-06-04 05:42:26 +00:00
|
|
|
import sqlite3
|
2016-06-09 03:44:45 +00:00
|
|
|
import shutil
|
|
|
|
import cme
|
2019-11-10 21:42:04 +00:00
|
|
|
import configparser
|
|
|
|
from configparser import ConfigParser, NoSectionError, NoOptionError
|
2023-03-02 16:01:29 +00:00
|
|
|
from cme.paths import CME_PATH, CONFIG_PATH, CERT_PATH, TMP_PATH
|
2023-03-03 14:50:43 +00:00
|
|
|
from cme.cmedb import initialize_db
|
2016-06-09 03:44:45 +00:00
|
|
|
from subprocess import check_output, PIPE
|
2022-04-21 17:36:19 +00:00
|
|
|
import sys
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2018-03-01 19:36:17 +00:00
|
|
|
|
2016-06-04 05:42:26 +00:00
|
|
|
def first_run_setup(logger):
|
2016-09-12 06:52:50 +00:00
|
|
|
if not os.path.exists(TMP_PATH):
|
|
|
|
os.mkdir(TMP_PATH)
|
|
|
|
|
2016-06-04 05:42:26 +00:00
|
|
|
if not os.path.exists(CME_PATH):
|
2016-06-09 03:44:45 +00:00
|
|
|
logger.info('First time use detected')
|
2016-12-15 07:28:00 +00:00
|
|
|
logger.info('Creating home directory structure')
|
2016-06-04 05:42:26 +00:00
|
|
|
os.mkdir(CME_PATH)
|
2017-03-27 21:09:36 +00:00
|
|
|
|
2022-03-13 12:01:04 +00:00
|
|
|
folders = ['logs', 'modules', 'protocols', 'workspaces', 'obfuscated_scripts', 'screenshots']
|
2017-03-27 21:09:36 +00:00
|
|
|
for folder in folders:
|
|
|
|
if not os.path.exists(os.path.join(CME_PATH, folder)):
|
2023-03-02 16:01:29 +00:00
|
|
|
logger.info("Creating missing folder {}".format(folder))
|
2018-03-01 19:36:17 +00:00
|
|
|
os.mkdir(os.path.join(CME_PATH, folder))
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2023-03-02 16:01:29 +00:00
|
|
|
initialize_db(logger)
|
2016-06-04 05:42:26 +00:00
|
|
|
|
2016-06-09 03:44:45 +00:00
|
|
|
if not os.path.exists(CONFIG_PATH):
|
|
|
|
logger.info('Copying default configuration file')
|
|
|
|
default_path = os.path.join(os.path.dirname(cme.__file__), 'data', 'cme.conf')
|
|
|
|
shutil.copy(default_path, CME_PATH)
|
2017-07-10 05:44:58 +00:00
|
|
|
else:
|
|
|
|
# This is just a quick check to make sure the config file isn't the old 3.x format
|
|
|
|
try:
|
2019-11-10 21:42:04 +00:00
|
|
|
config = configparser.ConfigParser()
|
2017-07-10 05:44:58 +00:00
|
|
|
config.read(CONFIG_PATH)
|
2018-03-01 19:36:17 +00:00
|
|
|
config.get('CME', 'workspace')
|
|
|
|
config.get('CME', 'pwn3d_label')
|
2022-02-07 21:19:46 +00:00
|
|
|
config.get('CME', 'audit_mode')
|
|
|
|
config.get('BloodHound', 'bh_enabled')
|
2023-02-21 19:40:52 +00:00
|
|
|
config.get('CME', 'log_mode')
|
2018-03-01 19:36:17 +00:00
|
|
|
except (NoSectionError, NoOptionError):
|
|
|
|
logger.info('Old configuration file detected, replacing with new version')
|
2017-07-10 05:44:58 +00:00
|
|
|
default_path = os.path.join(os.path.dirname(cme.__file__), 'data', 'cme.conf')
|
|
|
|
shutil.copy(default_path, CME_PATH)
|
2016-06-09 03:44:45 +00:00
|
|
|
|
2023-01-02 11:55:03 +00:00
|
|
|
# if not os.path.exists(CERT_PATH):
|
|
|
|
# logger.info('Generating SSL certificate')
|
|
|
|
# try:
|
|
|
|
# check_output(['openssl', 'help'], stderr=PIPE)
|
|
|
|
# if os.name != 'nt':
|
|
|
|
# os.system('openssl req -new -x509 -keyout {path} -out {path} -days 365 -nodes -subj "/C=US" > /dev/null 2>&1'.format(path=CERT_PATH))
|
|
|
|
# else:
|
|
|
|
# os.system('openssl req -new -x509 -keyout {path} -out {path} -days 365 -nodes -subj "/C=US"'.format(path=CERT_PATH))
|
|
|
|
# except OSError as e:
|
|
|
|
# if e.errno == errno.ENOENT:
|
|
|
|
# logger.error('OpenSSL command line utility is not installed, could not generate certificate, using default certificate')
|
|
|
|
# default_path = os.path.join(os.path.dirname(cme.__file__), 'data', 'default.pem')
|
|
|
|
# shutil.copy(default_path, CERT_PATH)
|
|
|
|
# else:
|
|
|
|
# logger.error('Error while generating SSL certificate: {}'.format(e))
|
|
|
|
# sys.exit(1)
|