Skip to content

Commit 88c8ba8

Browse files
committed
Updated the code
1 parent c2b6443 commit 88c8ba8

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

Pentesting/detectDDoS.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@
1010
# Time limit for tracking IPs (e.g., 60 seconds)
1111
TIME_WINDOW = 60
1212

13+
# Total packets processed
14+
total_packets = 0
15+
1316
# Block an IP using iptables (for Linux systems)
1417
def block_ip(ip):
1518
try:
1619
# Block the IP using iptables
1720
subprocess.run(['iptables', '-A', 'INPUT', '-s', ip, '-j', 'DROP'], check=True)
18-
print(f"Blocked IP: {ip}")
21+
print(f"Blocked IP: {ip}") # Log the blocked IP
1922
except subprocess.CalledProcessError as e:
20-
print(f"Error blocking IP {ip}: {e}")
21-
23+
except subprocess.CalledProcessError as e:
24+
c
25+
c
2226
# Clean up old IPs from the dictionary based on a time window
2327
def clean_old_ips():
2428
current_time = time.time()
@@ -29,11 +33,16 @@ def clean_old_ips():
2933

3034
# Function to detect a possible DDoS attack
3135
def detect_ddos(packet):
32-
global ip_count
36+
global ip_count, total_packets
37+
total_packets += 1
38+
print(f"Total packets processed: {total_packets}") # Log total number of packets
39+
3340
if IP in packet:
3441
ip_src = packet[IP].src
3542
current_time = time.time()
3643

44+
print(f"Packet detected from: {ip_src}") # Log each packet's source IP
45+
3746
# Clean up old IPs periodically
3847
clean_old_ips()
3948

@@ -44,11 +53,14 @@ def detect_ddos(packet):
4453
else:
4554
ip_count[ip_src] = (current_time, 1)
4655

56+
# Log how many packets received from this IP
57+
print(f"Packet count for {ip_src}: {ip_count[ip_src][1]}")
58+
4759
# If packet count exceeds the threshold, block the IP
4860
if ip_count[ip_src][1] > PACKET_THRESHOLD:
4961
print(f"Possible DDoS attack from {ip_src}")
5062
block_ip(ip_src)
5163
del ip_count[ip_src] # Stop tracking once blocked
5264

5365
# Start sniffing network traffic and apply the detect_ddos function on captured IP packets
54-
sniff(filter="ip", prn=detect_ddos)
66+
sniff(filter="ip", prn=detect_ddos)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import yfinance as yf
2+
import pandas as pd
3+
import schedule
4+
import time
5+
import matplotlib.pyplot as plt
6+
7+
# Fetch dividend data for a single stock
8+
def fetch_dividend_data(ticker):
9+
stock = yf.Ticker(ticker)
10+
dividend_data = stock.dividends # Fetch dividends history
11+
return dividend_data
12+
13+
# Calculate total expected dividend income for a single stock
14+
def calculate_dividend_income(ticker, shares_owned):
15+
dividends = fetch_dividend_data(ticker)
16+
if dividends.empty:
17+
print(f"No dividend data available for {ticker}")
18+
return 0
19+
else:
20+
# Sum up the total dividends paid in the last year
21+
total_dividends_per_share = dividends.last('365D').sum() # Dividends in the last 365 days
22+
total_income = total_dividends_per_share * shares_owned
23+
return total_income
24+
25+
# Watchlist with the number of shares owned for each stock
26+
portfolio = {
27+
"AAPL": 50, # 50 shares of Apple
28+
"MSFT": 30, # 30 shares of Microsoft
29+
"T": 100, # 100 shares of AT&T
30+
"JNJ": 25 # 25 shares of Johnson & Johnson
31+
}
32+
33+
# Track expected dividend income for all stocks in the portfolio
34+
def track_dividend_income(portfolio):
35+
dividend_summary = pd.DataFrame(columns=["Stock", "Shares Owned", "Expected Dividend Income"])
36+
37+
for stock, shares_owned in portfolio.items():
38+
income = calculate_dividend_income(stock, shares_owned)
39+
dividend_summary = pd.concat([dividend_summary, pd.DataFrame({"Stock": [stock], "Shares Owned": [shares_owned], "Expected Dividend Income": [income]})])
40+
41+
return dividend_summary
42+
43+
# Create a dashboard to visualize expected dividend income for all stocks
44+
def create_dashboard(portfolio):
45+
income_summary = track_dividend_income(portfolio)
46+
47+
fig, ax = plt.subplots(figsize=(10, 6))
48+
income_summary.plot(x="Stock", y="Expected Dividend Income", kind="bar", ax=ax)
49+
plt.title("Expected Dividend Income from Portfolio")
50+
plt.xlabel("Stock")
51+
plt.ylabel("Expected Dividend Income (in USD)")
52+
plt.show()
53+
54+
# Job to calculate and save dividend income daily
55+
def job():
56+
print("Updating expected dividend income data...")
57+
summary = track_dividend_income(portfolio)
58+
summary.to_csv('dividend_income_tracker.csv', index=False)
59+
print(summary)
60+
61+
# Schedule to run every day at 10 AM
62+
schedule.every().day.at("10:00").do(job)
63+
64+
# Initial calculation and dashboard creation
65+
create_dashboard(portfolio)
66+
67+
# Start the schedule loop after creating the dashboard
68+
while True:
69+
schedule.run_pending()
70+
time.sleep(1)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
yfinance==0.2.43
2+
pandas==2.2.3
3+
numpy==2.1.1
4+
schedule==1.2.2
5+
matplotlib==3.9.2

0 commit comments

Comments
 (0)