Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Scanner #1

Merged
merged 1 commit into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PoRtScAnNeR/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
71 changes: 71 additions & 0 deletions PoRtScAnNeR/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# PoRtScAnNeR

**PoRtScAnNeR** is a simple command-line tool written in Go that performs TCP and UDP port scanning on a specified host. This project helps in checking the status of open or closed ports on a host and is useful for network diagnostics or security testing.

## Features

- **TCP Connect Scan**: Scans a list of TCP ports to check if they are open or closed.
- **UDP Scan**: Checks if specified UDP ports are open or filtered.
- **Simple CLI Usage**: Takes command-line input for hostname, ports, and scan type.
- **Port Range Parsing**: Parses a comma-separated list of ports for scanning.

## Installation

### Prerequisites

- **Go**: Make sure you have Go installed. You can download and install it from [here](https://golang.org/doc/install).

### Clone the Repository

To clone the repository and navigate to the project directory, run the following:

```bash
git clone https://github.com/YourUsername/PoRtScAnNeR.git
cd PoRtScAnNeR/cmd/portscanneR
```bash
./main.go <hostname> <ports> [scan_type]
```
- hostname: The target host to scan (e.g., localhost or 192.168.1.1).
- ports: A comma-separated list of ports to scan (e.g., "80,443,8080").
- scan_type (optional): The type of scan, either TCP or UDP (defaults to TCP).
### Example Commands
1. TCP Scan on ports 80, 443, and 8080:

```bash
./portscanner localhost "80,443,8080" TCP
```
2. UDP Scan on ports 53 and 67:
```bash
./portscanner localhost "53,67" UDP
```
### Output Example
```bash
Scanning localhost with TCP scan on ports [80 443 8080]
Starting TCP Scan...
Port 80 is closed
Port 443 is closed
Port 8080 is closed
```
```bash
Scanning localhost with UDP scan on ports [53 67]
Starting UDP Scan...
Port 53 is open or filtered (UDP)
Port 67 is open or filtered (UDP)
```
## Project Structure
```bash
/PoRtScAnNeR
├── go.mod # Go module file
├── go.sum # Go dependencies
├── main.go # Main application entry point
├── internal
│ ├── scanner # Package for scanning functionality
│ │ ├── tcp.go # TCP scanning functions
│ │ └── udp.go # UDP scanning functions
│ └── utils # Package for utility functions
│ └── ports.go # Functions for parsing ports
```
- main.go: The main program that handles user input and starts the scan.
- internal/scanner/tcp.go: Contains the logic for performing TCP port scans.
- internal/scanner/udp.go: Contains the logic for performing UDP port scans.
- internal/utils/ports.go: Utility functions for parsing port input and checking port validity.
46 changes: 46 additions & 0 deletions PoRtScAnNeR/cmd/portscanner/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package main

import (
"fmt"
"os"
"strings"

"github.com/Ayushi40804/PoRtScAnNeR/internal/scanner" // Adjust the import path as needed
"github.com/Ayushi40804/PoRtScAnNeR/internal/utils" // Adjust the import path as needed
)

func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: portscanner <hostname> <ports> [scan_type]")
fmt.Println("Example: portscanner localhost '80,443,8080' TCP")
os.Exit(1)
}

hostname := os.Args[1]
portsInput := os.Args[2]
scanType := "TCP" // Default scan type
if len(os.Args) == 4 {
scanType = os.Args[3]
}

// Convert ports from string to slice of integers
ports, err := utils.ParsePorts(portsInput)
if err != nil {
fmt.Printf("Invalid port input: %v\n", err)
os.Exit(1)
}

// Perform the scan
fmt.Printf("Scanning %s with %s scan on ports %v\n", hostname, scanType, ports)
switch strings.ToUpper(scanType) {
case "TCP":
fmt.Println("Starting TCP Scan...")
scanner.TCPConnectScan(hostname, ports)
case "UDP":
fmt.Println("Starting UDP Scan...")
scanner.UDPScan(hostname, ports)
default:
fmt.Println("Invalid scan type selected. Please choose TCP or UDP.")
os.Exit(1)
}
}
3 changes: 3 additions & 0 deletions PoRtScAnNeR/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/Ayushi40804/PoRtScAnNeR

go 1.23.2
Empty file added PoRtScAnNeR/go.sum
Empty file.
21 changes: 21 additions & 0 deletions PoRtScAnNeR/internal/scanner/tcp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package scanner

import (
"fmt"
"net"
"time"
)

// TCPConnectScan performs a TCP Connect scan on the specified ports
func TCPConnectScan(hostname string, ports []int) {
for _, port := range ports {
address := fmt.Sprintf("%s:%d", hostname, port)
conn, err := net.DialTimeout("tcp", address, time.Second)
if err != nil {
fmt.Printf("Port %d is closed\n", port)
} else {
fmt.Printf("Port %d is open\n", port)
conn.Close()
}
}
}
27 changes: 27 additions & 0 deletions PoRtScAnNeR/internal/scanner/udp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package scanner

import (
"fmt"
"net"
"time"
)

// UDPScan performs a UDP scan on the specified ports
func UDPScan(hostname string, ports []int) {
for _, port := range ports {
address := fmt.Sprintf("%s:%d", hostname, port)
conn, err := net.DialTimeout("udp", address, time.Second)
if err != nil {
fmt.Printf("Port %d is closed or filtered (UDP)\n", port)
continue
}
defer conn.Close()

_, err = conn.Write([]byte{})
if err != nil {
fmt.Printf("Failed to send UDP packet to port %d\n", port)
continue
}
fmt.Printf("Port %d is open or filtered (UDP)\n", port)
}
}
28 changes: 28 additions & 0 deletions PoRtScAnNeR/internal/utils/ports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"fmt"
"strconv"
"strings"
)

// ParsePorts converts a comma-separated list of port numbers (as a string) into a slice of integers.
func ParsePorts(ports string) ([]int, error) {
var result []int
for _, p := range strings.Split(ports, ",") {
port, err := strconv.Atoi(strings.TrimSpace(p))
if err != nil {
return nil, err // Return an error if the conversion fails
}
if !IsValidPort(port) {
return nil, fmt.Errorf("port %d is out of range", port)
}
result = append(result, port)
}
return result, nil
}

// IsValidPort checks if the given port number is valid (between 1 and 65535).
func IsValidPort(port int) bool {
return port >= 1 && port <= 65535
}