From c9de57a6213fb8cbace9035f3d6509f22a6fc05b Mon Sep 17 00:00:00 2001 From: Anshuman Atrey Date: Fri, 1 Nov 2024 14:47:25 +0530 Subject: [PATCH] Port Scanner Project --- Port_Scanner/README.md | 126 +++++++++++++++++ Port_Scanner/port-scanner.py | 53 +++++++ app.js | 262 +++++++++++++++++------------------ 3 files changed, 310 insertions(+), 131 deletions(-) create mode 100644 Port_Scanner/README.md create mode 100644 Port_Scanner/port-scanner.py diff --git a/Port_Scanner/README.md b/Port_Scanner/README.md new file mode 100644 index 00000000..0848ceb8 --- /dev/null +++ b/Port_Scanner/README.md @@ -0,0 +1,126 @@ +# Port Scanner + +## Overview + +A port scanner is a tool used to identify open ports and services available on a networked device. It works by attempting to connect to a range of ports on a specified IP address. If a connection is successful, the port is considered "open," indicating that a service is running on that port. + +### Key Concepts + +- **IP Address**: An Internet Protocol address is a unique identifier for a device on a network. It allows devices to communicate with each other. +- **Port**: A port is a virtual point where network connections start and end. Ports are identified by numbers (0-65535) and are used by protocols to differentiate between different services on a device (e.g., HTTP uses port 80, HTTPS uses port 443). + +## Code Explanation + +The code begins by importing necessary libraries: + +```python +import socket +import threading +from queue import Queue +``` + +These imports provide the following functionalities: + +- `socket`: Access to the BSD socket interface for network communication. +- `threading`: Enables the program to run multiple threads for concurrent operations. +- `Queue`: A thread-safe queue to manage ports to be scanned. + +The number of threads to use for scanning is defined as: + +```python +NUM_THREADS = 100 +``` + +This constant determines the number of threads used for scanning. Increasing the number of threads can accelerate the scanning process. + +A queue is created to hold the ports to be scanned: + +```python +port_queue = Queue() +``` + +This queue holds the ports that need to be scanned, ensuring thread-safe access to the list of ports. + +### Scanning Function + +```python +def scan_port(ip, port): + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(1) # Set a timeout for the connection attempt + result = s.connect_ex((ip, port)) # Try to connect to the port + if result == 0: + print(f"Port {port} is open") + except Exception as e: + print(f"Error scanning port {port}: {e}") +``` + +- **scan_port(ip, port)**: This function attempts to connect to a specified port on the given IP address. + - `socket.socket(socket.AF_INET, socket.SOCK_STREAM)`: Creates a TCP socket. + - `s.settimeout(1)`: Sets a timeout of 1 second for the connection attempt. + - `s.connect_ex((ip, port))`: Attempts to connect to the specified IP and port. Returns 0 if successful (port is open). + - If an error occurs, it prints an error message. + +### Worker Function + +```python +def worker(ip): + while not port_queue.empty(): + port = port_queue.get() # Get a port from the queue + scan_port(ip, port) # Scan the port + port_queue.task_done() # Mark the task as done +``` + +- **worker(ip)**: This function runs in each thread and processes ports from the queue. + - It retrieves a port from the queue, scans it, and marks the task as done. + +### Main Scanning Function + +```python +def port_scanner(ip, start_port, end_port): + + # Fill the queue with ports to scan + + for port in range(start_port, end_port + 1): + port_queue.put(port) + + # Create and start threads + + threads = [] + for _ in range(NUM_THREADS): + thread = threading.Thread(target=worker, args=(ip,)) + thread.start() + threads.append(thread) + + # Wait for all threads to finish + + for thread in threads: + thread.join() +``` + +- **port_scanner(ip, start_port, end_port)**: This function sets up the scanning process. + - It fills the `port_queue` with ports in the specified range. + - It creates and starts multiple threads to scan the ports concurrently. + - It waits for all threads to finish before completing the scan. + +```python +### Main Execution Block +if __name__ == "__main__": + target_ip = input("Enter the IP address to scan: ") + start_port = int(input("Enter the starting port: ")) + end_port = int(input("Enter the ending port: ")) + print(f"Scanning {target_ip} from port {start_port} to {end_port}...") + port_scanner(target_ip, start_port, end_port) + print("Scanning completed.") +``` + +- This block runs when the script is executed directly. +- It prompts the user for the target IP address and the range of ports to scan. +- It calls the `port_scanner` function to perform the scan and prints the results. + +## How to Use + +1. **Run the Script**: Execute the script in a Python environment. +2. **Input the Target IP**: When prompted, enter the IP address of the device you want to scan. +3. **Specify Port Range**: Enter the starting and ending port numbers for the scan. +4. **View Results**: The script will output which ports are open on the specified IP address. diff --git a/Port_Scanner/port-scanner.py b/Port_Scanner/port-scanner.py new file mode 100644 index 00000000..fbfdf938 --- /dev/null +++ b/Port_Scanner/port-scanner.py @@ -0,0 +1,53 @@ +import socket +import threading +from queue import Queue + +# Define the number of threads to use for scanning +NUM_THREADS = 100 + +# Create a queue to hold the ports to be scanned +port_queue = Queue() + +# Function to scan a single port +def scan_port(ip, port): + try: + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.settimeout(1) # Set a timeout for the connection attempt + result = s.connect_ex((ip, port)) # Try to connect to the port + if result == 0: + print(f"Port {port} is open") + except Exception as e: + print(f"Error scanning port {port}: {e}") + +# Worker function to process ports from the queue +def worker(ip): + while not port_queue.empty(): + port = port_queue.get() # Get a port from the queue + scan_port(ip, port) # Scan the port + port_queue.task_done() # Mark the task as done + +# Main function to set up the scanner +def port_scanner(ip, start_port, end_port): + # Fill the queue with ports to scan + for port in range(start_port, end_port + 1): + port_queue.put(port) + + # Create and start threads + threads = [] + for _ in range(NUM_THREADS): + thread = threading.Thread(target=worker, args=(ip,)) + thread.start() + threads.append(thread) + + # Wait for all threads to finish + for thread in threads: + thread.join() + +if __name__ == "__main__": + target_ip = input("Enter the IP address to scan: ") + start_port = int(input("Enter the starting port: ")) + end_port = int(input("Enter the ending port: ")) + + print(f"Scanning {target_ip} from port {start_port} to {end_port}...") + port_scanner(target_ip, start_port, end_port) + print("Scanning completed.") diff --git a/app.js b/app.js index 6797a654..a0c9159e 100644 --- a/app.js +++ b/app.js @@ -1,132 +1,132 @@ -const express = require('express'); -const mongoose = require('mongoose'); -const bcrypt = require('bcrypt'); -const cors = require('cors'); - -// Import required modules - -// Create an Express app -const app = express(); - -// Enable CORS -app.use(express.json()); -app.use(cors()); - -// Connect to MongoDB -mongoose.connect('mongodb://localhost:27017/cafe', { useNewUrlParser: true, useUnifiedTopology: true }) - .then(() => { - console.log('Connected to MongoDB'); - }) - .catch((error) => { - console.error('Error connecting to MongoDB:', error); - }); - -// Define a user schema -const userSchema = new mongoose.Schema({ - username: { type: String, required: true }, - password: { type: String, required: true } -}); - -// Define a user model -const User = mongoose.model('User', userSchema); -const productSchema = new mongoose.Schema({ - name: { type: String, required: true }, - price: { type: Number, required: true }, - quantity: { type: Number, required: true } -}); -const Product = mongoose.model('Product', productSchema); -// Register a new user -app.post('/register', async (req, res) => { - try { - const { username, password } = req.body; - - // Check if the username already exists - const existingUser = await User.findOne({ username }); - if (existingUser) { - return res.status(400).json({ message: 'Username already exists' }); - } - - // Hash the password - const hashedPassword = await bcrypt.hash(password, 10); - - // Create a new user - const newUser = new User({ username, password: hashedPassword }); - await newUser.save(); - - res.status(201).json({ message: 'User registered successfully' }); - } catch (error) { - console.error('Error registering user:', error); - res.status(500).json({ message: 'Internal server error' }); - } -}); - -// User login -app.post('/login', async (req, res) => { - try { - const { username, password } = req.body; - - // Find the user by username - const user = await User.findOne({ username }); - if (!user) { - return res.status(404).json({ message: 'User not found' }); - } - - // Compare the password - const isPasswordValid = await bcrypt.compare(password, user.password); - if (!isPasswordValid) { - return res.status(401).json({ message: 'Invalid password' }); - } - - res.status(200).json({ message: 'Login successful' }); - } catch (error) { - console.error('Error logging in:', error); - res.status(500).json({ message: 'Internal server error' }); - } -}); - -app.get('/products',async (req,res)=>{ - try{ - const products = await Product.find(); - res.status(200).json(products); - }catch(error){ - console.error('Error getting products:',error); - res.status(500).json({message:'Internal server error'}); - } -}) - -app.post('/products',async (req,res)=>{ - try{ - const {name,price,quantity} = req.body; - const newProduct = new Product({name,price,quantity}); - await newProduct.save(); - res.status(201).json({message:'Product added successfully'}); - }catch(error){ - console.error('Error adding product:',error); - res.status(500).json({message:'Internal server error'}); - } -}) - -app.put('/products/:id',async (req,res)=>{ - try{ - const {name,price,quantity} = req.body; - await Product.findByIdAndUpdate(req.params.id,{name,price,quantity}); - res.status(200).json({message:'Product updated successfully'}); - }catch(error){ - console.error('Error updating product:',error); - res.status(500).json({message:'Internal server error'}); - } -}) - -app.delete('/products/:id',async (req,res)=>{ - try{ - await Product.findByIdAndDelete(req.params.id); - res.status(200).json({message:'Product deleted successfully'}); - }catch(error){ - console.error('Error deleting product:',error); - res.status(500).json({message:'Internal server error'}); - } -}) -// Start the server -app.listen(3000, () => { - console.log('Server started on port 3000'); +const express = require('express'); +const mongoose = require('mongoose'); +const bcrypt = require('bcrypt'); +const cors = require('cors'); + +// Import required modules + +// Create an Express app +const app = express(); + +// Enable CORS +app.use(express.json()); +app.use(cors()); + +// Connect to MongoDB +mongoose.connect('mongodb://localhost:27017/cafe', { useNewUrlParser: true, useUnifiedTopology: true }) + .then(() => { + console.log('Connected to MongoDB'); + }) + .catch((error) => { + console.error('Error connecting to MongoDB:', error); + }); + +// Define a user schema +const userSchema = new mongoose.Schema({ + username: { type: String, required: true }, + password: { type: String, required: true } +}); + +// Define a user model +const User = mongoose.model('User', userSchema); +const productSchema = new mongoose.Schema({ + name: { type: String, required: true }, + price: { type: Number, required: true }, + quantity: { type: Number, required: true } +}); +const Product = mongoose.model('Product', productSchema); +// Register a new user +app.post('/register', async (req, res) => { + try { + const { username, password } = req.body; + + // Check if the username already exists + const existingUser = await User.findOne({ username }); + if (existingUser) { + return res.status(400).json({ message: 'Username already exists' }); + } + + // Hash the password + const hashedPassword = await bcrypt.hash(password, 10); + + // Create a new user + const newUser = new User({ username, password: hashedPassword }); + await newUser.save(); + + res.status(201).json({ message: 'User registered successfully' }); + } catch (error) { + console.error('Error registering user:', error); + res.status(500).json({ message: 'Internal server error' }); + } +}); + +// User login +app.post('/login', async (req, res) => { + try { + const { username, password } = req.body; + + // Find the user by username + const user = await User.findOne({ username }); + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } + + // Compare the password + const isPasswordValid = await bcrypt.compare(password, user.password); + if (!isPasswordValid) { + return res.status(401).json({ message: 'Invalid password' }); + } + + res.status(200).json({ message: 'Login successful' }); + } catch (error) { + console.error('Error logging in:', error); + res.status(500).json({ message: 'Internal server error' }); + } +}); + +app.get('/products',async (req,res)=>{ + try{ + const products = await Product.find(); + res.status(200).json(products); + }catch(error){ + console.error('Error getting products:',error); + res.status(500).json({message:'Internal server error'}); + } +}) + +app.post('/products',async (req,res)=>{ + try{ + const {name,price,quantity} = req.body; + const newProduct = new Product({name,price,quantity}); + await newProduct.save(); + res.status(201).json({message:'Product added successfully'}); + }catch(error){ + console.error('Error adding product:',error); + res.status(500).json({message:'Internal server error'}); + } +}) + +app.put('/products/:id',async (req,res)=>{ + try{ + const {name,price,quantity} = req.body; + await Product.findByIdAndUpdate(req.params.id,{name,price,quantity}); + res.status(200).json({message:'Product updated successfully'}); + }catch(error){ + console.error('Error updating product:',error); + res.status(500).json({message:'Internal server error'}); + } +}) + +app.delete('/products/:id',async (req,res)=>{ + try{ + await Product.findByIdAndDelete(req.params.id); + res.status(200).json({message:'Product deleted successfully'}); + }catch(error){ + console.error('Error deleting product:',error); + res.status(500).json({message:'Internal server error'}); + } +}) +// Start the server +app.listen(3000, () => { + console.log('Server started on port 3000'); }); \ No newline at end of file