-
Notifications
You must be signed in to change notification settings - Fork 10
/
initdb.py
76 lines (64 loc) · 2.09 KB
/
initdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import sys
import subprocess
import psycopg2
import htpasswd
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
def exec_cmd(cmd, cwd=None):
proc = subprocess.call(cmd, shell=True, cwd=cwd)
if proc:
raise Exception(f'[!] {cmd} failed')
print(f'[o] {cmd} succ')
path = "/root/.msf4/"
_pg_user = os.getenv('HOMADOS_PG_USER') or 'postgres'
_pg_pass = os.getenv('HOMADOS_PG_PASS') or 'homados@123'
_pg_host = os.getenv('HOMADOS_PG_SERVER') or '127.0.0.1'
_pg_port = os.getenv('HOMADOS_PG_PORT') or '5432'
# 判断是否存在.msf4文件夹,存在则代表已初始化
if not os.path.exists(path):
os.makedirs(path)
else:
sys.exit(0)
# 创建数据库配置文件
with open(f'{path}/database.yml', 'w') as f:
f.write(f'''
development: &pgsql
adapter: postgresql
database: msf
username: {_pg_user}
password: {_pg_pass}
host: {_pg_host}
port: {_pg_port}
pool: 200
production: &production
<<: *pgsql
test:
<<: *pgsql
database: msftest
username: {_pg_user}
password: {_pg_pass}
''')
# 创建并迁移msf数据库
exec_cmd('bundle exec rake db:create', cwd='/root/metasploit-framework')
exec_cmd('bundle exec rake db:migrate', cwd='/root/metasploit-framework')
# 创建homados数据库
conn = psycopg2.connect(dbname='postgres', user=_pg_user, password=_pg_pass, host=_pg_host, port=_pg_port)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = conn.cursor()
try:
cur.execute("CREATE DATABASE homados")
except psycopg2.errors.DuplicateDatabase as e:
print('database "homados" already exists')
# 迁移homaods数据库
exec_cmd('python3 manage.py migrate kb --database=kbase', cwd='/root/homados')
exec_cmd('python3 manage.py migrate', cwd='/root/homados')
exec_cmd('python3 manage.py createcachetable', cwd='/root/homados')
# 修改 nginx htpasswd
username = os.getenv('USERNAME')
password = os.getenv('PASSWORD')
if username and password:
with htpasswd.Basic('/etc/htpasswd') as userdb:
try:
userdb.add(username, password)
except htpasswd.basic.UserExists as e:
userdb.change_password(username, password)