-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.py
183 lines (149 loc) · 5.4 KB
/
convert.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
import os
import datetime
import shutil
import json
from pykeepass import PyKeePass
from urlparse import urlparse
shutil.copyfile("in.kdbx", "out.kdbx")
kp = PyKeePass("out.kdbx", password="test")
groupLabels = {
"passwords.Password": "Passwords",
"webforms.WebForm": "Logins",
"wallet.membership.Membership": "Memberships",
"securenotes.SecureNote": "Notes",
"wallet.government.Passport": "Passports",
"wallet.computer.UnixServer": "Servers",
"wallet.computer.Router": "Routers",
"wallet.financial.BankAccountUS": "Bank Accounts",
"wallet.financial.CreditCard": "Credit Cards",
"wallet.computer.License": "Licenses",
}
groups = {}
def getGroup(item):
group = groups.get(item["typeName"])
if group:
return group
label = groupLabels.get(item["typeName"])
if not label:
raise Exception("Unknown type name {}".format(item["typeName"]))
group = kp.add_group(kp.root_group, label)
groups[item["typeName"]] = group
return group
def getField(item, designation):
secure = item["secureContents"]
if "fields" in secure:
for field in secure["fields"]:
d = field.get("designation")
if d == designation:
return field["value"]
return None
with open("in.1pif/data.1pif", "r") as fp:
data = fp.read().strip().split("***5642bee8-a5ff-11dc-8314-0800200c9a66***")
for line in data:
if line.strip() == "":
continue
item = json.loads(line.strip())
if item.get("trashed"):
continue
group = getGroup(item)
entry = kp.add_entry(group, item["title"], "", "")
secure = item["secureContents"]
# Username
if "username" in secure:
entry.username = secure["username"]
else:
entry.username = getField(item, "username")
# Password
if "password" in secure:
entry.password = secure["password"]
else:
entry.password = getField(item, "password")
# Other web fields
if "fields" in secure:
for field in secure["fields"]:
d = field.get("designation")
if d != "username" and d != "password":
entry.set_custom_property("Web field: {}".format(field["name"]), field["value"])
# Password history
if "passwordHistory" in secure:
for p in secure["passwordHistory"]:
d = datetime.datetime.fromtimestamp(p["time"])
entry.set_custom_property("Password history ({})".format(d), p["value"])
# Find URL in fields
if not entry.url:
if "htmlAction" in secure:
entry.url = secure["htmlAction"]
# Membership fields
if "membership_no" in secure and not entry.username:
entry.username = secure["membership_no"]
# Passport fields
if "number" in secure and not entry.username:
entry.username = secure["number"]
# Router fields
if "network_name" in secure and not entry.username:
entry.username = secure["network_name"]
if "wireless_password" in secure and not entry.password:
entry.password = secure["wireless_password"]
# Bank account
if "iban" in secure and not entry.username:
entry.username = secure["iban"]
if "swift" in secure and not entry.username:
entry.username = secure["swift"]
if "routingNo" in secure and not entry.username:
entry.username = secure["routingNo"]
if "accountNo" in secure and not entry.username:
entry.username = secure["accountNo"]
if "telephonePin" in secure and not entry.password:
entry.password = secure["telephonePin"]
# Credit card
if "ccnum" in secure and not entry.username:
entry.username = secure["ccnum"]
if "pin" in secure and not entry.password:
entry.password = secure["pin"]
# Sections
if "sections" in secure:
for s in secure["sections"]:
t = s["title"]
if "fields" in s:
for f in s["fields"]:
v = f.get("v")
if not v:
continue
k = f["k"]
ft = "{} - {}".format(t, f["t"])
if t == "":
ft = f["t"]
if k == "string" or k == "concealed" or k == "menu" or k == "cctype" or k == "monthYear":
entry.set_custom_property(ft, str(v))
elif k == "date":
d = datetime.datetime.fromtimestamp(v)
entry.set_custom_property(ft, str(d))
else:
raise Exception("Unknown k: {}".format(k))
# Notes
if "notesPlain" in secure:
entry.notes = secure["notesPlain"]
# URLs
settings = {
"Allow": [],
"Deny": [],
"Realm": "",
}
applySettings = False
if "location" in item:
entry.url = item["location"]
if "URLs" in secure:
for u in secure["URLs"]:
if not entry.url:
entry.url = u["url"]
url = urlparse(u["url"])
settings["Allow"].append(url.hostname)
applySettings = True
if applySettings:
settings["Allow"] = list(set(settings["Allow"]))
entry.set_custom_property("KeePassHttp Settings", json.dumps(settings))
# Dates
entry.ctime = datetime.datetime.fromtimestamp(item["createdAt"])
entry.mtime = datetime.datetime.fromtimestamp(item["updatedAt"])
kp.save()