2018-05-10 08:15:23 +03:00
# GPO Parser for audit extensions
#
# Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
# Written by Garming Sam <garming@catalyst.net.nz>
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
#
import codecs
import csv
import io
from io import BytesIO
from xml . etree . ElementTree import Element , SubElement
from samba . gp_parse import GPParser
# [MS-GPAC] Group Policy Audit Configuration
class GPAuditCsvParser ( GPParser ) :
encoding = ' utf-8 '
header = None
lines = [ ]
def parse ( self , contents ) :
self . lines = [ ]
2018-09-05 16:18:16 +03:00
reader = csv . reader ( codecs . getreader ( self . encoding ) ( BytesIO ( contents ) ) )
2018-05-10 08:15:23 +03:00
2018-09-05 16:18:16 +03:00
self . header = next ( reader )
2018-05-10 08:15:23 +03:00
for row in reader :
line = { }
for i , x in enumerate ( row ) :
line [ self . header [ i ] ] = x
self . lines . append ( line )
# print line
def write_xml ( self , filename ) :
2018-09-05 14:46:44 +03:00
with open ( filename , ' wb ' ) as f :
2018-05-10 08:15:23 +03:00
root = Element ( ' CsvFile ' )
child = SubElement ( root , ' Row ' )
for e in self . header :
value = SubElement ( child , ' Value ' )
value . text = e
for line in self . lines :
child = SubElement ( root , ' Row ' )
2018-05-30 00:43:53 +03:00
for e , title in [ ( line [ x ] , x ) for x in self . header ] :
2018-05-10 08:15:23 +03:00
value = SubElement ( child , ' Value ' )
value . text = e
2018-05-30 00:43:53 +03:00
# Metadata for generalization
if title == ' Policy Target ' and e != ' ' :
value . attrib [ ' user_id ' ] = ' TRUE '
if ( title == ' Setting Value ' and e != ' ' and
( line [ ' Subcategory ' ] == ' RegistryGlobalSacl ' or
line [ ' Subcategory ' ] == ' FileGlobalSacl ' ) ) :
value . attrib [ ' acl ' ] = ' TRUE '
2018-05-10 08:15:23 +03:00
self . write_pretty_xml ( root , f )
# contents = codecs.open(filename, encoding='utf-8').read()
# self.load_xml(fromstring(contents))
def load_xml ( self , root ) :
header = True
self . lines = [ ]
for r in root . findall ( ' Row ' ) :
if header :
header = False
self . header = [ ]
for v in r . findall ( ' Value ' ) :
2020-07-04 05:27:06 +03:00
if not isinstance ( v . text , str ) :
2018-09-05 19:01:17 +03:00
v . text = v . text . decode ( self . output_encoding )
self . header . append ( v . text )
2018-05-10 08:15:23 +03:00
else :
line = { }
for i , v in enumerate ( r . findall ( ' Value ' ) ) :
line [ self . header [ i ] ] = v . text if v . text is not None else ' '
2020-07-04 05:27:06 +03:00
if not isinstance ( self . header [ i ] , str ) :
2018-09-05 19:01:17 +03:00
line [ self . header [ i ] ] = line [ self . header [ i ] ] . decode ( self . output_encoding )
2018-05-10 08:15:23 +03:00
self . lines . append ( line )
def write_binary ( self , filename ) :
2018-09-05 14:52:30 +03:00
from io import open
2019-02-21 05:11:39 +03:00
with open ( filename , ' w ' , encoding = self . encoding ) as f :
2018-09-05 14:52:30 +03:00
# In this case "binary" means "utf-8", so we let Python do that.
2018-05-10 08:15:23 +03:00
writer = csv . writer ( f , quoting = csv . QUOTE_MINIMAL )
writer . writerow ( self . header )
for line in self . lines :
writer . writerow ( [ line [ x ] for x in self . header ] )