Simon igobl commited on
Commit
270b534
·
1 Parent(s): 8e9be73

Changed python hopsworks requriments. Added a script to gather the .txt files with phishing urls from the phishing database. (#9)

Browse files
pyproject.toml CHANGED
@@ -2,7 +2,7 @@
2
  name = "phising-detection"
3
  version = "0.1.0"
4
  description = "Add your description here"
5
- requires-python = ">=3.12"
6
  dependencies = [
7
  "confluent-kafka>=2.3.0",
8
  "hopsworks==4.2.*",
 
2
  name = "phising-detection"
3
  version = "0.1.0"
4
  description = "Add your description here"
5
+ requires-python = ">=3.12,<3.14"
6
  dependencies = [
7
  "confluent-kafka>=2.3.0",
8
  "hopsworks==4.2.*",
src/phising_detection/data/load_phishing_urls.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+
3
+ Collecting Phishing URLs from the Phishing database on Github and loading them into Hopswork
4
+
5
+ There are multiple pages on github the .txt files with active pshing urls.
6
+ 1. Gather the urls to all the .txt files witht the Phishing urls
7
+ 2. Gather the data from the .txt files
8
+ 3. Load the data into Hopswork
9
+
10
+ """
11
+ # Imports
12
+ import requests
13
+ import pandas as pd
14
+ import sys
15
+ import os
16
+
17
+ # Add the src folder to sys.path so Python can see utils
18
+ src_folder = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) # go up one level from data/
19
+ sys.path.append(src_folder)
20
+
21
+ # Now import
22
+ from utils import hopsworks_utils as hw
23
+
24
+
25
+ # Global variables
26
+ MANIFEST_LINK = "https://raw.githubusercontent.com/Phishing-Database/Phishing.Database/refs/heads/master/phishing-links-ACTIVE/phishing-links-ACTIVE.manifest.txt"
27
+ LINK_BASE = "https://raw.githubusercontent.com/Phishing-Database/Phishing.Database/refs/heads/master/phishing-links-ACTIVE/"
28
+
29
+ def request_manifest_link():
30
+
31
+ """
32
+ Requests and collects data from the manifest page.
33
+ The manifest pages that holds what pages that contain the active Phishing urls
34
+
35
+ """
36
+ # Gather all links to the .txt files with the active Phising urls
37
+ resp = requests.get(MANIFEST_LINK, timeout=10)
38
+ resp.raise_for_status()
39
+ content = resp.text
40
+ txt_links = list(dict.fromkeys(line for line in content.split("\n") if line.strip()))
41
+
42
+ return txt_links
43
+
44
+ def request_phishing_urls(links):
45
+ """
46
+ Requesting data from links to .txt files with phishing urls
47
+
48
+ :param links: a list with the suffix of the URLs for pages with active phishing links
49
+ :return all_dataframe: a dataframe containing the phishing URLs
50
+ """
51
+ all_urls = []
52
+
53
+ for link_suffix in links:
54
+ # Request the data
55
+ resp = requests.get(LINK_BASE + link_suffix, timeout=10)
56
+ resp.raise_for_status()
57
+ content = resp.text
58
+ current_urls = list(dict.fromkeys(line for line in content.split("\n") if line.strip()))
59
+
60
+ # Keep only http/https URLs
61
+ current_urls = [url for url in current_urls if url.startswith(("http://", "https://"))]
62
+ #print(f"amount of collected urls after filtering:{len(current_urls)}")
63
+
64
+ # Append to all_urls
65
+ all_urls += current_urls
66
+
67
+ # remove duplicates across files
68
+ all_urls = list(dict.fromkeys(all_urls))
69
+
70
+
71
+ # Create DataFrame
72
+ df = pd.DataFrame(all_urls, columns=["Phishing_URL"])
73
+
74
+ return df
75
+
76
+ def load_into_hopswork(dataframe):
77
+ """
78
+ Loading the Phishing urls into Hopswork
79
+
80
+ :param dataframes: A list of dataframes containing the Phishing URLs
81
+ """
82
+ # connecting to hopswork
83
+ project = hw.connect_to_hopsworks()
84
+
85
+ # load dataframe into a feature group
86
+ fg = hw.upload_dataframe_to_feature_group(
87
+ project=project,
88
+ df=dataframe,
89
+ feature_group_name="phishing_urls",
90
+ version=1,
91
+ description="A dataframe with all the active Phishing URLs collected from the updated Phishing database",
92
+ primary_key= ["phishing_url"],
93
+ event_time= None,
94
+ online_enabled=True
95
+ )
96
+
97
+
98
+ def main():
99
+ # Get the required link suffixs
100
+ all_links = request_manifest_link()
101
+ # Get the data from the links
102
+ dataframes = request_phishing_urls(all_links)
103
+ # load the data into Hopswork
104
+ load_into_hopswork(dataframes)
105
+
106
+
107
+
108
+ if __name__ == "__main__":
109
+ main()
uv.lock CHANGED
@@ -1,6 +1,6 @@
1
  version = 1
2
  revision = 3
3
- requires-python = ">=3.12"
4
 
5
  [[package]]
6
  name = "avro"
 
1
  version = 1
2
  revision = 3
3
+ requires-python = ">=3.12, <3.14"
4
 
5
  [[package]]
6
  name = "avro"