2015-11-30 21:29:30 +08:00
|
|
|
import sys
|
2015-12-09 22:33:44 +08:00
|
|
|
import socket
|
|
|
|
import psutil
|
2015-11-30 21:29:30 +08:00
|
|
|
from enum import IntEnum
|
2017-09-10 18:11:51 +08:00
|
|
|
from network.info import get_host_subnets, local_ips
|
2015-11-30 21:29:30 +08:00
|
|
|
|
|
|
|
__author__ = 'uri'
|
|
|
|
|
|
|
|
|
|
|
|
class OperatingSystem(IntEnum):
|
|
|
|
Windows = 0
|
|
|
|
Linux = 1
|
|
|
|
|
|
|
|
|
|
|
|
class SystemInfoCollector(object):
|
|
|
|
"""
|
|
|
|
A class that checks the current operating system and calls system information collecting modules accordingly
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.os = SystemInfoCollector.get_os()
|
|
|
|
if OperatingSystem.Windows == self.os:
|
|
|
|
from windows_info_collector import WindowsInfoCollector
|
|
|
|
self.collector = WindowsInfoCollector()
|
|
|
|
else:
|
|
|
|
from linux_info_collector import LinuxInfoCollector
|
|
|
|
self.collector = LinuxInfoCollector()
|
|
|
|
|
|
|
|
def get_info(self):
|
|
|
|
return self.collector.get_info()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_os():
|
|
|
|
if sys.platform.startswith("win"):
|
|
|
|
return OperatingSystem.Windows
|
|
|
|
else:
|
|
|
|
return OperatingSystem.Linux
|
2015-12-09 22:33:44 +08:00
|
|
|
|
|
|
|
|
|
|
|
class InfoCollector(object):
|
|
|
|
"""
|
|
|
|
Generic Info Collection module
|
|
|
|
"""
|
2016-08-20 22:58:59 +08:00
|
|
|
|
2015-12-09 22:33:44 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.info = {}
|
|
|
|
|
|
|
|
def get_hostname(self):
|
2017-09-11 21:56:23 +08:00
|
|
|
"""
|
2017-09-12 00:24:18 +08:00
|
|
|
Adds the fully qualified computer hostname to the system information.
|
|
|
|
:return: Nothing
|
2017-09-11 21:56:23 +08:00
|
|
|
"""
|
|
|
|
self.info['hostname'] = socket.getfqdn()
|
2015-12-09 22:33:44 +08:00
|
|
|
|
|
|
|
def get_process_list(self):
|
2017-09-12 00:24:18 +08:00
|
|
|
"""
|
|
|
|
Adds process information from the host to the system information.
|
|
|
|
Currently lists process name, ID, parent ID, command line
|
|
|
|
and the full image path of each process.
|
|
|
|
:return: Nothing
|
|
|
|
"""
|
2015-12-09 22:33:44 +08:00
|
|
|
processes = {}
|
|
|
|
for process in psutil.process_iter():
|
2016-03-02 21:38:51 +08:00
|
|
|
try:
|
|
|
|
processes[process.pid] = {"name": process.name(),
|
2016-08-20 22:58:59 +08:00
|
|
|
"pid": process.pid,
|
|
|
|
"ppid": process.ppid(),
|
|
|
|
"cmdline": " ".join(process.cmdline()),
|
|
|
|
"full_image_path": process.exe(),
|
|
|
|
}
|
2016-03-02 21:38:51 +08:00
|
|
|
except psutil.AccessDenied:
|
2016-08-20 22:58:59 +08:00
|
|
|
# we may be running as non root
|
|
|
|
# and some processes are impossible to acquire in Windows/Linux
|
|
|
|
# in this case we'll just add what we can
|
2016-03-02 21:38:51 +08:00
|
|
|
processes[process.pid] = {"name": "null",
|
2016-08-20 22:58:59 +08:00
|
|
|
"pid": process.pid,
|
|
|
|
"ppid": process.ppid(),
|
|
|
|
"cmdline": "ACCESS DENIED",
|
|
|
|
"full_image_path": "null",
|
|
|
|
}
|
2017-09-25 20:32:13 +08:00
|
|
|
continue
|
|
|
|
except WindowsError:
|
|
|
|
# we may be running as non root
|
|
|
|
# and some processes are impossible to acquire in Windows/Linux
|
|
|
|
# in this case we'll just add what we can
|
|
|
|
processes[process.pid] = {"name": "null",
|
|
|
|
"pid": process.pid,
|
|
|
|
"ppid": process.ppid(),
|
|
|
|
"cmdline": "ACCESS DENIED",
|
|
|
|
"full_image_path": "null",
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
|
2015-12-09 22:33:44 +08:00
|
|
|
self.info['process_list'] = processes
|
2017-09-10 18:11:51 +08:00
|
|
|
|
|
|
|
def get_network_info(self):
|
2017-09-11 21:56:23 +08:00
|
|
|
"""
|
|
|
|
Adds network information from the host to the system information.
|
2017-09-12 00:24:18 +08:00
|
|
|
Currently updates with a list of networks accessible from host,
|
|
|
|
containing host ip and the subnet range.
|
2017-09-11 21:56:23 +08:00
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
self.info['network_info'] = {'networks': get_host_subnets()}
|