Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/smanonr..../opt/sharedra...
File: vhost_data.py
#! /opt/imh-python/bin/python3
[0] Fix | Delete
''' Gather important VirtualHost information. '''
[1] Fix | Delete
[2] Fix | Delete
import re
[3] Fix | Delete
import os
[4] Fix | Delete
import json
[5] Fix | Delete
import logging
[6] Fix | Delete
from argparse import ArgumentParser
[7] Fix | Delete
[8] Fix | Delete
import envinfo
[9] Fix | Delete
[10] Fix | Delete
LOGGER = logging.getLogger(__name__)
[11] Fix | Delete
[12] Fix | Delete
__author__ = "Daniel K"
[13] Fix | Delete
__email__ = "danielk@inmotionhosting.com"
[14] Fix | Delete
[15] Fix | Delete
[16] Fix | Delete
def get_vhost_list(conf=None):
[17] Fix | Delete
'''Collect necessary information from VirtualHost entries.'''
[18] Fix | Delete
[19] Fix | Delete
if conf is None:
[20] Fix | Delete
conf = envinfo.get_data()['apache_conf']
[21] Fix | Delete
[22] Fix | Delete
vhost_list = []
[23] Fix | Delete
[24] Fix | Delete
vhost_rx = re.compile(
[25] Fix | Delete
r'<VirtualHost (?P<ip>[0-9.]+):(?P<port>[0-9]+)( ([0-9.]+):([0-9]+))*>'
[26] Fix | Delete
)
[27] Fix | Delete
[28] Fix | Delete
if os.path.exists(conf):
[29] Fix | Delete
with open(conf, encoding='utf-8') as file_handle:
[30] Fix | Delete
vhost_data = {}
[31] Fix | Delete
vhost_data['hostnames'] = []
[32] Fix | Delete
try:
[33] Fix | Delete
for line in file_handle:
[34] Fix | Delete
vhost_match = vhost_rx.search(line)
[35] Fix | Delete
[36] Fix | Delete
if "#" not in line:
[37] Fix | Delete
if "/VirtualHost" in line:
[38] Fix | Delete
if 'ip' in vhost_data:
[39] Fix | Delete
if 'port' in vhost_data:
[40] Fix | Delete
vhost_list.append(vhost_data)
[41] Fix | Delete
vhost_data = {}
[42] Fix | Delete
vhost_data['hostnames'] = []
[43] Fix | Delete
elif vhost_match is not None:
[44] Fix | Delete
vhost_data['ip'] = vhost_match.group('ip')
[45] Fix | Delete
vhost_data['port'] = vhost_match.group('port')
[46] Fix | Delete
elif "ServerName" in line:
[47] Fix | Delete
vhost_data['hostnames'].append(line.split()[1])
[48] Fix | Delete
elif "ServerAlias" in line:
[49] Fix | Delete
line = line.strip()
[50] Fix | Delete
for host in line.split(" ", 1)[1].split():
[51] Fix | Delete
vhost_data['hostnames'].append(host)
[52] Fix | Delete
elif "Include" in line:
[53] Fix | Delete
if 'includes' not in vhost_data:
[54] Fix | Delete
vhost_data['includes'] = []
[55] Fix | Delete
vhost_data['includes'].append(line.split()[1])
[56] Fix | Delete
elif "SSLCertificateFile" in line:
[57] Fix | Delete
vhost_data['ssl'] = line.split()[1]
[58] Fix | Delete
elif "SSLCertificateKeyFile" in line:
[59] Fix | Delete
vhost_data['key'] = line.split()[1]
[60] Fix | Delete
elif "SSLCertificateChainFile" in line:
[61] Fix | Delete
vhost_data['ca'] = line.split()[1]
[62] Fix | Delete
elif "DocumentRoot" in line:
[63] Fix | Delete
vhost_data['docroot'] = line.split()[1]
[64] Fix | Delete
except OSError:
[65] Fix | Delete
LOGGER.error("Error reading file")
[66] Fix | Delete
return None
[67] Fix | Delete
[68] Fix | Delete
return vhost_list
[69] Fix | Delete
[70] Fix | Delete
[71] Fix | Delete
def search_vhost_list(vhost_list, host=None, ip_address=None, port=None):
[72] Fix | Delete
'''Search vhost_list for first matching entry for given information'''
[73] Fix | Delete
[74] Fix | Delete
if vhost_list is None:
[75] Fix | Delete
vhost_list = get_vhost_list()
[76] Fix | Delete
[77] Fix | Delete
working_list = []
[78] Fix | Delete
[79] Fix | Delete
for vhost_data in vhost_list:
[80] Fix | Delete
if (ip_address is None) or (ip_address in vhost_data['ip']):
[81] Fix | Delete
if (port is None) or (port in vhost_data['port']):
[82] Fix | Delete
working_list.append(vhost_data)
[83] Fix | Delete
[84] Fix | Delete
if len(working_list) == 0:
[85] Fix | Delete
for vhost_data in vhost_list:
[86] Fix | Delete
if (ip_address is None) or (ip_address in vhost_data['ip']):
[87] Fix | Delete
if '*' in vhost_data['port']:
[88] Fix | Delete
working_list.append(vhost_data)
[89] Fix | Delete
[90] Fix | Delete
if len(working_list) == 0:
[91] Fix | Delete
return vhost_list[1]
[92] Fix | Delete
[93] Fix | Delete
for vhost_data in working_list:
[94] Fix | Delete
if host in vhost_data['hostnames']:
[95] Fix | Delete
return vhost_data
[96] Fix | Delete
[97] Fix | Delete
return None
[98] Fix | Delete
[99] Fix | Delete
[100] Fix | Delete
def parse_args():
[101] Fix | Delete
'''Parse command line aruments'''
[102] Fix | Delete
[103] Fix | Delete
parser = ArgumentParser(description=__doc__)
[104] Fix | Delete
[105] Fix | Delete
output_parser_group = parser.add_argument_group("Output options")
[106] Fix | Delete
output_group = output_parser_group.add_mutually_exclusive_group()
[107] Fix | Delete
[108] Fix | Delete
output_group.add_argument(
[109] Fix | Delete
"-d",
[110] Fix | Delete
"--documentroot",
[111] Fix | Delete
action='store_true',
[112] Fix | Delete
help="Output only the document root.",
[113] Fix | Delete
)
[114] Fix | Delete
[115] Fix | Delete
output_group.add_argument(
[116] Fix | Delete
"-j",
[117] Fix | Delete
"--json",
[118] Fix | Delete
action='store_true',
[119] Fix | Delete
help="Output only the document root.",
[120] Fix | Delete
)
[121] Fix | Delete
[122] Fix | Delete
parser.add_argument(
[123] Fix | Delete
'-c',
[124] Fix | Delete
'--config',
[125] Fix | Delete
type=str,
[126] Fix | Delete
default=None,
[127] Fix | Delete
help="Location of a custom Apache configuration.",
[128] Fix | Delete
)
[129] Fix | Delete
[130] Fix | Delete
search_group = parser.add_argument_group("Search options")
[131] Fix | Delete
[132] Fix | Delete
search_group.add_argument(
[133] Fix | Delete
"-i", "--ip", type=str, default=None, help="IP to search."
[134] Fix | Delete
)
[135] Fix | Delete
[136] Fix | Delete
port_group = search_group.add_mutually_exclusive_group()
[137] Fix | Delete
[138] Fix | Delete
port_group.add_argument(
[139] Fix | Delete
"-p", "--port", type=str, default=None, help="Port to search."
[140] Fix | Delete
)
[141] Fix | Delete
[142] Fix | Delete
port_group.add_argument(
[143] Fix | Delete
"-s",
[144] Fix | Delete
"--standardports",
[145] Fix | Delete
type=str,
[146] Fix | Delete
default=None,
[147] Fix | Delete
help="Try port 443 first, and if no entry, try port 80.",
[148] Fix | Delete
)
[149] Fix | Delete
[150] Fix | Delete
search_group.add_argument("hostname", type=str, help="Hostname to search.")
[151] Fix | Delete
[152] Fix | Delete
args = parser.parse_args()
[153] Fix | Delete
[154] Fix | Delete
if args.documentroot:
[155] Fix | Delete
output = "docroot"
[156] Fix | Delete
elif args.json:
[157] Fix | Delete
output = "json"
[158] Fix | Delete
else:
[159] Fix | Delete
output = "standard"
[160] Fix | Delete
[161] Fix | Delete
return output, args.ip, args.port, args.hostname, args.config
[162] Fix | Delete
[163] Fix | Delete
[164] Fix | Delete
def main():
[165] Fix | Delete
'''Main function for script'''
[166] Fix | Delete
[167] Fix | Delete
(output, ip_address, port, host, conf) = parse_args()
[168] Fix | Delete
[169] Fix | Delete
vhost_list = get_vhost_list(conf)
[170] Fix | Delete
vhost_data = search_vhost_list(vhost_list, host, ip_address, port)
[171] Fix | Delete
[172] Fix | Delete
if vhost_data is None:
[173] Fix | Delete
print("No results found")
[174] Fix | Delete
elif output == "docroot":
[175] Fix | Delete
print(vhost_data['docroot'])
[176] Fix | Delete
elif output == "json":
[177] Fix | Delete
print(
[178] Fix | Delete
json.dumps(
[179] Fix | Delete
vhost_data, sort_keys=True, indent=4, separators=(',', ': ')
[180] Fix | Delete
)
[181] Fix | Delete
)
[182] Fix | Delete
else:
[183] Fix | Delete
print("Hostname: %s" % host)
[184] Fix | Delete
print("IP/port: {}:{}".format(vhost_data['ip'], vhost_data['port']))
[185] Fix | Delete
print("Document Root: %s" % vhost_data['docroot'])
[186] Fix | Delete
if 'ssl' in vhost_data:
[187] Fix | Delete
print("SSL Certificate: %s" % vhost_data['ssl'])
[188] Fix | Delete
print("SSL Private Key: %s" % vhost_data['key'])
[189] Fix | Delete
if 'ca' in vhost_data:
[190] Fix | Delete
print("SSL CA Bundle: %s" % vhost_data['ca'])
[191] Fix | Delete
if 'includes' in vhost_data:
[192] Fix | Delete
for path in vhost_data['includes']:
[193] Fix | Delete
print("Include: %s" % path)
[194] Fix | Delete
[195] Fix | Delete
[196] Fix | Delete
if __name__ == "__main__":
[197] Fix | Delete
main()
[198] Fix | Delete
[199] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function