Monday 11 March 2024

Cybersecurity using Python

 


1. Hashing Passwords:

import hashlib

def hash_password(password):

    hashed_password = hashlib.sha256(password.encode()).hexdigest()

    return hashed_password

# Example

password = "my_secure_password"

hashed_password = hash_password(password)

print("Hashed Password:", hashed_password)

#clcoding.com 

Hashed Password: 2c9a8d02fc17ae77e926d38fe83c3529d6638d1d636379503f0c6400e063445f

2. Generating Random Passwords:

import random

import string

def generate_random_password(length=12):

    characters = string.ascii_letters + string.digits + string.punctuation

    password = ''.join(random.choice(characters) for _ in range(length))

    return password

# Example

random_password = generate_random_password()

print("Random Password:", random_password)

#clcoding.com 

Random Password: zH7~ANoO:7#S

3. Network Scanning with Scapy:

from scapy.all import IP, ICMP, sr1

def ping(host):

    packet = IP(dst=host)/ICMP()

    response = sr1(packet, timeout=2, verbose=0)

    if response:

        return f"{host} is online"

    else:

        return f"{host} is offline"

# Example

host_to_scan = "example.com"

result = ping(host_to_scan)

print(result)

#clcoding.com

4. Web Scraping for Security Research:

import requests

from bs4 import BeautifulSoup

def scrape_security_news():

    url = "https://example-security-news.com"

    response = requests.get(url)

    soup = BeautifulSoup(response.text, 'html.parser')

    headlines = soup.find_all('h2', class_='security-headline')

    return [headline.text for headline in headlines]

# Example

security_headlines = scrape_security_news()

print("Security Headlines:", security_headlines)

#clcoding.com

5. Password Cracking Simulation:

import hashlib

def simulate_password_cracking(hashed_password, password_list):

    for password in password_list:

        if hashlib.sha256(password.encode()).hexdigest() == hashed_password:

            return f"Password cracked: {password}"

    return "Password not found"

# Example

hashed_password_to_crack = "d033e22ae348aeb5660fc2140aec35850c4da997"

common_passwords = ["password", "123456", "qwerty", "admin"]

result = simulate_password_cracking(hashed_password_to_crack, common_passwords)

print(result)

#clcoding.com

6. Secure File Handling:

import os

def secure_file_deletion(file_path):

    with open(file_path, 'w') as file:

        file.write(os.urandom(1024))  

        # Overwrite the file with random data

    os.remove(file_path)

    print(f"{file_path} securely deleted")

# Example

file_path_to_delete = "example.txt"

secure_file_deletion(file_path_to_delete)

#clcoding.com


0 Comments:

Post a Comment

Popular Posts

Categories

AI (31) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (20) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (887) Python Coding Challenge (284) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses