Python – ElementTree XML parser

Displays all elements and subelemts of an XML file

#!/usr/bin/python
from xml.etree import ElementTree

with open ('/home/gmastrokostas/PycharmProjects/learning/Linux_int.xml', 'rt') as file:
    tree = ElementTree.parse(file)

for node_1 in tree.iter():
    print node_1.tag, node_1.attrib
Share Button

Python – Parse through an XML file generated by Nmap.

The script below uses two different modules. The ElementTree and the NmapParser. The former module is used to manipulate XML files. The latter module is used specifically to manipulate XML files generated by the Nmap port scanner. In both cases both modules will need to read the actual XML file. This script is better suited for an environment that has Linux and Windows servers.

The Nmap command used to generate the capture is

1
nmap -T4 -A -v 10.0.0.29 -oX file.xml

Sample of output

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
HostName:                desktop.sfentona.lol
Operating System Guess:  Linux 3.7 - 3.9 - Accuracy Detection 98
-----------------------------------------------------------------------------
Port: 22    State: open  Protocol: tcp Product: None            Version: None       ExtrInfo: protocol 2.0
Port: 80    State: open  Protocol: tcp Product: nginx           Version: 1.4.6      ExtrInfo: Ubuntu    
Port: 139   State: open  Protocol: tcp Product: Samba smbd      Version: 3.X        ExtrInfo: workgroup: DESKTOP
Port: 445   State: open  Protocol: tcp Product: Samba smbd      Version: 3.X        ExtrInfo: workgroup: DESKTOP



HostName:                    ad1.sfentona.lol
Operating System Guess:  Microsoft Windows 7 or Windows Server 2012 - Accuracy Detection 100
-----------------------------------------------------------------------------
Port: 53    State: open  Protocol: tcp Product: Microsoft DNS                       Version: None                 ExtrInfo: None      
Port: 88    State: open  Protocol: tcp Product: Windows 2003 Kerberos               Version: None                 ExtrInfo: server time: 2014-12-28 03:22:28Z
Port: 135   State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 139   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 389   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 445   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 464   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 593   State: open  Protocol: tcp Product: Microsoft Windows RPC over HTTP     Version: 1.0                  ExtrInfo: None      
Port: 636   State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 3268  State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 3269  State: open  Protocol: tcp Product: None                                Version: None                 ExtrInfo: None      
Port: 3389  State: open  Protocol: tcp Product: Microsoft Terminal Service          Version: None                 ExtrInfo: None      
Port: 49152 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49153 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49154 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49155 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49156 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49158 State: open  Protocol: tcp Product: Microsoft Windows RPC over HTTP     Version: 1.0                  ExtrInfo: None      
Port: 49159 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49167 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None      
Port: 49175 State: open  Protocol: tcp Product: Microsoft Windows RPC               Version: None                 ExtrInfo: None

 
Continue reading

Share Button