splunksecrets 1.1.0__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.
- splunksecrets/__init__.py +19 -0
- splunksecrets/cli.py +264 -0
- splunksecrets/dbconnect.py +156 -0
- splunksecrets/phantom.py +99 -0
- splunksecrets/splunk.py +131 -0
- splunksecrets-1.1.0.dist-info/METADATA +10 -0
- splunksecrets-1.1.0.dist-info/RECORD +9 -0
- splunksecrets-1.1.0.dist-info/WHEEL +4 -0
- splunksecrets-1.1.0.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
from .dbconnect import decrypt_dbconnect, encrypt_dbconnect, get_key_and_iv
|
|
2
|
+
from .splunk import decrypt, encrypt, encrypt_new
|
|
3
|
+
from .phantom import decrypt_phantom, encrypt_phantom, to_bytes
|
|
4
|
+
|
|
5
|
+
__all__ = [
|
|
6
|
+
"decrypt_dbconnect",
|
|
7
|
+
"encrypt_dbconnect",
|
|
8
|
+
"decrypt_splunk_legacy",
|
|
9
|
+
"decrypt_splunk",
|
|
10
|
+
"encrypt_splunk_legacy",
|
|
11
|
+
"encrypt_splunk",
|
|
12
|
+
"encrypt_new",
|
|
13
|
+
"decrypt",
|
|
14
|
+
"encrypt",
|
|
15
|
+
"get_key_and_iv",
|
|
16
|
+
"decrypt_phantom",
|
|
17
|
+
"encrypt_phantom",
|
|
18
|
+
"to_bytes",
|
|
19
|
+
]
|
splunksecrets/cli.py
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import click
|
|
2
|
+
import pcrypt
|
|
3
|
+
import re
|
|
4
|
+
from cryptography.hazmat.primitives import serialization
|
|
5
|
+
|
|
6
|
+
from .dbconnect import encrypt_dbconnect, decrypt_dbconnect
|
|
7
|
+
from .phantom import encrypt_phantom, decrypt_phantom
|
|
8
|
+
from .splunk import encrypt, encrypt_new, decrypt
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def __ensure_binary(ctx, param, value):
|
|
12
|
+
if value is None and not param.required:
|
|
13
|
+
return None
|
|
14
|
+
if isinstance(value, str):
|
|
15
|
+
value = value.encode()
|
|
16
|
+
return value
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def __ensure_int(ctx, param, value):
|
|
20
|
+
if value is None and not param.required:
|
|
21
|
+
return None
|
|
22
|
+
try:
|
|
23
|
+
return int(value)
|
|
24
|
+
except ValueError:
|
|
25
|
+
raise click.BadParameter(f"{param.name} should be int")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def __ensure_text(ctx, param, value):
|
|
29
|
+
if value is None and not param.required:
|
|
30
|
+
return None
|
|
31
|
+
if isinstance(value, bytes):
|
|
32
|
+
value = value.decode()
|
|
33
|
+
return value
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def __load_phantom_private_key(ctx, param, value):
|
|
37
|
+
if ctx.get_parameter_source(param.name).name != "ENVIRONMENT":
|
|
38
|
+
with open(value, "rb") as f:
|
|
39
|
+
value = f.read().strip()
|
|
40
|
+
|
|
41
|
+
# Validate the key loads
|
|
42
|
+
serialization.load_pem_private_key(value, password=None)
|
|
43
|
+
|
|
44
|
+
return value
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def __load_phantom_secret_key(ctx, param, value):
|
|
48
|
+
if ctx.get_parameter_source(param.name).name == "ENVIRONMENT":
|
|
49
|
+
return value
|
|
50
|
+
|
|
51
|
+
with open(value, "rb") as f:
|
|
52
|
+
value = f.read().strip()
|
|
53
|
+
m = re.search(
|
|
54
|
+
bytes(r"^SECRET_KEY = '(?P<secret_key>.+)'$"), value, flags=re.MULTILINE
|
|
55
|
+
)
|
|
56
|
+
if not m:
|
|
57
|
+
raise click.BadParameter("Malformed secret key file")
|
|
58
|
+
return m.groupdict()["secret_key"]
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def __load_splunk_secret(ctx, param, value):
|
|
62
|
+
if ctx.get_parameter_source(param.name).name != "ENVIRONMENT":
|
|
63
|
+
with open(value, "rb") as f:
|
|
64
|
+
value = f.read().strip()
|
|
65
|
+
elif isinstance(value, str):
|
|
66
|
+
value = bytes(value, encoding="utf-8")
|
|
67
|
+
|
|
68
|
+
return value.strip()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
@click.group()
|
|
72
|
+
def main():
|
|
73
|
+
pass
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
@main.command("dbconnect-encrypt")
|
|
77
|
+
@click.option(
|
|
78
|
+
"-S",
|
|
79
|
+
"--secret",
|
|
80
|
+
required=True,
|
|
81
|
+
envvar="DBCONNECT_SECRET",
|
|
82
|
+
callback=__load_splunk_secret,
|
|
83
|
+
)
|
|
84
|
+
@click.option(
|
|
85
|
+
"--password",
|
|
86
|
+
envvar="PASSWORD",
|
|
87
|
+
prompt=True,
|
|
88
|
+
hide_input=True,
|
|
89
|
+
callback=__ensure_text,
|
|
90
|
+
)
|
|
91
|
+
def dbconnect_encrypt(secret, password):
|
|
92
|
+
"""Encrypt password used for dbconnect identity"""
|
|
93
|
+
click.echo(encrypt_dbconnect(secret, password))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@main.command("dbconnect-decrypt")
|
|
97
|
+
@click.option(
|
|
98
|
+
"-S",
|
|
99
|
+
"--secret",
|
|
100
|
+
required=True,
|
|
101
|
+
envvar="DBCONNECT_SECRET",
|
|
102
|
+
callback=__load_splunk_secret,
|
|
103
|
+
)
|
|
104
|
+
@click.option("--ciphertext", envvar="PASSWORD", prompt=True, callback=__ensure_text)
|
|
105
|
+
def dbconnect_decrypt(secret, ciphertext):
|
|
106
|
+
"""Decrypt password used for dbconnect identity"""
|
|
107
|
+
click.echo(decrypt_dbconnect(secret, ciphertext))
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
@main.command("phantom-encrypt")
|
|
111
|
+
@click.option(
|
|
112
|
+
"-P",
|
|
113
|
+
"--private-key",
|
|
114
|
+
required=True,
|
|
115
|
+
envvar="PHANTOM_PRIVATE_KEY",
|
|
116
|
+
callback=__load_phantom_private_key,
|
|
117
|
+
)
|
|
118
|
+
@click.option(
|
|
119
|
+
"-S",
|
|
120
|
+
"--secret-key",
|
|
121
|
+
required=True,
|
|
122
|
+
envvar="PHANTOM_SECRET_KEY",
|
|
123
|
+
callback=__load_phantom_secret_key,
|
|
124
|
+
)
|
|
125
|
+
@click.option(
|
|
126
|
+
"--password",
|
|
127
|
+
envvar="PASSWORD",
|
|
128
|
+
prompt=True,
|
|
129
|
+
hide_input=True,
|
|
130
|
+
callback=__ensure_text,
|
|
131
|
+
)
|
|
132
|
+
@click.option(
|
|
133
|
+
"-A", "--asset-id", envvar="PHANTOM_ASSET_ID", prompt=True, callback=__ensure_int
|
|
134
|
+
)
|
|
135
|
+
def phantom_encrypt(private_key, secret_key, password, asset_id):
|
|
136
|
+
"""Encrypt password used for Phantom asset"""
|
|
137
|
+
click.echo(encrypt_phantom(private_key, secret_key, password, asset_id))
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
@main.command("phantom-decrypt")
|
|
141
|
+
@click.option(
|
|
142
|
+
"-P",
|
|
143
|
+
"--private-key",
|
|
144
|
+
required=True,
|
|
145
|
+
envvar="PHANTOM_PRIVATE_KEY",
|
|
146
|
+
callback=__load_phantom_private_key,
|
|
147
|
+
)
|
|
148
|
+
@click.option(
|
|
149
|
+
"-S",
|
|
150
|
+
"--secret-key",
|
|
151
|
+
required=True,
|
|
152
|
+
envvar="PHANTOM_SECRET_KEY",
|
|
153
|
+
callback=__load_phantom_secret_key,
|
|
154
|
+
)
|
|
155
|
+
@click.option("--ciphertext", envvar="PASSWORD", prompt=True, callback=__ensure_text)
|
|
156
|
+
@click.option(
|
|
157
|
+
"-A", "--asset-id", envvar="PHANTOM_ASSET_ID", prompt=True, callback=__ensure_int
|
|
158
|
+
)
|
|
159
|
+
def phantom_decrypt(private_key, secret_key, ciphertext, asset_id):
|
|
160
|
+
"""Decrypt password used for Phantom asset"""
|
|
161
|
+
click.echo(decrypt_phantom(private_key, secret_key, ciphertext, asset_id))
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
@main.command("splunk-encrypt")
|
|
165
|
+
@click.option(
|
|
166
|
+
"-S",
|
|
167
|
+
"--splunk-secret",
|
|
168
|
+
required=True,
|
|
169
|
+
envvar="SPLUNK_SECRET",
|
|
170
|
+
callback=__load_splunk_secret,
|
|
171
|
+
)
|
|
172
|
+
@click.option("-I", "--iv", envvar="SPLUNK_IV", callback=__ensure_binary)
|
|
173
|
+
@click.option(
|
|
174
|
+
"--password",
|
|
175
|
+
envvar="PASSWORD",
|
|
176
|
+
prompt=True,
|
|
177
|
+
hide_input=True,
|
|
178
|
+
callback=__ensure_text,
|
|
179
|
+
)
|
|
180
|
+
def splunk_encrypt(splunk_secret, password, iv=None):
|
|
181
|
+
"""Encrypt password using Splunk 7.2 algorithm"""
|
|
182
|
+
click.echo(encrypt_new(splunk_secret, password, iv))
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@main.command("splunk-decrypt")
|
|
186
|
+
@click.option(
|
|
187
|
+
"-S",
|
|
188
|
+
"--splunk-secret",
|
|
189
|
+
required=True,
|
|
190
|
+
envvar="SPLUNK_SECRET",
|
|
191
|
+
callback=__load_splunk_secret,
|
|
192
|
+
)
|
|
193
|
+
@click.option("--ciphertext", envvar="PASSWORD", prompt=True, callback=__ensure_text)
|
|
194
|
+
def splunk_decrypt(splunk_secret, ciphertext):
|
|
195
|
+
"""Decrypt password using Splunk 7.2 algorithm"""
|
|
196
|
+
click.echo(decrypt(splunk_secret, ciphertext))
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@main.command("splunk-legacy-encrypt")
|
|
200
|
+
@click.option(
|
|
201
|
+
"-S",
|
|
202
|
+
"--splunk-secret",
|
|
203
|
+
required=True,
|
|
204
|
+
envvar="SPLUNK_SECRET",
|
|
205
|
+
callback=__load_splunk_secret,
|
|
206
|
+
)
|
|
207
|
+
@click.option(
|
|
208
|
+
"--password",
|
|
209
|
+
envvar="PASSWORD",
|
|
210
|
+
prompt=True,
|
|
211
|
+
hide_input=True,
|
|
212
|
+
callback=__ensure_text,
|
|
213
|
+
)
|
|
214
|
+
@click.option("--no-salt/--salt", default=False)
|
|
215
|
+
def splunk_legacy_encrypt(splunk_secret, password, no_salt):
|
|
216
|
+
"""Encrypt password using legacy Splunk algorithm (pre-7.2)"""
|
|
217
|
+
click.echo(encrypt(splunk_secret, password, no_salt))
|
|
218
|
+
|
|
219
|
+
|
|
220
|
+
@main.command("splunk-legacy-decrypt")
|
|
221
|
+
@click.option(
|
|
222
|
+
"-S",
|
|
223
|
+
"--splunk-secret",
|
|
224
|
+
required=True,
|
|
225
|
+
envvar="SPLUNK_SECRET",
|
|
226
|
+
callback=__load_splunk_secret,
|
|
227
|
+
)
|
|
228
|
+
@click.option("--ciphertext", envvar="PASSWORD", prompt=True, callback=__ensure_text)
|
|
229
|
+
@click.option("--no-salt/--salt/=", default=False)
|
|
230
|
+
def splunk_legacy_decrypt(splunk_secret, ciphertext, no_salt):
|
|
231
|
+
"""Decrypt password using legacy Splunk algorithm (pre-7.2)"""
|
|
232
|
+
click.echo(decrypt(splunk_secret, ciphertext, no_salt))
|
|
233
|
+
|
|
234
|
+
@main.command("dbconnect-legacy-encrypt")
|
|
235
|
+
@click.option(
|
|
236
|
+
"-S",
|
|
237
|
+
"--secret",
|
|
238
|
+
required=True,
|
|
239
|
+
envvar="DBCONNECT_SECRET",
|
|
240
|
+
callback=__load_splunk_secret,
|
|
241
|
+
)
|
|
242
|
+
@click.option(
|
|
243
|
+
"--password",
|
|
244
|
+
envvar="PASSWORD",
|
|
245
|
+
prompt=True,
|
|
246
|
+
hide_input=True,
|
|
247
|
+
callback=__ensure_text,
|
|
248
|
+
)
|
|
249
|
+
def dbconnect_legacy_encrypt(secret, password):
|
|
250
|
+
"""Encrypt password used for dbconnect identity using legacy algorithm"""
|
|
251
|
+
click.echo(encrypt_dbconnect(secret, password, legacy=True))
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
@main.command("splunk-hash-passwd")
|
|
255
|
+
@click.option(
|
|
256
|
+
"--password",
|
|
257
|
+
envvar="PASSWORD",
|
|
258
|
+
prompt=True,
|
|
259
|
+
hide_input=True,
|
|
260
|
+
callback=__ensure_text,
|
|
261
|
+
)
|
|
262
|
+
def splunk_hash_passwd(password):
|
|
263
|
+
"""Generate password hash for use in $SPLUNK_HOME/etc/passwd"""
|
|
264
|
+
click.echo(pcrypt.crypt(password))
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
from cryptography.hazmat.backends import default_backend
|
|
5
|
+
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
|
|
6
|
+
from cryptography.hazmat.primitives import hashes
|
|
7
|
+
from cryptography.hazmat.primitives.padding import PKCS7
|
|
8
|
+
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
|
|
9
|
+
from cryptography.hazmat.primitives.kdf.scrypt import Scrypt
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def get_key_and_iv(password, salt, algorithm=hashes.MD5):
|
|
13
|
+
"""
|
|
14
|
+
Python implementation of EVP_BytesToKey
|
|
15
|
+
|
|
16
|
+
Based on https://gist.github.com/tly1980/b6c2cc10bb35cb4446fb6ccf5ee5efbc
|
|
17
|
+
"""
|
|
18
|
+
if isinstance(password, str):
|
|
19
|
+
password = password.encode()
|
|
20
|
+
if isinstance(salt, str):
|
|
21
|
+
salt = salt.encode()
|
|
22
|
+
|
|
23
|
+
def mdf(data):
|
|
24
|
+
"""Shortcut for hash using Cryptograph"""
|
|
25
|
+
digest = hashes.Hash(algorithm())
|
|
26
|
+
digest.update(data)
|
|
27
|
+
return digest.finalize()
|
|
28
|
+
|
|
29
|
+
keyiv = mdf(password + salt)
|
|
30
|
+
tmp = [keyiv]
|
|
31
|
+
while len(tmp) < 48:
|
|
32
|
+
tmp.append(mdf(tmp[-1] + password + salt))
|
|
33
|
+
keyiv += tmp[-1]
|
|
34
|
+
return keyiv[:32], keyiv[32:48]
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def get_key(password, salt):
|
|
38
|
+
"""
|
|
39
|
+
Derive a key from a password and salt using Scrypt.
|
|
40
|
+
|
|
41
|
+
Used in the new encryption algorithm.
|
|
42
|
+
"""
|
|
43
|
+
kdf = Scrypt(
|
|
44
|
+
salt=salt,
|
|
45
|
+
length=32,
|
|
46
|
+
n=32, # This is a low number of iterations, but it's what DBX uses...
|
|
47
|
+
r=8,
|
|
48
|
+
p=1,
|
|
49
|
+
backend=default_backend(),
|
|
50
|
+
)
|
|
51
|
+
return kdf.derive(password)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def encrypt_dbconnect(secret_key, plaintext, salt=None, legacy=False):
|
|
55
|
+
"""
|
|
56
|
+
Encrypt a plaintext password using the new or legacy algorithm.
|
|
57
|
+
"""
|
|
58
|
+
if legacy:
|
|
59
|
+
return encrypt_dbconnect_legacy(secret_key, plaintext, salt)
|
|
60
|
+
else:
|
|
61
|
+
return encrypt_dbconnect_new(secret_key, plaintext, salt)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def decrypt_dbconnect(secret_key, ciphertext):
|
|
65
|
+
"""
|
|
66
|
+
Based on whether or not the ciphertext starts with "Salted__",
|
|
67
|
+
run either the legacy or new decryption function.
|
|
68
|
+
"""
|
|
69
|
+
ciphertext = base64.b64decode(ciphertext)
|
|
70
|
+
|
|
71
|
+
if ciphertext.startswith(b"Salted__"):
|
|
72
|
+
return decrypt_dbconnect_legacy(secret_key, ciphertext)
|
|
73
|
+
else:
|
|
74
|
+
return decrypt_dbconnect_new(secret_key, ciphertext)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def decrypt_dbconnect_new(secret_key, ciphertext):
|
|
78
|
+
"""Implement AES GCM decryption as used in newer versions of dbconnect"""
|
|
79
|
+
|
|
80
|
+
# Get salt and actual ciphertext from input
|
|
81
|
+
salt, iv, ciphertext = ciphertext[:16], ciphertext[16:28], ciphertext[28:]
|
|
82
|
+
|
|
83
|
+
# Derive key using Scrypt
|
|
84
|
+
key = get_key(secret_key, salt)
|
|
85
|
+
|
|
86
|
+
aesgcm = AESGCM(key)
|
|
87
|
+
plaintext = aesgcm.decrypt(iv, ciphertext, None)
|
|
88
|
+
return plaintext.decode("utf-8")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def decrypt_dbconnect_legacy(secret_key, ciphertext):
|
|
92
|
+
"""Implement `openssl aes-256-cbc` encryption as used in older versions of dbconnect"""
|
|
93
|
+
|
|
94
|
+
# Get salt and actual ciphertext from input
|
|
95
|
+
salt, ciphertext = ciphertext[8:16], ciphertext[16:]
|
|
96
|
+
|
|
97
|
+
# Use OpenSSL EVP_BytesToKey (with md5) to derive key and iv
|
|
98
|
+
key, iv = get_key_and_iv(secret_key, salt, algorithm=hashes.MD5)
|
|
99
|
+
|
|
100
|
+
# Decrypt
|
|
101
|
+
algorithm = algorithms.AES(key)
|
|
102
|
+
cipher = Cipher(algorithm, mode=modes.CBC(iv), backend=default_backend())
|
|
103
|
+
decryptor = cipher.decryptor()
|
|
104
|
+
plaintext = decryptor.update(ciphertext)
|
|
105
|
+
|
|
106
|
+
# Unpad the plaintext
|
|
107
|
+
unpadder = PKCS7(128).unpadder()
|
|
108
|
+
unpadded_data = unpadder.update(plaintext)
|
|
109
|
+
unpadded_data += unpadder.finalize()
|
|
110
|
+
|
|
111
|
+
# Return string result
|
|
112
|
+
return unpadded_data.decode()
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def encrypt_dbconnect_new(secret_key, plaintext, salt=None):
|
|
116
|
+
"""Implement `openssl aes-256-gcm` encryption as used in newer versions of dbconnect"""
|
|
117
|
+
if salt is None:
|
|
118
|
+
salt = os.urandom(16)
|
|
119
|
+
|
|
120
|
+
# Derive key using Scrypt
|
|
121
|
+
key = get_key(secret_key, salt)
|
|
122
|
+
|
|
123
|
+
# Generate random IV
|
|
124
|
+
iv = os.urandom(12)
|
|
125
|
+
|
|
126
|
+
aesgcm = AESGCM(key)
|
|
127
|
+
ciphertext = aesgcm.encrypt(iv, plaintext.encode("utf-8"), None)
|
|
128
|
+
|
|
129
|
+
result = salt + iv + ciphertext
|
|
130
|
+
return base64.b64encode(result).decode("utf-8")
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def encrypt_dbconnect_legacy(secret_key, plaintext, salt=None):
|
|
134
|
+
"""Implement `openssl aes-256-cbc` decryption as used in older versions of dbconnect"""
|
|
135
|
+
|
|
136
|
+
# Use a random salt unless one is provided
|
|
137
|
+
if salt is None:
|
|
138
|
+
salt = os.urandom(8)
|
|
139
|
+
|
|
140
|
+
# Use OpenSSL EVP_BytesToKey (with md5) to derive key and iv
|
|
141
|
+
key, iv = get_key_and_iv(secret_key, salt, algorithm=hashes.MD5)
|
|
142
|
+
|
|
143
|
+
# Pad the plaintext to 16 bytes
|
|
144
|
+
plaintext = plaintext.encode()
|
|
145
|
+
padder = PKCS7(128).padder()
|
|
146
|
+
padded_data = padder.update(plaintext)
|
|
147
|
+
padded_data += padder.finalize()
|
|
148
|
+
|
|
149
|
+
# Encrypt
|
|
150
|
+
algorithm = algorithms.AES(key)
|
|
151
|
+
cipher = Cipher(algorithm, mode=modes.CBC(iv), backend=default_backend())
|
|
152
|
+
encryptor = cipher.encryptor()
|
|
153
|
+
ciphertext = encryptor.update(padded_data) + encryptor.finalize()
|
|
154
|
+
|
|
155
|
+
# Base64 result
|
|
156
|
+
return base64.b64encode(b"Salted__" + salt + ciphertext).decode()
|
splunksecrets/phantom.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import struct
|
|
3
|
+
|
|
4
|
+
from cryptography.hazmat.backends import default_backend
|
|
5
|
+
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
|
|
6
|
+
from cryptography.hazmat.primitives import hashes, serialization
|
|
7
|
+
from cryptography.hazmat.primitives.padding import PKCS7
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def to_bytes(num, size, byte_order):
|
|
11
|
+
"""Function to convert a number to bytes"""
|
|
12
|
+
fmt = ">" if byte_order.lower() == "big" else "<"
|
|
13
|
+
fmt += "B" * int(size)
|
|
14
|
+
num = bin(num)[2:].zfill(size * 8)
|
|
15
|
+
args = [int(num[i : i + 8], 2) for i in range(0, len(num), 8)]
|
|
16
|
+
|
|
17
|
+
return struct.pack(fmt, *args)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def encrypt_phantom(private_key, secret_key, plaintext, asset_id):
|
|
21
|
+
"""Use AES 256 CBC to encrypt credentials in Phantom"""
|
|
22
|
+
|
|
23
|
+
# Get the public key bytes from the private key
|
|
24
|
+
private_key = serialization.load_pem_private_key(private_key, password=None)
|
|
25
|
+
public_key = private_key.public_key()
|
|
26
|
+
public_key_bytes = to_bytes(
|
|
27
|
+
public_key.public_numbers().n, int(public_key.key_size / 8), byte_order="big"
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# Ensure the secret_key is bytes
|
|
31
|
+
if isinstance(secret_key, str):
|
|
32
|
+
secret_key = secret_key.encode()
|
|
33
|
+
|
|
34
|
+
# Get SHA256(public_key_bytes + secret_key)
|
|
35
|
+
digest = hashes.Hash(hashes.SHA256())
|
|
36
|
+
digest.update(public_key_bytes)
|
|
37
|
+
digest.update(secret_key)
|
|
38
|
+
key = digest.finalize()
|
|
39
|
+
|
|
40
|
+
# Get the iv from asset_id
|
|
41
|
+
digest = hashes.Hash(hashes.SHA1())
|
|
42
|
+
digest.update(str(asset_id).encode())
|
|
43
|
+
iv = digest.finalize()[:16]
|
|
44
|
+
|
|
45
|
+
# Pad the plaintext to 16 bytes
|
|
46
|
+
plaintext = plaintext.encode()
|
|
47
|
+
padder = PKCS7(128).padder()
|
|
48
|
+
padded_data = padder.update(plaintext)
|
|
49
|
+
padded_data += padder.finalize()
|
|
50
|
+
|
|
51
|
+
# Encrypt
|
|
52
|
+
algorithm = algorithms.AES(key)
|
|
53
|
+
cipher = Cipher(algorithm, mode=modes.CBC(iv), backend=default_backend())
|
|
54
|
+
encryptor = cipher.encryptor()
|
|
55
|
+
ciphertext = encryptor.update(padded_data) + encryptor.finalize()
|
|
56
|
+
|
|
57
|
+
# Base64 result
|
|
58
|
+
return base64.b64encode(ciphertext).decode()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def decrypt_phantom(private_key, secret_key, ciphertext, asset_id):
|
|
62
|
+
"""Use AES 256 CBC to decrypt credentials in Phantom"""
|
|
63
|
+
# Get the public key bytes from the private key
|
|
64
|
+
private_key = serialization.load_pem_private_key(private_key, password=None)
|
|
65
|
+
public_key = private_key.public_key()
|
|
66
|
+
public_key_bytes = to_bytes(
|
|
67
|
+
public_key.public_numbers().n, int(public_key.key_size / 8), byte_order="big"
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
# Ensure the secret_key is bytes
|
|
71
|
+
if isinstance(secret_key, str):
|
|
72
|
+
secret_key = secret_key.encode()
|
|
73
|
+
|
|
74
|
+
# Get SHA256(public_key_bytes + secret_key)
|
|
75
|
+
digest = hashes.Hash(hashes.SHA256())
|
|
76
|
+
digest.update(public_key_bytes)
|
|
77
|
+
digest.update(secret_key)
|
|
78
|
+
key = digest.finalize()
|
|
79
|
+
|
|
80
|
+
# Get the iv from asset_id
|
|
81
|
+
digest = hashes.Hash(hashes.SHA1())
|
|
82
|
+
digest.update(str(asset_id).encode())
|
|
83
|
+
iv = digest.finalize()[:16]
|
|
84
|
+
|
|
85
|
+
# Decrypt
|
|
86
|
+
algorithm = algorithms.AES(key)
|
|
87
|
+
cipher = Cipher(algorithm, mode=modes.CBC(iv), backend=default_backend())
|
|
88
|
+
decryptor = cipher.decryptor()
|
|
89
|
+
|
|
90
|
+
# Decode base64
|
|
91
|
+
plaintext = decryptor.update(base64.b64decode(ciphertext))
|
|
92
|
+
|
|
93
|
+
# Unpad the plaintext
|
|
94
|
+
unpadder = PKCS7(128).unpadder()
|
|
95
|
+
unpadded_data = unpadder.update(plaintext)
|
|
96
|
+
unpadded_data += unpadder.finalize()
|
|
97
|
+
|
|
98
|
+
# Return string result
|
|
99
|
+
return unpadded_data.decode()
|
splunksecrets/splunk.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import itertools
|
|
3
|
+
import os
|
|
4
|
+
|
|
5
|
+
from cryptography.hazmat.backends import default_backend
|
|
6
|
+
from cryptography.hazmat.decrepit.ciphers.algorithms import ARC4
|
|
7
|
+
from cryptography.hazmat.primitives.ciphers import algorithms, Cipher, modes
|
|
8
|
+
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
|
9
|
+
from cryptography.hazmat.primitives import hashes
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def b64decode(encoded):
|
|
13
|
+
"""Wrapper around `base64.b64decode` to add padding if necessary"""
|
|
14
|
+
padding_len = 4 - (len(encoded) % 4)
|
|
15
|
+
if padding_len < 4:
|
|
16
|
+
encoded += "=" * padding_len
|
|
17
|
+
return base64.b64decode(encoded)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def decrypt(secret, ciphertext, nosalt=False):
|
|
21
|
+
"""Given the first 16 bytes of splunk.secret, decrypt a Splunk password"""
|
|
22
|
+
plaintext = None
|
|
23
|
+
|
|
24
|
+
if ciphertext.startswith("$1$"):
|
|
25
|
+
ciphertext = b64decode(ciphertext[3:])
|
|
26
|
+
if len(secret) < 16:
|
|
27
|
+
raise ValueError(f"secret too short, need 16 bytes, got {len(secret)}")
|
|
28
|
+
key = secret[:16]
|
|
29
|
+
|
|
30
|
+
algorithm = ARC4(key)
|
|
31
|
+
cipher = Cipher(algorithm, mode=None, backend=default_backend())
|
|
32
|
+
decryptor = cipher.decryptor()
|
|
33
|
+
plaintext = decryptor.update(ciphertext)
|
|
34
|
+
|
|
35
|
+
chars = []
|
|
36
|
+
if nosalt is False:
|
|
37
|
+
for char1, char2 in zip(plaintext[:-1], itertools.cycle("DEFAULTSA")):
|
|
38
|
+
if char1 == ord(char2):
|
|
39
|
+
chars.append(char1)
|
|
40
|
+
else:
|
|
41
|
+
chars.append(char1 ^ ord(char2))
|
|
42
|
+
else:
|
|
43
|
+
chars = plaintext[:-1]
|
|
44
|
+
|
|
45
|
+
plaintext = "".join([chr(c) for c in chars])
|
|
46
|
+
elif ciphertext.startswith("$7$"):
|
|
47
|
+
# pad secret to 254 bytes with nulls
|
|
48
|
+
if isinstance(secret, str):
|
|
49
|
+
secret = secret.encode()
|
|
50
|
+
secret = secret.ljust(254, b"\0")
|
|
51
|
+
|
|
52
|
+
ciphertext = b64decode(ciphertext[3:])
|
|
53
|
+
|
|
54
|
+
kdf = PBKDF2HMAC(
|
|
55
|
+
algorithm=hashes.SHA256(),
|
|
56
|
+
length=32,
|
|
57
|
+
salt=b"disk-encryption",
|
|
58
|
+
iterations=1,
|
|
59
|
+
backend=default_backend(),
|
|
60
|
+
)
|
|
61
|
+
key = kdf.derive(secret[:254])
|
|
62
|
+
|
|
63
|
+
iv = ciphertext[:16]
|
|
64
|
+
tag = ciphertext[-16:]
|
|
65
|
+
ciphertext = ciphertext[16:-16]
|
|
66
|
+
|
|
67
|
+
algorithm = algorithms.AES(key)
|
|
68
|
+
cipher = Cipher(algorithm, mode=modes.GCM(iv, tag), backend=default_backend())
|
|
69
|
+
decryptor = cipher.decryptor()
|
|
70
|
+
plaintext = decryptor.update(ciphertext).decode()
|
|
71
|
+
|
|
72
|
+
return plaintext
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def encrypt(secret, plaintext, nosalt=False):
|
|
76
|
+
"""Given the first 16 bytes of splunk.secret, encrypt a Splunk password"""
|
|
77
|
+
if len(secret) < 16:
|
|
78
|
+
raise ValueError(f"secret too short, need 16 bytes, got {len(secret)}")
|
|
79
|
+
|
|
80
|
+
key = secret[:16]
|
|
81
|
+
|
|
82
|
+
chars = []
|
|
83
|
+
if nosalt is False:
|
|
84
|
+
for char1, char2 in zip(plaintext, itertools.cycle("DEFAULTSA")):
|
|
85
|
+
if ord(char1) == ord(char2):
|
|
86
|
+
chars.append(ord(char1))
|
|
87
|
+
else:
|
|
88
|
+
chars.append(ord(char1) ^ ord(char2))
|
|
89
|
+
else:
|
|
90
|
+
chars = [ord(x) for x in plaintext]
|
|
91
|
+
|
|
92
|
+
chars.append(0)
|
|
93
|
+
|
|
94
|
+
plaintext = b"".join([bytes([c]) for c in chars])
|
|
95
|
+
|
|
96
|
+
algorithm = ARC4(key)
|
|
97
|
+
cipher = Cipher(algorithm, mode=None, backend=default_backend())
|
|
98
|
+
encryptor = cipher.encryptor()
|
|
99
|
+
ciphertext = encryptor.update(plaintext)
|
|
100
|
+
ciphertext = base64.b64encode(ciphertext).decode()
|
|
101
|
+
|
|
102
|
+
return f"$1${ciphertext}"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def encrypt_new(secret, plaintext, iv=None):
|
|
106
|
+
"""Use the new AES 256 GCM encryption in Splunk 7.2"""
|
|
107
|
+
|
|
108
|
+
if isinstance(secret, str):
|
|
109
|
+
# pad secret to 254 bytes with nulls
|
|
110
|
+
secret = secret.encode()
|
|
111
|
+
secret = secret.ljust(254, b"\0")
|
|
112
|
+
|
|
113
|
+
kdf = PBKDF2HMAC(
|
|
114
|
+
algorithm=hashes.SHA256(),
|
|
115
|
+
length=32,
|
|
116
|
+
salt=b"disk-encryption",
|
|
117
|
+
iterations=1,
|
|
118
|
+
backend=default_backend(),
|
|
119
|
+
)
|
|
120
|
+
key = kdf.derive(secret[:254])
|
|
121
|
+
|
|
122
|
+
if iv is None:
|
|
123
|
+
iv = os.urandom(16)
|
|
124
|
+
|
|
125
|
+
algorithm = algorithms.AES(key)
|
|
126
|
+
cipher = Cipher(algorithm, mode=modes.GCM(iv), backend=default_backend())
|
|
127
|
+
encryptor = cipher.encryptor()
|
|
128
|
+
ciphertext = encryptor.update(plaintext.encode()) + encryptor.finalize()
|
|
129
|
+
payload = base64.b64encode(b"%s%s%s" % (iv, ciphertext, encryptor.tag)).decode()
|
|
130
|
+
|
|
131
|
+
return f"$7${payload}"
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: splunksecrets
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Command-line tools for working with secrets for Splunk products
|
|
5
|
+
Author: Steve McMaster, Cameron Schmidt
|
|
6
|
+
Maintainer: Cameron Schmidt
|
|
7
|
+
Requires-Python: >=3.9
|
|
8
|
+
Requires-Dist: click>=8.0.0
|
|
9
|
+
Requires-Dist: cryptography>=43
|
|
10
|
+
Requires-Dist: pcrypt
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
splunksecrets/__init__.py,sha256=yrAt91HH8xPdjnIfmv_KP51MOHy9oWpVeSCWPBoow8Y,490
|
|
2
|
+
splunksecrets/cli.py,sha256=MbxP1sZig4Xwz_qAvu5i6RoRCYyDIbsipvpUFP7j9rM,6953
|
|
3
|
+
splunksecrets/dbconnect.py,sha256=0N6B31P3Va1lSX272MeJln2FbpopGTQ5Xj5cGryiIug,4851
|
|
4
|
+
splunksecrets/phantom.py,sha256=qyqmmMg9Iue-aACSDTNPBWIjbTj_upy7HKWtlEmb1Zg,3298
|
|
5
|
+
splunksecrets/splunk.py,sha256=C_K_zOQ3PGEv5tfmQsnln-AaT0E-47mbOYk0tfxZ30c,4148
|
|
6
|
+
splunksecrets-1.1.0.dist-info/METADATA,sha256=p22fOuwVp-B_VB1vRtRHCCjME3cy1Te24RqRtZEkeyw,303
|
|
7
|
+
splunksecrets-1.1.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
8
|
+
splunksecrets-1.1.0.dist-info/entry_points.txt,sha256=pfNVNvRAm9kTouuDEW6nPdR3I0XYbkXBK24Ml0tnFvI,57
|
|
9
|
+
splunksecrets-1.1.0.dist-info/RECORD,,
|