forked from ronys/pypwsafe
/
PWSafeV3Records.py
1275 lines (1096 loc) · 40 KB
/
PWSafeV3Records.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#===============================================================================
# SYMANTEC: Copyright © 2009-2011 Symantec Corporation. All rights reserved.
#
# This file is part of PyPWSafe.
#
# PyPWSafe is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# PyPWSafe is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PyPWSafe. If not, see http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#===============================================================================
"""Record and record properity objects
"""
from struct import unpack, pack
import calendar, time
import logging, logging.config
from errors import *
import os
from uuid import UUID, uuid4
#logging.config.fileConfig('/etc/mss/psafe_log.conf')
psafe_logger = logging.getLogger("psafe.lib.record")
psafe_logger.debug('initing')
class Record(object):
"""Represents a psafe3 record
Container item: Name of properity
Attrs
records [RecordProp] List of properities
lk {TypeName:RecordProp}
"""
def __init__(self, fetchblock_f=None):
psafe_logger.debug('Creating record object')
self.records = []
self.lk = {}
if fetchblock_f:
psafe_logger.debug("Creating from existing data")
rcd = Create_Prop(fetchblock_f)
psafe_logger.debug("Creating record %s" % repr(rcd))
self.records.append(rcd)
self.lk[rcd.rNAME] = rcd
while type(rcd) != EOERecordProp:
rcd = Create_Prop(fetchblock_f)
psafe_logger.debug("Creating record %s" % repr(rcd))
self.records.append(rcd)
self.lk[rcd.rNAME] = rcd
psafe_logger.debug("Created all records. ")
else:
psafe_logger.debug("Creating blank object")
# Create the UUID object
self._if_noitem(UUIDRecordProp.rNAME)
# Create the EOE object. This must happen last and can't use the if_noitem because it may
# insert somewhere besides the end.
eoe = EOERecordProp()
self.records.append(eoe)
self.lk[eoe.rNAME] = eoe
psafe_logger.debug('Created record object. ')
def __getitem__(self, key):
self._if_noitem(key)
return self.lk[key].get()
def __setitem__(self, key, val):
self._if_noitem(key)
# atm we are just updating our lk record
self.lk[key].set(val)
def _if_noitem(self, item):
"""If an item isn't in our key store, create it. """
if not self.lk.has_key(item):
for i in RecordPropTypes.values():
if i.rNAME == item:
r = i()
self.lk[item] = r
self.records.insert(0, r)
def __iter__(self):
return self.lk.__iter__()
def __repr__(self):
ret = ''
for i in self:
ret += repr(self[i]) + "\n"
return ret[:-1]
def __str__(self):
ret = ''
for i in self.lk.keys():
#print str(self.lk[i])
ret += str(self.lk[i]) + "\n"
return ret[:-1]
def __len__(self):
return len(self.records)
def hmac_data(self):
"""Returns the data required for the "broken" hmac in psafe3. See bug 1812081. """
ret = ''
for i in self.records:
# the data field has the data minus the padding
#if i.serial()!=i.data:
# psafe_logger.warn('Serial != data for class %s. s: %s d: %s'%(repr(i.__class__),repr(i.serial()),repr(i.data)))
ret += i.serial()
return ret
def serialiaze(self):
""" """
ret = ''
for r in self.records:
ret += r.serialiaze()
return ret
# Record Prop
class RecordProp(object):
"""A single properity of a psafe3 record. This represents an unknown type or is overridden by records of a known type.
rTYPE int Properity type. May be null.
rNAME string Code name of properity type.
type int Prop type.
len int Length, in bytes, of data
raw_data string Record data including padding and headers
data string Record data minus headers and padding
"""
rTYPE = None
rNAME = "Unknown"
def __init__(self, ptype, plen, pdata):
self.type = ptype
if self.rTYPE:
assert self.rTYPE == ptype
else:
self.rTYPE = ptype
self.len = plen
self.raw_data = pdata
self.data = pdata[5:(plen + 5)]
psafe_logger.debug('Created psafe record prop with rTYPE of %s. Class %s', repr(self.rTYPE), repr(self.__class__))
self.parse()
if self.rNAME == "Unknown":
psafe_logger.warn('Created psafe record prop of unknown type. rType: %s rLen: %s Data: %s Raw: %s', repr(ptype), repr(plen), repr(self.data), repr(self.raw_data))
else:
psafe_logger.debug('Created psafe record prop of known type. rType: %s rLen: %s Data: %s Raw: %s', repr(ptype), repr(plen), repr(self.data), repr(self.raw_data))
def parse(self):
"""Override me. Called on init to parse received data. """
pass
def __repr__(self):
s = self.serial()
return "RecordProp(%s,%d,%s)" % (hex(self.type), len(s), repr(s))
def __str__(self):
return self.__repr__()
def get(self):
return self.data
def set(self, value):
self.data = value
def _rand_char(self):
"""Returns a random char"""
return os.urandom(1)
def _pad(self, data):
""" Pad out data to 16 bytes """
add_data = 16 - len(data) % 16
if add_data == 16:
add_data = 0
padding = ''
for i in xrange(0, add_data):
padding += self._rand_char()
assert add_data != 16
assert len(padding) == add_data
assert len(data + padding) % 16 == 0
return data + padding
def serial(self):
"""Returns the raw data blocks to generate this object
EXCLUDING TYPE+LEN!"""
#psafe_logger.debug('Serial to %s',repr(self.data))
return self.data
def serialiaze(self):
"""Returns the raw data blocks to generate this object. """
serial = self.serial()
header = pack('=lc', len(serial), chr(self.rTYPE))
padded = self._pad(header + serial)
psafe_logger.debug("Padded output %s" % repr(padded))
return padded
class UUIDRecordProp(RecordProp):
"""Record's unique id
uuid uuid.UUID
>>> x=UUIDRecordProp(0x1,16,'\x10\x00\x00\x00\x01\xfa@\xbd\x1f \xf5B\xf5\x88v#\xe4\x08\xae\x8a\xa1@\x16[\xfb\x8c\x87mq\xf70[')
>>> repr(x)
"UUIDRecordProp(0x1,16,'\\x10\\x00\\x00\\x00\\x01\\xfa@\\xbd\\x1f \\xf5B\\xf5\\x88v#\\xe4\\x08\\xae\\x8a\\xa1@\\x16[\\xfb\\x8c\\x87mq\\xf70[')"
>>> str(x)
'UUID=fa40bd1f-20f5-42f5-8876-23e408ae8aa1'
>>> x.serial()
'\xfa@\xbd\x1f \xf5B\xf5\x88v#\xe4\x08\xae\x8a\xa1'
"""
rTYPE = 0x01
rNAME = 'UUID'
def __init__(self, ptype=None, plen=16, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.uuid = uuid4()
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.uuid = UUID(bytes=unpack('=16s', self.data)[0])
def __repr__(self):
return "UUID" + RecordProp.__repr__(self)
def __str__(self):
return "UUID=%s" % str(self.uuid)
def get(self):
return self.uuid
def set(self, value):
"""Accepts a UUID object or a hex string
"""
if type(value) == UUID:
self.uuid = value
else:
self.uuid = UUID(fields=value)
def serial(self):
#psafe_logger.debug("Serial to %s",repr(pack('=16s',str(self.uuid.bytes))))
return pack('=16s', str(self.uuid.bytes))
class GroupRecordProp(RecordProp):
"""Record's Group
group_str string Raw group string
group [string] List of the groups. First entry is the top level group.
>>> x=GroupRecordProp(0x2,7,'\x07\x00\x00\x00\x02Group 0\x11"\xf1\x84')
>>> str(x)
"Group: 'Group 0'"
>>> repr(x)
'GroupRecordProp(0x2,7,\'\\x07\\x00\\x00\\x00\\x02Group 0\\x11"\\xf1\\x84\')'
>>> x.get()
['Group 0']
>>> x.serial()
'\x07\x00\x00\x00\x02Group 0'
"""
rTYPE = 0x02
rNAME = 'Group'
def __init__(self, ptype=None, plen=0, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.group = []
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.group_str = self.data
self.group = self.group_str.split('.')
def __repr__(self):
return "Group" + RecordProp.__repr__(self)
def __str__(self):
return "Group: %s" % repr(self.group_str)
def get(self):
return self.group
def set(self, value):
self.group = value
def serial(self):
self.group_str = '.'.join(self.group)
#psafe_logger.debug("Serial to %s Data %s"%(repr(self.group_str),repr(self.data)))
return self.group_str
class TitleRecordProp(RecordProp):
"""Record's title
title string Title
>>> x=TitleRecordProp(0x3,7,'\x07\x00\x00\x00\x03Title 0\xd5\xed\xf5l')
>>> str(x)
"Title='Title 0'"
>>> repr(x)
"TitleRecordProp(0x3,7,'\\x07\\x00\\x00\\x00\\x03Title 0\\xd5\\xed\\xf5l')"
>>> x.serial()
'Title 0'
>>> x.get()
'Title 0'
"""
rTYPE = 0x03
rNAME = 'Title'
def __init__(self, ptype=None, plen=0, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.title = ''
pdata = ''
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.title = self.data[:self.len]
def __repr__(self):
return "Title" + RecordProp.__repr__(self)
def __str__(self):
return "Title=" + repr(self.title)
def get(self):
return self.title
def set(self, value):
self.title = str(value)
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(self.title),repr(self.data)))
return self.title
class UsernameRecordProp(RecordProp):
"""Record's username
username string ...
>>> x=UsernameRecordProp(0x4,9,'\t\x00\x00\x00\x04username0\x06\xfb')
>>> str(x)
"Username='username0'"
>>> repr(x)
"UsernameRecordProp(0x4,9,'\\t\\x00\\x00\\x00\\x04username0\\x06\\xfb')"
>>> x.get()
'username0'
>>> x.serial()
'username0'
"""
rTYPE = 0x04
rNAME = 'Username'
def __init__(self, ptype=None, plen=0, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
pdata = ''
self.username = ''
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.username = self.data[:self.len]
def __repr__(self):
return "Username" + RecordProp.__repr__(self)
def __str__(self):
return "Username=" + repr(self.username)
def get(self):
return self.username
def set(self, value):
self.username = value
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(self.username),repr(self.data)))
return self.username
class NotesRecordProp(RecordProp):
"""Record notes
notes string ...
>>> x=NotesRecordProp(0x5,10,'\n\x00\x00\x00\x05more notes\x8c')
>>> str(x)
"Notes='more notes'"
>>> repr(x)
"NotesRecordProp(0x5,10,'\\n\\x00\\x00\\x00\\x05more notes\\x8c')"
>>> x.get()
'more notes'
>>> x.serial()
'more notes'
"""
rTYPE = 0x05
rNAME = 'Notes'
def __init__(self, ptype=None, plen=0, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.notes = ''
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.notes = self.data[:self.len]
def __repr__(self):
return "Notes" + RecordProp.__repr__(self)
def __str__(self):
return "Notes=" + repr(self.notes)
def get(self):
return self.notes
def set(self, value):
self.notes = str(value)
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(self.notes),repr(self.data)))
return self.notes
class PasswordRecordProp(RecordProp):
"""Record's password
password string ...
>>> x=PasswordRecordProp(0x6,9,'\t\x00\x00\x00\x06password0d\xe7')
>>> str(x)
"Password='password0'"
>>> repr(x)
"PasswordRecordProp(0x6,9,'\\t\\x00\\x00\\x00\\x06password0d\\xe7')"
>>> x.get()
'password0'
>>> x.serial()
'password0'
"""
rTYPE = 0x06
rNAME = 'Password'
def __init__(self, ptype=None, plen=0, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.password = ''
else:
RecordProp.__init__(self, ptype, plen, pdata)
# TODO: Add handling for links. See formatv3 3.3[3]
def parse(self):
self.password = self.data[:self.len]
def __repr__(self):
return "Password" + RecordProp.__repr__(self)
def __str__(self):
return "Password=" + repr(self.password)
def get(self):
return self.password
def set(self, value):
self.password = str(value)
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(self.password),repr(self.data)))
return self.password
class CreationTimeRecordProp(RecordProp):
"""Record's ctime
password string ...
>>> x=CreationTimeRecordProp(0x7,4,'\x04\x00\x00\x00\x07\xda\x17;G\x86\xd5\x7f\xd2\x1a\xeb\xc5')
>>> x.serial()
'\xda\x17;G'
>>> x.get()
(2007, 11, 14, 15, 44, 26, 2, 318, 0)
>>> str(x)
'CTime=Wed, 14 Nov 2007 15:44:26 +0000'
>>> repr(x)
"CreationTimeRecordProp(0x7,4,'\\x04\\x00\\x00\\x00\\x07\\xda\\x17;G\\x86\\xd5\\x7f\\xd2\\x1a\\xeb\\xc5')"
"""
rTYPE = 0x07
rNAME = 'ctime'
def __init__(self, ptype=None, plen=4, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.dt = time.gmtime()
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.dt = parsedatetime(self.data[:self.len])
def __repr__(self):
return "CreationTime" + RecordProp.__repr__(self)
def __str__(self):
return "CTime=" + time.strftime("%a, %d %b %Y %H:%M:%S +0000", self.dt)
def get(self):
return self.dt
def set(self, value):
self.dt = value
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(makedatetime(self.dt)),repr(self.data)))
return makedatetime(self.dt)
class ModTimeRecordProp(RecordProp):
"""Record's mtime
password string ...
>>> x=ModTimeRecordProp(0x8,4,'\x04\x00\x00\x00\x08\xd6\x8apI\xd2\xec\xc0_\xfb\x994')
>>> x.serial()
'\xd6\x8apI'
>>> x.get()
(2009, 1, 16, 13, 25, 42, 4, 16, 0)
>>> str(x)
'MTime=Fri, 16 Jan 2009 13:25:42 +0000'
>>> repr(x)
"ModTimeRecordProp(0x8,4,'\\x04\\x00\\x00\\x00\\x08\\xd6\\x8apI\\xd2\\xec\\xc0_\\xfb\\x994')"
"""
rTYPE = 0x08
rNAME = 'mtime'
def __init__(self, ptype=None, plen=4, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.dt = time.gmtime()
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.dt = parsedatetime(self.data[:self.len])
def __repr__(self):
return 'ModTime' + RecordProp.__repr__(self)
def __str__(self):
return "MTime=" + time.strftime("%a, %d %b %Y %H:%M:%S +0000", self.dt)
def get(self):
return self.dt
def set(self, value):
self.dt = value
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(makedatetime(self.dt)),repr(self.data)))
return makedatetime(self.dt)
class LastAccessTimeRecordProp(RecordProp):
"""Record's ctime
password string ...
>>> x=LastAccessTimeRecordProp(0x9,4,'\x04\x00\x00\x00\t\xe4\x9fpI\xb2H\x860\x7f|\xf8')
>>> str(x)
'LastAccess=Fri, 16 Jan 2009 14:55:32 +0000'
>>> repr(x)
"LastAccessTimeRecordProp(0x9,4,'\\x04\\x00\\x00\\x00\\t\\xe4\\x9fpI\\xb2H\\x860\\x7f|\\xf8')"
>>> x.get()
(2009, 1, 16, 14, 55, 32, 4, 16, 0)
>>> x.serial()
'\xe4\x9fpI'
"""
rTYPE = 0x09
rNAME = 'LastAccess'
def __init__(self, ptype=None, plen=4, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.dt = time.gmtime()
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.dt = parsedatetime(self.data[:self.len])
def __repr__(self):
return 'LastAccessTime' + RecordProp.__repr__(self)
def __str__(self):
return self.rNAME + "=" + time.strftime("%a, %d %b %Y %H:%M:%S +0000", self.dt)
def get(self):
return self.dt
def set(self, value):
self.dt = value
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(makedatetime(self.dt)),repr(self.data)))
return makedatetime(self.dt)
class PasswordExpiryTimeRecordProp(RecordProp):
"""Record's experation time
password string ...
>>> x=PasswordExpiryTimeRecordProp(0xa,4,'\x04\x00\x00\x00\n""wIy\xb8La\x8fOu')
>>> str(x)
'PasswordExpiry=Wed, 21 Jan 2009 13:24:50 +0000'
>>> repr(x)
'PasswordExpiryTimeRecordProp(0xa,4,\'\\x04\\x00\\x00\\x00\\n""wIy\\xb8La\\x8fOu\')'
>>> x.get()
(2009, 1, 21, 13, 24, 50, 2, 21, 0)
>>> x.serial()
'""wI'
"""
rTYPE = 0x0a
rNAME = 'PasswordExpiry'
def __init__(self, ptype=None, plen=4, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.dt = time.gmtime(0)
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.dt = parsedatetime(self.data[:self.len])
def __repr__(self):
return 'PasswordExpiryTime' + RecordProp.__repr__(self)
def __str__(self):
return self.rNAME + "=" + time.strftime("%a, %d %b %Y %H:%M:%S +0000", self.dt)
def get(self):
return self.dt
def set(self, value):
self.dt = value
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(makedatetime(self.dt)),repr(self.data)))
return makedatetime(self.dt)
class LastModificationTimeRecordProp(RecordProp):
"""Record's experation time
password string ...
>>> x=LastModificationTimeRecordProp(0xc,4,'\x04\x00\x00\x00\x0c\xd6\x8apI\x14\xff\xb1?\x80q\xeb')
>>> str(x)
'LastModification=Fri, 16 Jan 2009 13:25:42 +0000'
>>> repr(x)
"LastModificationTimeRecordProp(0xc,4,'\\x04\\x00\\x00\\x00\\x0c\\xd6\\x8apI\\x14\\xff\\xb1?\\x80q\\xeb')"
>>> x.get()
(2009, 1, 16, 13, 25, 42, 4, 16, 0)
>>> x.serial()
'\xd6\x8apI'
"""
rTYPE = 0x0c
rNAME = 'LastModification'
def __init__(self, ptype=None, plen=4, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.dt = time.gmtime()
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.dt = parsedatetime(self.data[:self.len])
def __repr__(self):
return 'LastModificationTime' + RecordProp.__repr__(self)
def __str__(self):
return self.rNAME + "=" + time.strftime("%a, %d %b %Y %H:%M:%S +0000", self.dt)
def get(self):
return self.dt
def set(self, value):
self.dt = value
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(makedatetime(self.dt)),repr(self.data)))
return makedatetime(self.dt)
class URLRecordProp(RecordProp):
"""Record's URL
title string Title
>>> x=URLRecordProp(0xd,5,'\x05\x00\x00\x00\ra url\xaczf\xca:2')
>>> repr(x)
"URLRecordProp(0xd,5,'\\x05\\x00\\x00\\x00\\ra url\\xaczf\\xca:2')"
>>> str(x)
"URL='a url'"
>>> x.get()
'a url'
>>> x.serial()
'a url'
"""
rTYPE = 0x0d
rNAME = 'URL'
def __init__(self, ptype=None, plen=0, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.url = ''
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.url = self.data[:self.len]
def __repr__(self):
return 'URL' + RecordProp.__repr__(self)
def __str__(self):
return self.rNAME + "=" + repr(self.url)
def get(self):
return self.url
def set(self, value):
self.url = str(value)
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(self.url),repr(self.data)))
return self.url
class AutotypeRecordProp(RecordProp):
"""Record's title
title string Title
>>> x=AutotypeRecordProp(0xe,12,'\x0c\x00\x00\x00\x0emeh autotype-Ch\xcd\x99$i\xc0\xb7\x87\x0f\x0bN,\x05')
>>> repr(x)
"AutotypeRecordProp(0xe,12,'\\x0c\\x00\\x00\\x00\\x0emeh autotype-Ch\\xcd\\x99$i\\xc0\\xb7\\x87\\x0f\\x0bN,\\x05')"
>>> str(x)
"AutoType='meh autotype'"
>>> x.get()
'meh autotype'
>>> x.serial()
'meh autotype'
"""
rTYPE = 0x0e
rNAME = 'Autotype'
def __init__(self, ptype=None, plen=0, pdata=None):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.autotype = ''
else:
RecordProp.__init__(self, ptype, plen, pdata)
def parse(self):
self.autotype = self.data[:self.len]
def __repr__(self):
return "Autotype" + RecordProp.__repr__(self)
def __str__(self):
return "AutoType=" + repr(self.autotype)
def get(self):
return self.autotype
def set(self, value):
self.autotype = str(value)
def serial(self):
#psafe_logger.debug("Serial to %s data %s"%(repr(self.autotype),repr(self.data)))
return self.autotype
class PasswordHistoryRecordProp(RecordProp):
"""Record's old passwords
Password History is an optional record. If it exists, it stores the
creation times and values of the last few passwords used in the current
entry, in the following format:
"fmmnnTLPTLP...TLP"
where:
f = {0,1} if password history is on/off
mm = 2 hexadecimal digits max size of history list (i.e. max = 255)
nn = 2 hexadecimal digits current size of history list
T = Time password was set (time_t written out in %08x)
L = 4 hexadecimal digit password length (in TCHAR)
P = Password
raw_data string Just my data
enabled bool True if we are keeping password history
history [time,pass] Password history
maxsize int Max size of history
zerohack bool True if there is a trailing 00. Used
to make sure the hmac calcs right on
certian records with no history.
ie does 0ff0000 instead of 0ff00.
Update: See bug 2529736.
>>> x=PasswordHistoryRecordProp(0xf,72,'H\x00\x00\x00\x0f1ff03473b17da000blaskdjflkxn48d98049000claskdjflkxnd49708a980008N0Y2Dkir\x9b\xa4\xb5')
>>> x.serial()
'1ff03473b17da000blaskdjflkxn48d98049000claskdjflkxnd49708a980008N0Y2Dkir'
>>> repr(x)
"PasswordHistoryRecordProp(0xf,72,'H\\x00\\x00\\x00\\x0f1ff03473b17da000blaskdjflkxn48d98049000claskdjflkxnd49708a980008N0Y2Dkir\\x9b\\xa4\\xb5')"
>>> str(x)
'PasswordHistory(Enabled: True\nMax size: 255\nCur Size: 3\nCreated: Wed, 14 Nov 2007 15:44:26 +0000\nPassword: laskdjflkxn\nCreated: Tue, 23 Sep 2008 23:48:25 +0000\nPassword: laskdjflkxnd\nCreated: Fri, 16 Jan 2009 13:24:40 +0000\nPassword: N0Y2Dkir\n)'
>>> x.get()
{'maxsize': 255, 'enable': True, 'history': {'Tue, 23 Sep 2008 23:48:25 +0000': 'laskdjflkxnd', 'Fri, 16 Jan 2009 13:24:40 +0000': 'N0Y2Dkir', 'Wed, 14 Nov 2007 15:44:26 +0000': 'laskdjflkxn'}, 'currentsize': 3}
"""
rTYPE = 0x0f
rNAME = 'PasswordHistory'
def __init__(self, ptype=None, plen=0, pdata=None, enabled=0, maxsize=255):
if not ptype:
ptype = self.rTYPE
assert ptype == self.rTYPE
if not pdata:
self.enabled = enabled
self.maxsize = maxsize
self.history = []
else:
RecordProp.__init__(self, ptype, plen, pdata)
def serial(self):
ret = ''
if self.enabled:
ret += "1"
else:
ret += "0"
ret += "%02x" % self.maxsize
ret += "%02x" % len(self.history)
psafe_logger.debug("Pre-passwords %s" % repr(ret))
for (tm, passwd) in self.history:
ret += "%08x" % calendar.timegm(tm)
ret += "%04x" % len(passwd)
ret += passwd
psafe_logger.debug("Post-add password %s" % repr(ret))
if len(self.history) == 0 and self.zerohack:
ret += "00"
#psafe_logger.debug("Serial to %s data %s"%(repr(ret),repr(self.data)))
return ret
def parse(self):
if len(self.data) == 7:
self.zerohack = True
else:
self.zerohack = False
if len(self.data) > 0:
# Enabled/disabled
if self.data[0] == "0":
self.enabled = False
elif self.data[0] == "1":
self.enabled = True
else:
raise PropParsingError, "Invalid enabled/disabled flag %s" % repr(self.data[0])
psafe_logger.debug("Set password history to %s", repr(self.enabled))
# Max size of hist list
try:
self.maxsize = int(self.data[1:3], 16)
except ValueError:
raise PropParsingError, "Invalid maxsize type %s" % repr(self.data[1:3])
if self.maxsize < 0 or self.maxsize > 255:
raise PropParsingError, "Invalid maxsize value %s" % repr(self.data[1:3])
psafe_logger.debug("Set password history max size to %d", self.maxsize)
# Current size of hist list
try:
self._cursize = int(self.data[3:5], 16)
except ValueError:
raise PropParsingError, "Invalid cursize type %s" % repr(self.data[3:5])
if self._cursize < 0 or self._cursize > 255:
raise PropParsingError, "Invalid cursize value %s" % repr(self.data[3:5])
psafe_logger.debug("Set current size of password history to %d", self._cursize)
# FIXME: Should this end at self.len?
try:
data = self.data[5:]
self.history = []
i = self._cursize
while len(data) > 12 and i > 0:
i -= 1
# Split out the data
tm_raw = data[:8]
len_raw = data[8:12]
data = data[12:]
# Parse known data
# TODO: Check for errors
tm = time.gmtime(int(tm_raw, 16))
len_real = int(len_raw, 16)
password = data[:len_real]
data = data[len_real:]
self.history.append((tm, password))
except ValueError:
raise PropParsingError, "Error parsing password history"
assert self._cursize == len(self.history)
def __repr__(self):
return self.rNAME + RecordProp.__repr__(self)
def __str__(self):
ret = self.rNAME + "("
ret += "Enabled: %s\n" % repr(self.enabled)
ret += "Max size: %s\n" % repr(self.maxsize)
ret += "Cur Size: %s\n" % repr(len(self.history))
for (createddt, passwd) in self.history:
ret += "Created: %s\n" % time.strftime("%a, %d %b %Y %H:%M:%S +0000", createddt)
ret += "Password: %s\n" % passwd
ret += ")"
return ret
def get(self):
hist = {}
for (createddt, passwd) in self.history:
#used to return time.strftime("%a, %d %b %Y %H:%M:%S +0000",createddt)
hist[createddt] = passwd
return dict(
enable=self.enabled
, maxsize=self.maxsize
, currentsize=len(self.history)
, history=hist
)
def set(self, value):
self.enabled = value['enabled']
self.maxsize = value['maxsize']
self.history = []
for (dt, passwd) in value['history']:
self.history.append((dt, passwd))
class PasswordPolicyRecordProp(RecordProp):
"""Record's title
This field allows a specific Password Policy per entry. The format is:
ffffnnnllluuudddsss"
where:
ffff = 4 hexadecimal digits representing the following flags
UseLowercase = 0x8000 - can have a minimum length
UseUppercase = 0x4000 - can have a minimum length
UseDigits = 0x2000 - can have a minimum length
UseSymbols = 0x1000 - can have a minimum length
UseHexDigits = 0x0800 (if set, then no other flags can be set)
UseEasyVision = 0x0400
MakePronounceable = 0x0200
Unused 0x01ff
nnn = 3 hexadecimal digits password total length
lll = 3 hexadecimal digits password minimum number of lowercase characters
uuu = 3 hexadecimal digits password minimum number of uppercase characters
ddd = 3 hexadecimal digits password minimum number of digit characters
sss = 3 hexadecimal digits password minimum number of symbol characters
>>> x=PasswordPolicyRecordProp(0x10,19,'\x13\x00\x00\x00\x10f000010004001005002n_S-\x84r\xeb\xe4')
>>> str(x)
'PasswordPolicy=\nUse Lowercase: True\nUse Uppercase: True\nUse Digits: True\nUse Symbols: True\nUse Hex: False\nUse Easy Version: False\nMake Pronounceable: False\nTotal Length: 16\nMin Lowercase: 4\nMin Uppercase: 1\nMin Digits: 5\nMin Symbols: 2'
>>> repr(x)
"PasswordPolicyRecordProp(0x10,19,'\\x13\\x00\\x00\\x00\\x10f000010004001005002n_S-\\x84r\\xeb\\xe4')"
>>> x.get()
{'UseEasy': False, 'MinSymbols': 2, 'MinDigits': 5, 'MakePronounceable': False, 'MinUpper': 1, 'UseHex': False, 'TotalLen': 16, 'UseDigits': True, 'UseLower': True, 'UseSymbols': True, 'MinLower': 4, 'UseUpper': True}
>>> x.serial()
'b000010004001005002'
"""
rTYPE = 0x10
rNAME = 'PasswordPolicy'
# A few constants
USELOWERCASE = 0x8000
USEUPPERCASE = 0x4000
USEDIGITS = 0x2000
USESYMBOLS = 0x1000
USEHEXDIGITS = 0x0800
USEEASYVERSION = 0x0400
MAKEPRONOUNCEABLE = 0x0200
UNUSED = 0x01ff
def __init__(self
, ptype=None
, plen=0
, pdata=None
, ttllen=14
, minlow=2
, minup=2
, mindig=2
, minsym=2
, uselowercase=True
, useuppercase=True
, usedigits=True
, usesymbols=True
, usehex=False
, useeasy=False
, makepron=False