cyber security lab excercise
cyber security lab excercise
Requirements :
Algorithm :
Procedure :
Code :
import socket
def port_scanner(ip, port):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(1) # Timeout for connection
s.connect((ip, port))
print(f"[+] Port {port} is OPEN on {ip}")
except:
print(f"[-] Port {port} is CLOSED on {ip}")
# Example Usage
ip = "8.8.8.8" # Localhost
for port in range(45,55):
port_scanner(ip, port)
Output :
[-] Port 45 is CLOSED on 8.8.8.8
[-] Port 46 is CLOSED on 8.8.8.8
[-] Port 47 is CLOSED on 8.8.8.8
[-] Port 48 is CLOSED on 8.8.8.8
[-] Port 49 is CLOSED on 8.8.8.8
[-] Port 50 is CLOSED on 8.8.8.8
[-] Port 51 is CLOSED on 8.8.8.8
[-] Port 52 is CLOSED on 8.8.8.8
[+] Port 53 is OPEN on 8.8.8.8
[-] Port 54 is CLOSED on 8.8.8.8
Result :
The program checks each port and reports whether it is open or closed. It
helps identify which ports are accessible on the target IP.
2. Log Analysis: Capturing User Input with Timestamps
Aim :
Requirements :
Algorithm :
Procedure :
def capture_input():
# Prompt user for input and allow multiple entries
while True:
user_input = input("Please type something (type 'exit' to stop): ")
if user_input.lower() == 'exit':
print("Exiting input capture.")
break
# Open the file in append mode and log the input with a timestamp
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
with open("keylog.txt", "a") as f:
f.write(f"[{timestamp}] {user_input}\n")
Result :
The program logs user input along with a timestamp in a file. It continues
until the user types "exit," saving all inputs to a text file.
3. Web Application Security: Testing for SQL Injection
Vulnerabilities
Aim :
Requirements :
Algorithm :
Procedure :
Code :
import requests
# Example Usage
check_sql_injection("https://portal.naanmudhalvan.tn.gov.in/login", "login")
check_sql_injection("http://example.com/search", "query")
check_sql_injection("http://testphp.vulnweb.com/login.php", "username")
check_sql_injection("http://testphp.vulnweb.com/search.php", "search")
Output :
Seems safe: https://portal.naanmudhalvan.tn.gov.in/login
Seems safe: http://example.com/search
Potential SQL injection vulnerability at:
http://testphp.vulnweb.com/login.php?username=' OR 1=1 –
Potential SQL injection vulnerability at:
http://testphp.vulnweb.com/search.php?search=' OR 1=1 --
Result :
The program checks for SQL injection vulnerabilities in web applications by
injecting a test payload. It reports potential vulnerabilities based on the server
response.
4. 2-Factor Authentication: Implementing Time-based
One-Time Password (OTP) Generation
Aim :
Requirements :
Algorithm :
Procedure :
# 2 FACTOR AUTHENTICATION
import time
def generate_otp():
return str((int(time.time()) + secret_key) % 1000000).zfill(6)
Output :
Enter your password: 12345
Your OTP is: 643284
Enter the OTP: 643284
Authentication complete.
Result :
The program prompts the user for a password and generates an OTP for
two-factor authentication. The user is authenticated only if the correct OTP is
entered.
5. Simple Caesar Cipher: Implementing Encryption and
Decryption with Shift Values
Aim :
Requirements :
Algorithm :
Procedure :
# Example usage
message = input("Enter the message: ")
shift = int(input("Enter the shift value: "))
Output :
Enter the message: hello world
Enter the shift value: 5
Encrypted message: mjqqt btwqi
Decrypted message: hello world
Result :
The program encrypts and decrypts a message using a Caesar cipher with a
specified shift. It demonstrates how text can be securely encoded and decoded.