File size: 10,136 Bytes
49d98a7
 
 
3aa35cf
 
 
 
 
49d98a7
3aa35cf
32441c9
 
 
49d98a7
3aa35cf
49d98a7
 
 
 
 
3aa35cf
49d98a7
32441c9
3aa35cf
 
 
 
 
 
 
 
32441c9
 
3aa35cf
 
 
 
 
 
 
 
 
32441c9
 
3aa35cf
 
 
 
 
 
 
32441c9
 
3aa35cf
 
 
32441c9
 
3aa35cf
 
 
32441c9
 
3aa35cf
 
32441c9
 
3aa35cf
 
 
32441c9
 
3aa35cf
 
 
 
 
 
 
32441c9
 
3aa35cf
 
 
32441c9
 
3aa35cf
 
 
 
 
 
 
 
 
 
32441c9
 
3aa35cf
32441c9
 
3aa35cf
 
 
 
 
 
 
32441c9
 
3aa35cf
 
 
32441c9
 
3aa35cf
 
 
 
 
 
 
32441c9
 
 
 
 
 
3aa35cf
 
 
32441c9
 
3aa35cf
 
 
32441c9
 
3aa35cf
32441c9
 
3aa35cf
 
32441c9
 
3aa35cf
 
 
 
32441c9
 
3aa35cf
 
 
 
 
 
32441c9
49d98a7
 
 
3aa35cf
 
 
 
 
 
32441c9
3aa35cf
 
32441c9
3aa35cf
 
32441c9
 
3aa35cf
 
 
 
 
 
 
 
 
 
49d98a7
 
3aa35cf
 
49d98a7
 
3aa35cf
 
49d98a7
d0e3211
49d98a7
3aa35cf
 
5639d2c
49d98a7
3aa35cf
 
 
 
49d98a7
3aa35cf
 
 
 
 
49d98a7
 
 
3aa35cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49d98a7
 
3aa35cf
49d98a7
 
 
3aa35cf
49d98a7
 
 
3aa35cf
49d98a7
 
 
 
3aa35cf
 
49d98a7
3aa35cf
 
 
 
49d98a7
3aa35cf
 
 
 
 
 
 
 
 
 
 
5639d2c
 
3aa35cf
 
49d98a7
3aa35cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import socket
import threading
import time
from queue import Queue

import uvicorn
from fastapi import FastAPI, Form, Request
from fastapi.responses import HTMLResponse

# --- UI Template (HTML, CSS, JS) ---
# NOTE: All curly braces for CSS and JS are doubled (e.g., {{ ... }}) 
# to escape them from Python's .format() method. The only single-brace
# placeholder is {content}.

HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>FastAPI Port Scanner</title>
    <style>
        :root {{
            --bg-color: #1a1a1a;
            --surface-color: #2c2c2c;
            --primary-color: #00aaff;
            --primary-hover-color: #0088cc;
            --text-color: #e0e0e0;
            --text-muted-color: #a0a0a0;
            --success-color: #4CAF50;
            --border-color: #444;
        }}
        body {{
            font-family: 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: var(--bg-color);
            color: var(--text-color);
            margin: 0;
            padding: 2rem;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }}
        .container {{
            background-color: var(--surface-color);
            padding: 2.5rem;
            border-radius: 12px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
            width: 100%;
            max-width: 650px;
            border: 1px solid var(--border-color);
        }}
        h1, h2 {{
            color: var(--primary-color);
            text-align: center;
            margin-top: 0;
        }}
        form {{
            display: flex;
            flex-direction: column;
            gap: 1.2rem;
        }}
        .form-group {{
            display: flex;
            flex-direction: column;
        }}
        label {{
            font-weight: bold;
            margin-bottom: 0.5rem;
            font-size: 0.9rem;
        }}
        input {{
            padding: 0.8rem 1rem;
            border-radius: 6px;
            border: 1px solid var(--border-color);
            background-color: var(--bg-color);
            color: var(--text-color);
            font-size: 1rem;
            transition: border-color 0.3s, box-shadow 0.3s;
        }}
        input:focus {{
            outline: none;
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(0, 170, 255, 0.2);
        }}
        button {{
            padding: 1rem;
            border-radius: 6px;
            border: none;
            background-color: var(--primary-color);
            color: #fff;
            font-size: 1.1rem;
            font-weight: bold;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 1rem;
        }}
        button:hover {{
            background-color: var(--primary-hover-color);
        }}
        .disclaimer {{
            font-size: 0.8rem;
            color: #ffeb3b;
            text-align: center;
            margin-top: 1.5rem;
            border: 1px dashed #ffeb3b;
            padding: 0.5rem;
            border-radius: 4px;
        }}
        .loader {{
            text-align: center;
            padding: 3rem 0;
            display: none; /* Hidden by default */
        }}
        .spinner {{
            border: 8px solid var(--border-color);
            border-top: 8px solid var(--primary-color);
            border-radius: 50%;
            width: 60px;
            height: 60px;
            animation: spin 1s linear infinite;
            margin: 0 auto 1rem;
        }}
        @keyframes spin {{
            0% {{ transform: rotate(0deg); }}
            100% {{ transform: rotate(360deg); }}
        }}
        .results-table {{
            width: 100%;
            border-collapse: collapse;
            margin-top: 1.5rem;
        }}
        .results-table th, .results-table td {{
            padding: 0.8rem;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }}
        .results-table th {{
            color: var(--primary-color);
        }}
        .open-port {{
            color: var(--success-color);
            font-weight: bold;
        }}
        .scan-summary p {{
            font-size: 1.1em;
            background: #252525;
            padding: 0.8em;
            border-radius: 6px;
        }}
        a {{
            color: var(--primary-color);
            text-decoration: none;
            display: inline-block;
            margin-top: 1.5rem;
            text-align: center;
            width: 100%;
        }}
    </style>
</head>
<body>
    <div class="container" id="main-container">
        <!-- The content will be replaced by render_form or render_results -->
        {content}
    </div>

    <script>
        function showLoader() {{
            const formContainer = document.getElementById('form-container');
            const loader = document.getElementById('loader');
            if (formContainer && loader) {{
                formContainer.style.display = 'none';
                loader.style.display = 'block';
            }}
        }}
    </script>
</body>
</html>
"""

FORM_CONTENT = """
<div id="form-container">
    <h1>FastAPI Port Scanner</h1>
    <form action="/scan" method="post" onsubmit="showLoader()">
        <div class="form-group">
            <label for="target">Target IP Address or Hostname:</label>
            <input type="text" id="target" name="target" value="127.0.0.1" required>
        </div>
        <div class="form-group">
            <label for="start_port">Start Port:</label>
            <input type="number" id="start_port" name="start_port" value="1" min="1" max="65535" required>
        </div>
        <div class="form-group">
            <label for="end_port">End Port:</label>
            <input type="number" id="end_port" name="end_port" value="65535" min="1" max="65535" required>
        </div>
        <div class="form-group">
            <label for="threads">Number of Threads (Concurrency):</label>
            <input type="number" id="threads" name="threads" value="2500" min="1" max="2500" required>
        </div>
        <button type="submit">Start Scan</button>
    </form>
    <div class="disclaimer">
        <strong>Warning:</strong> Use only on networks you own or have explicit permission to scan. Unauthorized scanning is illegal.
    </div>
</div>
<div class="loader" id="loader">
    <div class="spinner"></div>
    <p>Scanning in progress... This may take a moment.</p>
</div>
"""

# --- Scanner Logic ---
def run_scan(target_ip: str, start_port: int, end_port: int, num_threads: int):
    """
    Sets up the queue and worker threads for scanning and returns the results.
    This is now a self-contained function that returns results instead of using globals.
    """
    q = Queue()
    open_ports = []
    thread_lock = threading.Lock()

    def worker():
        while not q.empty():
            port = q.get()
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                s.settimeout(0.5)  # Aggressive timeout
                try:
                    if s.connect_ex((target_ip, port)) == 0:
                        with thread_lock:
                            open_ports.append(port)
                except (socket.timeout, ConnectionRefusedError):
                    pass
            q.task_done()

    # Populate queue
    for port in range(start_port, end_port + 1):
        q.put(port)

    # Create and start threads
    threads = []
    for _ in range(num_threads):
        thread = threading.Thread(target=worker, daemon=True)
        thread.start()
        threads.append(thread)
    
    q.join()  # Block until all ports are scanned

    open_ports.sort()
    return open_ports

# --- FastAPI Application ---
app = FastAPI()

@app.get("/", response_class=HTMLResponse)
async def get_form():
    """Serves the main page with the input form."""
    return HTML_TEMPLATE.format(content=FORM_CONTENT)

@app.post("/scan", response_class=HTMLResponse)
async def scan(
    target: str = Form(...),
    start_port: int = Form(...),
    end_port: int = Form(...),
    threads: int = Form(...)
):
    """Handles the scan request, runs the scanner, and returns the results page."""
    # Input validation
    if not (1 <= start_port <= end_port <= 65535):
        return HTMLResponse(content="<h1>Error: Invalid port range.</h1>", status_code=400)
    if not (1 <= threads <= 2500):
        return HTMLResponse(content="<h1>Error: Thread count must be between 1 and 2500.</h1>", status_code=400)

    # Resolve hostname
    try:
        target_ip = socket.gethostbyname(target)
    except socket.gaierror:
        return HTMLResponse(content=f"<h1>Error: Could not resolve hostname '{target}'</h1>", status_code=400)

    start_time = time.time()
    open_ports = run_scan(target_ip, start_port, end_port, threads)
    duration = time.time() - start_time

    # Build results content
    results_html = f"""
    <h2>Scan Results</h2>
    <div class="scan-summary">
        <p><strong>Target:</strong> {target} ({target_ip})</p>
        <p><strong>Time Taken:</strong> {duration:.2f} seconds</p>
        <p><strong>Open Ports Found:</strong> {len(open_ports)}</p>
    </div>
    """
    
    if open_ports:
        rows = "".join(f'<tr><td class="open-port">{port}</td></tr>' for port in open_ports)
        results_html += f"""
        <table class="results-table">
            <thead><tr><th>Open Port</th></tr></thead>
            <tbody>{rows}</tbody>
        </table>
        """
    else:
        results_html += "<p>No open ports were found in the specified range.</p>"
        
    results_html += '<a href="/">Scan Another Target</a>'

    return HTML_TEMPLATE.format(content=results_html)

# To run this app, you need uvicorn: `pip install uvicorn`
# Then run from your terminal: `uvicorn main:app --reload`
if __name__ == "__main__":
    uvicorn.run("main:app", host="0.0.0.0", port=5000, reload=False)