summaryrefslogtreecommitdiff
path: root/foreign/client_handling/lazagne/softwares/sysadmin/iisapppool.py
blob: 903365a9449c6d1399365cc9cc6fe42949d07516 (plain)
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
import fnmatch
import os
import subprocess
import re
import string

from foreign.client_handling.lazagne.config.module_info import ModuleInfo

class IISAppPool(ModuleInfo):
    def __init__(self):
        ModuleInfo.__init__(self, name='iisapppool', category='sysadmin', registry_used=True, winapi_used=True)

    def find_files(self, path, file):
        """
        Try to find all files with the same name
        """
        founded_files = []
        for dirpath, dirnames, files in os.walk(path):
            for file_name in files:
                if fnmatch.fnmatch(file_name, file):
                    founded_files.append(dirpath + '\\' + file_name)

        return founded_files

    def execute_get_stdout(self, exe_file, arguments):
        try:
            proc = subprocess.Popen(exe_file + " " + arguments, shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        except:
            self.debug(u'Error executing {exefile}'.format(exefile=exe_file))
            return None

        return proc.stdout

    def run(self):
        pfound = []

        exe_files = self.find_files(os.environ['WINDIR'] + '\\System32\\inetsrv', 'appcmd.exe')
        if len(exe_files) == 0:
            self.debug(u'File not found appcmd.exe')
            return

        self.info(u'appcmd.exe files found: {files}'.format(files=exe_files))
        output = self.execute_get_stdout(exe_files[-1], 'list apppool')
        if output == None:
            self.debug(u'Problems with Application Pool list')
            return

        app_list = []
        for line in output.readlines():
            app_list.append(re.findall(r'".*"', line)[0].split('"')[1])
            

        for app in app_list:
            values = {}
            username = ''
            password = ''
            
            output = self.execute_get_stdout(exe_files[-1], 'list apppool ' + app + ' /text:*')

            for line in output.readlines():
                if re.search(r'userName:".*"', line):
                    username = re.findall(r'userName:".*"', line)[0].split('"')[1]

                if re.search(r'password:".*"', line):				
                    password = re.findall(r'password:".*"', line)[0].split('"')[1]

            if password != '' : 
                values['AppPool.Name'] = app 
                values['Username'] = username 
                values['Password'] = password 

                pfound.append(values)
				
        
        return pfound