1 # GPO Parser for audit extensions
3 # Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
4 # Written by Garming Sam <garming@catalyst.net.nz>
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
24 from io
import BytesIO
25 from xml
.etree
.ElementTree
import Element
, SubElement
26 from samba
.gp_parse
import GPParser
27 # [MS-GPAC] Group Policy Audit Configuration
28 class GPAuditCsvParser(GPParser
):
33 def parse(self
, contents
):
35 reader
= csv
.reader(codecs
.getreader(self
.encoding
)(BytesIO(contents
)))
37 self
.header
= next(reader
)
40 for i
, x
in enumerate(row
):
41 line
[self
.header
[i
]] = x
43 self
.lines
.append(line
)
46 def write_xml(self
, filename
):
47 with
open(filename
, 'wb') as f
:
48 root
= Element('CsvFile')
49 child
= SubElement(root
, 'Row')
51 value
= SubElement(child
, 'Value')
54 for line
in self
.lines
:
55 child
= SubElement(root
, 'Row')
56 for e
, title
in [(line
[x
], x
) for x
in self
.header
]:
57 value
= SubElement(child
, 'Value')
60 # Metadata for generalization
61 if title
== 'Policy Target' and e
!= '':
62 value
.attrib
['user_id'] = 'TRUE'
63 if (title
== 'Setting Value' and e
!= '' and
64 (line
['Subcategory'] == 'RegistryGlobalSacl' or
65 line
['Subcategory'] == 'FileGlobalSacl')):
66 value
.attrib
['acl'] = 'TRUE'
68 self
.write_pretty_xml(root
, f
)
71 # contents = codecs.open(filename, encoding='utf-8').read()
72 # self.load_xml(fromstring(contents))
74 def load_xml(self
, root
):
78 for r
in root
.findall('Row'):
82 for v
in r
.findall('Value'):
83 if not isinstance(v
.text
, str):
84 v
.text
= v
.text
.decode(self
.output_encoding
)
85 self
.header
.append(v
.text
)
88 for i
, v
in enumerate(r
.findall('Value')):
89 line
[self
.header
[i
]] = v
.text
if v
.text
is not None else ''
90 if not isinstance(self
.header
[i
], str):
91 line
[self
.header
[i
]] = line
[self
.header
[i
]].decode(self
.output_encoding
)
93 self
.lines
.append(line
)
95 def write_binary(self
, filename
):
97 with
open(filename
, 'w', encoding
=self
.encoding
) as f
:
98 # In this case "binary" means "utf-8", so we let Python do that.
99 writer
= csv
.writer(f
, quoting
=csv
.QUOTE_MINIMAL
)
100 writer
.writerow(self
.header
)
101 for line
in self
.lines
:
102 writer
.writerow([line
[x
] for x
in self
.header
])