Search code examples
python-2.7html-generation

KeyError while generating an HTML page


I'm generating an HTML page from a simple template:

<div id="dateReference">
    <h1 id="currentDate">Exploit automation completed on: {date_of_attack}</h1>
</div>
<div id="amountOfHost">
    <h4 id="hostCount">Total of {amount_of_hosts} separate hosts attacked</h4>
</div>
<div id="hostDropDown" class="dropdown-content">
    {all_hosts_attacked}
</div>
<div id="successfulAttacks">
    <h4 id="successAttacksBanner">Successful Attacks:</h4>
</div>
{exploit_table}

How I'm creating the tags is by generating a simple string using a simple class:

import os
import datetime

import lib.settings


class HtmlPageGenerator(object):

    def __init__(self, successes, failures, host_count):
        self.success = successes
        self.fails = failures
        self.host_count = host_count
        self.html_template = lib.settings.HTML_PAGE_TEMPLATE
        self.attacked_hosts_list = open(lib.settings.HOST_FILE).readlines()

    def _generate_html_table(self, headers):
        retval = '<table id="generatedExploitTable"><tr>'
        for header in headers:
            retval += "<th>{}</th>".format(header)
        retval += "</tr><tr>"
        for value in self.success:
            retval += "<td>{}</td>".format(value)
        retval += "</tr></table>"
        return retval

    def _generate_drop_down_menu(self):
        retval = ""
        for host in self.attacked_hosts_list:
            retval += '<a href="#">{}</a>'.format(host.strip())
        return retval

    def generator(self):
        if not os.path.exists(lib.settings.HTML_PAGE_PATH):
            os.makedirs(lib.settings.HTML_PAGE_PATH)
        with open(self.html_template, 'r') as template, open(lib.settings.HTML_PAGE_GENERATION_FILE_PATH, 'a+') as out:
            to_format = template.read()
            out.write(
                to_format.format(
                    date_of_attack=str(datetime.datetime.today()).split(".")[0],
                    exploit_table=self._generate_html_table(["Exploit Paths"]),
                    amount_of_hosts=self.host_count,
                    all_hosts_attacked=self._generate_drop_down_menu()
                )
            )
        return lib.settings.HTML_PAGE_GENERATION_FILE_PATH

The _generate_html_table function is working properly, and has generated successfully, the issue is when I try to generate the <a href="#" tags it throws the following error:

Traceback (most recent call last):
  File "xxxxx.py", line 10, in <module>
    23
  File "/Users/admin/bin/tools/xxxxx/lib/page_generator.py", line 42, in generator
    all_hosts_attacked=self._generate_drop_down_menu()
KeyError: '\n            document'

What is causing my error and how can I fix this successfully? I've attempted to generate the links as a list and return it joined instead but it throws the same Exception. Any help with this would be great


Solution

  • Figured it out, I had javascript in the HTML, so as we all know javascript uses brackets {} in order to parse them: {{}} fixed.