|
|
|
|
|
|
|
|
|
|
|
from urllib.parse import urlparse,urlencode |
|
|
import ipaddress |
|
|
import re |
|
|
|
|
|
"""#### **3.1.1. Domain of the URL** |
|
|
Here, we are just extracting the domain present in the URL. This feature doesn't have much significance in the training. May even be dropped while training the model. |
|
|
""" |
|
|
''' |
|
|
# 1.Domain of the URL (Domain) |
|
|
def getDomain(url): |
|
|
domain = urlparse(url).netloc |
|
|
if re.match(r"^www.",domain): |
|
|
domain = domain.replace("www.","") |
|
|
return domain''' |
|
|
|
|
|
"""#### **3.1.2. IP Address in the URL** |
|
|
|
|
|
Checks for the presence of IP address in the URL. URLs may have IP address instead of domain name. If an IP address is used as an alternative of the domain name in the URL, we can be sure that someone is trying to steal personal information with this URL. |
|
|
|
|
|
If the domain part of URL has IP address, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def havingIP(url): |
|
|
try: |
|
|
ipaddress.ip_address(url) |
|
|
ip = 1 |
|
|
except: |
|
|
ip = 0 |
|
|
return ip |
|
|
|
|
|
"""#### **3.1.3. "@" Symbol in URL** |
|
|
|
|
|
Checks for the presence of '@' symbol in the URL. Using “@” symbol in the URL leads the browser to ignore everything preceding the “@” symbol and the real address often follows the “@” symbol. |
|
|
|
|
|
If the URL has '@' symbol, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def haveAtSign(url): |
|
|
if "@" in url: |
|
|
at = 1 |
|
|
else: |
|
|
at = 0 |
|
|
return at |
|
|
|
|
|
"""#### **3.1.4. Length of URL** |
|
|
|
|
|
Computes the length of the URL. Phishers can use long URL to hide the doubtful part in the address bar. In this project, if the length of the URL is greater than or equal 54 characters then the URL classified as phishing otherwise legitimate. |
|
|
|
|
|
If the length of URL >= 54 , the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def getLength(url): |
|
|
if len(url) < 54: |
|
|
length = 0 |
|
|
else: |
|
|
length = 1 |
|
|
return length |
|
|
|
|
|
"""#### **3.1.5. Depth of URL** |
|
|
|
|
|
Computes the depth of the URL. This feature calculates the number of sub pages in the given url based on the '/'. |
|
|
|
|
|
The value of feature is a numerical based on the URL. |
|
|
""" |
|
|
|
|
|
|
|
|
def getDepth(url): |
|
|
s = urlparse(url).path.split('/') |
|
|
depth = 0 |
|
|
for j in range(len(s)): |
|
|
if len(s[j]) != 0: |
|
|
depth = depth+1 |
|
|
return depth |
|
|
|
|
|
"""#### **3.1.6. Redirection "//" in URL** |
|
|
|
|
|
Checks the presence of "//" in the URL. The existence of “//” within the URL path means that the user will be redirected to another website. The location of the “//” in URL is computed. We find that if the URL starts with “HTTP”, that means the “//” should appear in the sixth position. However, if the URL employs “HTTPS” then the “//” should appear in seventh position. |
|
|
|
|
|
If the "//" is anywhere in the URL apart from after the protocal, thee value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def redirection(url): |
|
|
pos = url.rfind('//') |
|
|
if pos > 6: |
|
|
if pos > 7: |
|
|
return 1 |
|
|
else: |
|
|
return 0 |
|
|
else: |
|
|
return 0 |
|
|
|
|
|
"""#### **3.1.7. "http/https" in Domain name** |
|
|
|
|
|
Checks for the presence of "http/https" in the domain part of the URL. The phishers may add the “HTTPS” token to the domain part of a URL in order to trick users. |
|
|
|
|
|
If the URL has "http/https" in the domain part, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def httpDomain(url): |
|
|
domain = urlparse(url).netloc |
|
|
if 'https' in domain: |
|
|
return 1 |
|
|
else: |
|
|
return 0 |
|
|
|
|
|
"""#### **3.1.8. Using URL Shortening Services “TinyURL”** |
|
|
|
|
|
URL shortening is a method on the “World Wide Web” in which a URL may be made considerably smaller in length and still lead to the required webpage. This is accomplished by means of an “HTTP Redirect” on a domain name that is short, which links to the webpage that has a long URL. |
|
|
|
|
|
If the URL is using Shortening Services, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
shortening_services = r"bit\.ly|goo\.gl|shorte\.st|go2l\.ink|x\.co|ow\.ly|t\.co|tinyurl|tr\.im|is\.gd|cli\.gs|" \ |
|
|
r"yfrog\.com|migre\.me|ff\.im|tiny\.cc|url4\.eu|twit\.ac|su\.pr|twurl\.nl|snipurl\.com|" \ |
|
|
r"short\.to|BudURL\.com|ping\.fm|post\.ly|Just\.as|bkite\.com|snipr\.com|fic\.kr|loopt\.us|" \ |
|
|
r"doiop\.com|short\.ie|kl\.am|wp\.me|rubyurl\.com|om\.ly|to\.ly|bit\.do|t\.co|lnkd\.in|db\.tt|" \ |
|
|
r"qr\.ae|adf\.ly|goo\.gl|bitly\.com|cur\.lv|tinyurl\.com|ow\.ly|bit\.ly|ity\.im|q\.gs|is\.gd|" \ |
|
|
r"po\.st|bc\.vc|twitthis\.com|u\.to|j\.mp|buzurl\.com|cutt\.us|u\.bb|yourls\.org|x\.co|" \ |
|
|
r"prettylinkpro\.com|scrnch\.me|filoops\.info|vzturl\.com|qr\.net|1url\.com|tweez\.me|v\.gd|" \ |
|
|
r"tr\.im|link\.zip\.net" |
|
|
|
|
|
|
|
|
def tinyURL(url): |
|
|
match=re.search(shortening_services,url) |
|
|
if match: |
|
|
return 1 |
|
|
else: |
|
|
return 0 |
|
|
|
|
|
"""#### **3.1.9. Prefix or Suffix "-" in Domain** |
|
|
|
|
|
Checking the presence of '-' in the domain part of URL. The dash symbol is rarely used in legitimate URLs. Phishers tend to add prefixes or suffixes separated by (-) to the domain name so that users feel that they are dealing with a legitimate webpage. |
|
|
|
|
|
If the URL has '-' symbol in the domain part of the URL, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def prefixSuffix(url): |
|
|
if '-' in urlparse(url).netloc: |
|
|
return 1 |
|
|
else: |
|
|
return 0 |
|
|
|
|
|
"""### **3.2. Domain Based Features:** |
|
|
|
|
|
Many features can be extracted that come under this category. Out of them, below mentioned were considered for this project. |
|
|
|
|
|
* DNS Record |
|
|
* Website Traffic |
|
|
* Age of Domain |
|
|
* End Period of Domain |
|
|
|
|
|
Each of these features are explained and the coded below: |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import re |
|
|
from bs4 import BeautifulSoup |
|
|
|
|
|
import urllib |
|
|
import urllib.request |
|
|
from datetime import datetime |
|
|
|
|
|
"""#### **3.2.1. DNS Record** |
|
|
|
|
|
For phishing websites, either the claimed identity is not recognized by the WHOIS database or no records founded for the hostname. |
|
|
If the DNS record is empty or not found then, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""#### **3.2.2. Web Traffic** |
|
|
|
|
|
This feature measures the popularity of the website by determining the number of visitors and the number of pages they visit. However, since phishing websites live for a short period of time, they may not be recognized by the Alexa database (Alexa the Web Information Company., 1996). By reviewing our dataset, we find that in worst scenarios, legitimate websites ranked among the top 100,000. Furthermore, if the domain has no traffic or is not recognized by the Alexa database, it is classified as “Phishing”. |
|
|
|
|
|
If the rank of the domain < 100000, the vlaue of this feature is 1 (phishing) else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def web_traffic(url): |
|
|
try: |
|
|
|
|
|
url = urllib.parse.quote(url) |
|
|
rank = BeautifulSoup(urllib.request.urlopen("http://data.alexa.com/data?cli=10&dat=s&url=" + url).read(), "xml").find( |
|
|
"REACH")['RANK'] |
|
|
rank = int(rank) |
|
|
except TypeError: |
|
|
return 1 |
|
|
if rank <100000: |
|
|
return 1 |
|
|
else: |
|
|
return 0 |
|
|
|
|
|
"""#### **3.2.3. Age of Domain** |
|
|
|
|
|
This feature can be extracted from WHOIS database. Most phishing websites live for a short period of time. The minimum age of the legitimate domain is considered to be 12 months for this project. Age here is nothing but different between creation and expiration time. |
|
|
|
|
|
If age of domain > 12 months, the vlaue of this feature is 1 (phishing) else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def domainAge(domain_name): |
|
|
creation_date = domain_name.creation_date |
|
|
expiration_date = domain_name.expiration_date |
|
|
if (isinstance(creation_date,str) or isinstance(expiration_date,str)): |
|
|
try: |
|
|
creation_date = datetime.strptime(creation_date,'%Y-%m-%d') |
|
|
expiration_date = datetime.strptime(expiration_date,"%Y-%m-%d") |
|
|
except: |
|
|
return 1 |
|
|
if ((expiration_date is None) or (creation_date is None)): |
|
|
return 1 |
|
|
elif ((type(expiration_date) is list) or (type(creation_date) is list)): |
|
|
return 1 |
|
|
else: |
|
|
ageofdomain = abs((expiration_date - creation_date).days) |
|
|
if ((ageofdomain/30) < 6): |
|
|
age = 1 |
|
|
else: |
|
|
age = 0 |
|
|
return age |
|
|
|
|
|
"""#### **3.2.4. End Period of Domain** |
|
|
|
|
|
This feature can be extracted from WHOIS database. For this feature, the remaining domain time is calculated by finding the different between expiration time & current time. The end period considered for the legitimate domain is 6 months or less for this project. |
|
|
|
|
|
If end period of domain > 6 months, the vlaue of this feature is 1 (phishing) else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def domainEnd(domain_name): |
|
|
expiration_date = domain_name.expiration_date |
|
|
if isinstance(expiration_date,str): |
|
|
try: |
|
|
expiration_date = datetime.strptime(expiration_date,"%Y-%m-%d") |
|
|
except: |
|
|
return 1 |
|
|
if (expiration_date is None): |
|
|
return 1 |
|
|
elif (type(expiration_date) is list): |
|
|
return 1 |
|
|
else: |
|
|
today = datetime.now() |
|
|
end = abs((expiration_date - today).days) |
|
|
if ((end/30) < 6): |
|
|
end = 0 |
|
|
else: |
|
|
end = 1 |
|
|
return end |
|
|
|
|
|
"""## **3.3. HTML and JavaScript based Features** |
|
|
|
|
|
Many features can be extracted that come under this category. Out of them, below mentioned were considered for this project. |
|
|
|
|
|
* IFrame Redirection |
|
|
* Status Bar Customization |
|
|
* Disabling Right Click |
|
|
* Website Forwarding |
|
|
|
|
|
Each of these features are explained and the coded below: |
|
|
""" |
|
|
|
|
|
|
|
|
import requests |
|
|
|
|
|
"""### **3.3.1. IFrame Redirection** |
|
|
|
|
|
IFrame is an HTML tag used to display an additional webpage into one that is currently shown. Phishers can make use of the “iframe” tag and make it invisible i.e. without frame borders. In this regard, phishers make use of the “frameBorder” attribute which causes the browser to render a visual delineation. |
|
|
|
|
|
If the iframe is empty or repsonse is not found then, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def iframe(response): |
|
|
if response == "": |
|
|
return 1 |
|
|
else: |
|
|
if re.findall(r"[<iframe>|<frameBorder>]", response.text): |
|
|
return 0 |
|
|
else: |
|
|
return 1 |
|
|
|
|
|
"""### **3.3.2. Status Bar Customization** |
|
|
|
|
|
Phishers may use JavaScript to show a fake URL in the status bar to users. To extract this feature, we must dig-out the webpage source code, particularly the “onMouseOver” event, and check if it makes any changes on the status bar |
|
|
|
|
|
If the response is empty or onmouseover is found then, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def mouseOver(response): |
|
|
if response == "" : |
|
|
return 1 |
|
|
else: |
|
|
if re.findall("<script>.+onmouseover.+</script>", response.text): |
|
|
return 1 |
|
|
else: |
|
|
return 0 |
|
|
|
|
|
"""### **3.3.3. Disabling Right Click** |
|
|
|
|
|
Phishers use JavaScript to disable the right-click function, so that users cannot view and save the webpage source code. This feature is treated exactly as “Using onMouseOver to hide the Link”. Nonetheless, for this feature, we will search for event “event.button==2” in the webpage source code and check if the right click is disabled. |
|
|
|
|
|
If the response is empty or onmouseover is not found then, the value assigned to this feature is 1 (phishing) or else 0 (legitimate). |
|
|
""" |
|
|
|
|
|
|
|
|
def rightClick(response): |
|
|
if response == "": |
|
|
return 1 |
|
|
else: |
|
|
if re.findall(r"event.button ?== ?2", response.text): |
|
|
return 0 |
|
|
else: |
|
|
return 1 |
|
|
|
|
|
"""### **3.3.4. Website Forwarding** |
|
|
The fine line that distinguishes phishing websites from legitimate ones is how many times a website has been redirected. In our dataset, we find that legitimate websites have been redirected one time max. On the other hand, phishing websites containing this feature have been redirected at least 4 times. |
|
|
""" |
|
|
|
|
|
|
|
|
def forwarding(response): |
|
|
if response == "": |
|
|
return 1 |
|
|
else: |
|
|
if len(response.history) <= 2: |
|
|
return 0 |
|
|
else: |
|
|
return 1 |
|
|
|
|
|
"""## **4. Computing URL Features** |
|
|
|
|
|
Create a list and a function that calls the other functions and stores all the features of the URL in the list. We will extract the features of each URL and append to this list. |
|
|
""" |
|
|
|
|
|
|
|
|
def featureExtraction(url): |
|
|
|
|
|
features = [] |
|
|
|
|
|
|
|
|
features.append(havingIP(url)) |
|
|
features.append(haveAtSign(url)) |
|
|
features.append(getLength(url)) |
|
|
features.append(getDepth(url)) |
|
|
features.append(redirection(url)) |
|
|
features.append(httpDomain(url)) |
|
|
features.append(tinyURL(url)) |
|
|
features.append(prefixSuffix(url)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return features |
|
|
|
|
|
|
|
|
feature_names = ['Domain', 'Have_IP', 'Have_At', 'URL_Length', 'URL_Depth','Redirection', |
|
|
'https_Domain', 'TinyURL', 'Prefix/Suffix', 'Label'] |
|
|
|
|
|
|