Skip to content

Learn fundamental Python concepts and apply them to cybersecurity tasks like log analysis and attack detection. Build a strong foundation for automating security monitoring while understanding key data protection principles.

Notifications You must be signed in to change notification settings

vaibhavi-tilak/PythonForCybersecurity_Part1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

PythonForCybersecurity_Part1

Learn fundamental Python concepts and apply them to cybersecurity tasks like log analysis and attack detection. Build a strong foundation for automating security monitoring while understanding key data protection principles.


Hey! I’m Vaibhavi, but you can call me Vai. I’m passionate about cybersecurity and always exploring simpler ways to solve complex problems. This repository is designed to guide you on how to kickstart your journey in Python programming effortlessly, with a focus on cybersecurity applications.

Python is one of the most popular programming languages due to its simplicity and versatility. This series, Cybersecurity with Python, covers the basics of Python along with its vital applications in cybersecurity. You’ll learn how to use Python for log analysis, identifying code vulnerabilities, and ensuring data accuracy while maintaining CIA (Confidentiality, Integrity, and Availability) properties in data transmission. The series also includes detection of brute-force attacks using network logs and automating incident response by sending alerts to the security team via email.


Overview

This project offers a beginner-friendly Python programming series tailored for cybersecurity enthusiasts. You will learn essential Python concepts alongside practical applications in cybersecurity such as:

  • Log analysis
  • Identifying code vulnerabilities
  • Ensuring data confidentiality, integrity, and availability (CIA triad)
  • Detecting brute-force attacks using network logs
  • Automating incident response by sending security alerts via email

What You Will Learn

  • Basic Python syntax: print, input, variables, and data types
  • Operators: comparison, logical, and assignment
  • Control flow with if-else statements and loops (for, while)
  • Writing reusable functions
  • Working with collections: lists, tuples, sets, dictionaries
  • File handling to read and write security logs
  • Using regular expressions for pattern matching
  • Applying Python skills to real-world cybersecurity tasks
  • Understanding key security principles for data protection

Getting Started

Prerequisites

  • Python 3.x installed on your system
  • Basic familiarity with programming concepts (helpful but not required)

How to Use This Repository

  1. Start by exploring the basics of Python programming with provided scripts and examples.
  2. Progress to cybersecurity-related scripts focusing on log parsing, vulnerability detection, and incident automation.
  3. Experiment by modifying and extending the examples to deepen your understanding.

Table of Contents

  1. Print and Input Functions
  2. Variables and Data Types
  3. Operations
  4. Loops
  5. If Statements
  6. Functions
  7. Collections in Python
  8. File Handling

1. Print and Input Functions

Print Function

The print() function outputs data to the console. It can display strings, numbers, variables, or multiple items together.

  • Printing a string

print("Hello, world!")

  • Printing a number

print(123)

  • Printing a variable

x = 42
print(x)

  • Printing multiple items

name = "Alice"
age = 30
print("Name:", name, "Age:", age)

Input Function

The input() function collects user input. It shows an optional message, waits for the user to type input, and returns it as a string. To use input as other types, convert it explicitly.


name = input("Enter your name: ")
print("Hello, " + name + "!")


2. Variables and Data Types

Variables store data values without needing explicit types. Python assigns types automatically based on values. Common data types include:

  • Integer (int): Whole numbers like 123, -123, 0
  • Float: Numbers with decimals like 3.14, -0.001
  • String: Sequences of characters like "Hello, world!"
  • Boolean: Logical values True or False


# Defining variables

name = "Alice"     \# String
iq = 140           \# Integer
height = 5.6       \# Float
is_student = True  \# Boolean

# Checking types

print(type(name))
print(type(iq))
print(type(height))
print(type(is_student))


3. Operations

3.1 Comparison Operators

Return Boolean results by comparing values.


print(x == y)  \# Equal to
print(x != y)  \# Not equal to
print(x > y)   \# Greater than
print(x < y)   \# Less than
print(x >= y)  \# Greater than or equal to
print(x <= y)  \# Less than or equal to

3.2 Logical Operators

Combine Boolean conditions.


a = True
b = False
print(a and b)  \# Logical AND
print(a or b)   \# Logical OR
print(not a)    \# Logical NOT

3.3 Assignment Operators

Assign or modify variable values.


x = 10
y = 15
x += 5  \# x = x + 5
x *= 2  \# x = x * 2
y -= 3  \# y = y - 3

print(x)
print(y)


4. Loops

for Loop

Iterates over sequences or ranges.


for i in range(5):
print(i)

while Loop

Repeats as long as a condition is true.


count = 0
while count < 5:
print(count)
count += 1


5. If Statements

Execute code conditionally with if, optional elif, and else.


x = 10

if x > 5:
print("x is greater than 5")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")


6. Functions

Reusable blocks of code to perform specific tasks.


def greet(name):
return f"Hello, {name}!"

print(greet("Alice"))


7. Collections in Python

7.1 Lists [ ]

Ordered, mutable sequences.


fruits = ["apple", "banana", "cherry"]
print(fruits)  \# Access first element
fruits.append("orange")
print(fruits)

7.2 Tuples ( )

Ordered, immutable sequences.


coordinates = (1, "hello", 3.14, True)
print(coordinates)

7.3 Sets { }

Unordered collections of unique items.


unique_numbers = {1, 2, 3, 3}
print(unique_numbers)  \# Outputs: {1, 2, 3}

7.4 Dictionaries { Key: Value }

Unordered key-value pairs.


student = {"name": "Rambika", "age": 21}
print(student["name"])


8. File Handling

8.1 Reading a File


a = open("Logfile.txt", "r")
print(a.read())
a.close()

Reading line-by-line:


with open("Logfile.txt", "r") as b:
for line in b:
print(line.strip())

8.2 Writing to a File


f = open("Logfile.txt", "a")
f.write("\n172.16.22.11 445 SMB file access")
f.close()

Reading after appending:


with open("Logfile.txt", "r") as f:
print(f.read())

8.3 Using Regular Expressions


import re

text = "Name: John, Age: 30"
name = re.search(r"Name: (.*?),", text).group(1)
age = re.search(r"Age: (.*)", text).group(1)
print(f"Name: {name}, Age: {age}")

8.4 Reading File Line By Line with split()

Split lines into lists by delimiter (default space).


with open("Logfile.txt", "r") as file:
for line in file:
words = line.strip().split()
print(words)


Why Learn Python for Cybersecurity?

Python’s simplicity and powerful libraries make it ideal for automating cybersecurity tasks, analyzing logs, detecting vulnerabilities, and responding swiftly to threats. This series empowers you to build practical tools for security monitoring and automation grounded in strong Python skills.


What’s Next?

Stay tuned for upcoming parts where we dive deeper into real-world cybersecurity applications including advanced log analysis, brute-force attack detection, and automated incident response systems. 🚀


Happy Coding,
Vai (Vaibhavi)

About

Learn fundamental Python concepts and apply them to cybersecurity tasks like log analysis and attack detection. Build a strong foundation for automating security monitoring while understanding key data protection principles.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published