Initial commit: Werkzeuge-Sammlung

Enthält:
- rdp_client.py: RDP Client mit GUI und Monitor-Auswahl
- rdp.sh: Bash-basierter RDP Client
- teamleader_test/: Network Scanner Fullstack-App
- teamleader_test2/: Network Mapper CLI

Subdirectories mit eigenem Repo wurden ausgeschlossen.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
root
2026-01-28 09:39:24 +01:00
commit cb073786b3
112 changed files with 23543 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
"""API router initialization."""
from fastapi import APIRouter
from app.api.endpoints import scans, hosts, topology, websocket
api_router = APIRouter()
# Include endpoint routers
api_router.include_router(scans.router, prefix="/scans", tags=["scans"])
api_router.include_router(hosts.router, prefix="/hosts", tags=["hosts"])
api_router.include_router(topology.router, prefix="/topology", tags=["topology"])
api_router.include_router(websocket.router, prefix="/ws", tags=["websocket"])

View File

@@ -0,0 +1 @@
"""API endpoints package."""

View File

@@ -0,0 +1,222 @@
"""Host API endpoints."""
import logging
from typing import List, Optional
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from sqlalchemy import or_
from app.database import get_db
from app.models import Host, Service
from app.schemas import HostResponse, HostDetailResponse, ServiceResponse, NetworkStatistics
from app.services.topology_service import TopologyService
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("", response_model=List[HostResponse])
def list_hosts(
status: Optional[str] = Query(None, description="Filter by status (online/offline)"),
limit: int = Query(100, ge=1, le=1000),
offset: int = Query(0, ge=0),
search: Optional[str] = Query(None, description="Search by IP or hostname"),
db: Session = Depends(get_db)
):
"""
List discovered hosts.
Args:
status: Filter by host status
limit: Maximum number of hosts to return
offset: Number of hosts to skip
search: Search query
db: Database session
Returns:
List of hosts
"""
query = db.query(Host)
# Apply filters
if status:
query = query.filter(Host.status == status)
if search:
search_pattern = f"%{search}%"
query = query.filter(
or_(
Host.ip_address.like(search_pattern),
Host.hostname.like(search_pattern)
)
)
# Order by last seen
query = query.order_by(Host.last_seen.desc())
# Apply pagination
hosts = query.limit(limit).offset(offset).all()
return hosts
@router.get("/statistics", response_model=NetworkStatistics)
def get_network_statistics(db: Session = Depends(get_db)):
"""
Get network statistics.
Args:
db: Database session
Returns:
Network statistics
"""
topology_service = TopologyService(db)
stats = topology_service.get_network_statistics()
# Get most common services
from sqlalchemy import func
service_counts = db.query(
Service.service_name,
func.count(Service.id).label('count')
).filter(
Service.service_name.isnot(None)
).group_by(
Service.service_name
).order_by(
func.count(Service.id).desc()
).limit(10).all()
# Get last scan time
from app.models import Scan
last_scan = db.query(Scan).order_by(Scan.started_at.desc()).first()
return NetworkStatistics(
total_hosts=stats['total_hosts'],
online_hosts=stats['online_hosts'],
offline_hosts=stats['offline_hosts'],
total_services=stats['total_services'],
total_scans=db.query(func.count(Scan.id)).scalar() or 0,
last_scan=last_scan.started_at if last_scan else None,
most_common_services=[
{'service_name': s[0], 'count': s[1]}
for s in service_counts
]
)
@router.get("/by-service/{service_name}", response_model=List[HostResponse])
def get_hosts_by_service(
service_name: str,
limit: int = Query(100, ge=1, le=1000),
offset: int = Query(0, ge=0),
db: Session = Depends(get_db)
):
"""
Get all hosts that provide a specific service.
Args:
service_name: Service name to filter by
limit: Maximum number of hosts to return
offset: Number of hosts to skip
db: Database session
Returns:
List of hosts providing the service
"""
hosts = db.query(Host).join(Service).filter(
Service.service_name == service_name
).distinct().order_by(
Host.last_seen.desc()
).limit(limit).offset(offset).all()
return hosts
@router.get("/{host_id}", response_model=HostDetailResponse)
def get_host_detail(host_id: int, db: Session = Depends(get_db)):
"""
Get detailed information about a specific host.
Args:
host_id: Host ID
db: Database session
Returns:
Detailed host information
"""
host = db.query(Host).filter(Host.id == host_id).first()
if not host:
raise HTTPException(status_code=404, detail=f"Host {host_id} not found")
return host
@router.get("/{host_id}/services", response_model=List[ServiceResponse])
def get_host_services(host_id: int, db: Session = Depends(get_db)):
"""
Get all services for a specific host.
Args:
host_id: Host ID
db: Database session
Returns:
List of services
"""
host = db.query(Host).filter(Host.id == host_id).first()
if not host:
raise HTTPException(status_code=404, detail=f"Host {host_id} not found")
return host.services
@router.delete("/{host_id}")
def delete_host(host_id: int, db: Session = Depends(get_db)):
"""
Delete a host from the database.
Args:
host_id: Host ID
db: Database session
Returns:
Success message
"""
host = db.query(Host).filter(Host.id == host_id).first()
if not host:
raise HTTPException(status_code=404, detail=f"Host {host_id} not found")
db.delete(host)
db.commit()
logger.info(f"Deleted host {host_id} ({host.ip_address})")
return {"message": f"Host {host_id} deleted successfully"}
@router.get("/ip/{ip_address}", response_model=HostResponse)
def get_host_by_ip(ip_address: str, db: Session = Depends(get_db)):
"""
Get host information by IP address.
Args:
ip_address: IP address
db: Database session
Returns:
Host information
"""
host = db.query(Host).filter(Host.ip_address == ip_address).first()
if not host:
raise HTTPException(
status_code=404,
detail=f"Host with IP {ip_address} not found"
)
return host

View File

@@ -0,0 +1,209 @@
"""Scan API endpoints."""
import asyncio
import logging
from typing import List
from fastapi import APIRouter, Depends, HTTPException, BackgroundTasks
from sqlalchemy.orm import Session
from app.database import get_db
from app.schemas import (
ScanConfigRequest,
ScanResponse,
ScanStatusResponse,
ScanStartResponse,
ScanStatus as ScanStatusEnum
)
from app.services.scan_service import ScanService
from app.api.endpoints.websocket import (
send_scan_progress,
send_host_discovered,
send_scan_completed,
send_scan_failed
)
logger = logging.getLogger(__name__)
router = APIRouter()
@router.post("/start", response_model=ScanStartResponse, status_code=202)
async def start_scan(
config: ScanConfigRequest,
db: Session = Depends(get_db)
):
"""
Start a new network scan.
Args:
config: Scan configuration
background_tasks: Background task handler
db: Database session
Returns:
Scan start response with scan ID
"""
try:
scan_service = ScanService(db)
# Create scan record
scan = scan_service.create_scan(config)
scan_id = scan.id
# Create progress callback for WebSocket updates
async def progress_callback(update: dict):
"""Send progress updates via WebSocket."""
update_type = update.get('type')
update_scan_id = update.get('scan_id', scan_id)
if update_type == 'scan_progress':
await send_scan_progress(update_scan_id, update.get('progress', 0), update.get('current_host'))
elif update_type == 'host_discovered':
await send_host_discovered(update_scan_id, update.get('host'))
elif update_type == 'scan_completed':
await send_scan_completed(update_scan_id, {'hosts_found': update.get('hosts_found', 0)})
elif update_type == 'scan_failed':
await send_scan_failed(update_scan_id, update.get('error', 'Unknown error'))
# Create background task wrapper that uses a new database session
async def run_scan_task():
from app.database import SessionLocal
scan_db = SessionLocal()
try:
scan_service_bg = ScanService(scan_db)
await scan_service_bg.execute_scan(scan_id, config, progress_callback)
finally:
scan_db.close()
# Create and store the task for this scan
task = asyncio.create_task(run_scan_task())
scan_service.active_scans[scan_id] = task
logger.info(f"Started scan {scan_id} for {config.network_range}")
return ScanStartResponse(
scan_id=scan_id,
message=f"Scan started for network {config.network_range}",
status=ScanStatusEnum.PENDING
)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error(f"Error starting scan: {e}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to start scan")
@router.get("/{scan_id}/status", response_model=ScanStatusResponse)
def get_scan_status(scan_id: int, db: Session = Depends(get_db)):
"""
Get the status of a specific scan.
Args:
scan_id: Scan ID
db: Database session
Returns:
Scan status information
"""
scan_service = ScanService(db)
scan = scan_service.get_scan_status(scan_id)
if not scan:
raise HTTPException(status_code=404, detail=f"Scan {scan_id} not found")
# Calculate progress
progress = 0.0
if scan.status == ScanStatusEnum.COMPLETED.value:
progress = 1.0
elif scan.status == ScanStatusEnum.RUNNING.value:
# Estimate progress based on hosts found
# This is a rough estimate; real-time progress comes from WebSocket
if scan.hosts_found > 0:
progress = 0.5 # Host discovery done
return ScanStatusResponse(
id=scan.id,
started_at=scan.started_at,
completed_at=scan.completed_at,
scan_type=scan.scan_type,
network_range=scan.network_range,
status=ScanStatusEnum(scan.status),
hosts_found=scan.hosts_found,
ports_scanned=scan.ports_scanned,
error_message=scan.error_message,
progress=progress,
current_host=None,
estimated_completion=None
)
@router.get("", response_model=List[ScanResponse])
def list_scans(
limit: int = 50,
offset: int = 0,
db: Session = Depends(get_db)
):
"""
List recent scans.
Args:
limit: Maximum number of scans to return
offset: Number of scans to skip
db: Database session
Returns:
List of scans
"""
scan_service = ScanService(db)
scans = scan_service.list_scans(limit=limit, offset=offset)
return [
ScanResponse(
id=scan.id,
started_at=scan.started_at,
completed_at=scan.completed_at,
scan_type=scan.scan_type,
network_range=scan.network_range,
status=ScanStatusEnum(scan.status),
hosts_found=scan.hosts_found,
ports_scanned=scan.ports_scanned,
error_message=scan.error_message
)
for scan in scans
]
@router.delete("/{scan_id}/cancel")
def cancel_scan(scan_id: int, db: Session = Depends(get_db)):
"""
Cancel a running scan.
Args:
scan_id: Scan ID
db: Database session
Returns:
Success message
"""
scan_service = ScanService(db)
# Check if scan exists
scan = scan_service.get_scan_status(scan_id)
if not scan:
raise HTTPException(status_code=404, detail=f"Scan {scan_id} not found")
# Check if scan is running
if scan.status not in [ScanStatusEnum.PENDING.value, ScanStatusEnum.RUNNING.value]:
raise HTTPException(
status_code=400,
detail=f"Cannot cancel scan in status: {scan.status}"
)
# Attempt to cancel
success = scan_service.cancel_scan(scan_id)
if success:
return {"message": f"Scan {scan_id} cancelled successfully"}
else:
raise HTTPException(status_code=500, detail="Failed to cancel scan")

View File

@@ -0,0 +1,70 @@
"""Topology API endpoints."""
import logging
from fastapi import APIRouter, Depends, HTTPException, Query
from sqlalchemy.orm import Session
from app.database import get_db
from app.schemas import TopologyResponse
from app.services.topology_service import TopologyService
logger = logging.getLogger(__name__)
router = APIRouter()
@router.get("", response_model=TopologyResponse)
def get_network_topology(
include_offline: bool = Query(False, description="Include offline hosts"),
db: Session = Depends(get_db)
):
"""
Get network topology graph data.
Args:
include_offline: Whether to include offline hosts
db: Database session
Returns:
Topology data with nodes and edges
"""
try:
topology_service = TopologyService(db)
topology = topology_service.generate_topology(include_offline=include_offline)
logger.info(f"Generated topology with {len(topology.nodes)} nodes")
return topology
except Exception as e:
logger.error(f"Error generating topology: {e}", exc_info=True)
raise HTTPException(status_code=500, detail="Failed to generate topology")
@router.get("/neighbors/{host_id}")
def get_host_neighbors(host_id: int, db: Session = Depends(get_db)):
"""
Get neighboring hosts for a specific host.
Args:
host_id: Host ID
db: Database session
Returns:
List of neighboring hosts
"""
topology_service = TopologyService(db)
neighbors = topology_service.get_host_neighbors(host_id)
return {
'host_id': host_id,
'neighbors': [
{
'id': h.id,
'ip_address': h.ip_address,
'hostname': h.hostname,
'status': h.status
}
for h in neighbors
]
}

View File

@@ -0,0 +1,222 @@
"""WebSocket endpoint for real-time updates."""
import asyncio
import json
import logging
from typing import Set
from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from datetime import datetime
logger = logging.getLogger(__name__)
router = APIRouter()
class ConnectionManager:
"""Manager for WebSocket connections."""
def __init__(self):
"""Initialize connection manager."""
self.active_connections: Set[WebSocket] = set()
async def connect(self, websocket: WebSocket):
"""
Accept and register a new WebSocket connection.
Args:
websocket: WebSocket connection
"""
await websocket.accept()
self.active_connections.add(websocket)
logger.info(f"WebSocket connected. Total connections: {len(self.active_connections)}")
def disconnect(self, websocket: WebSocket):
"""
Remove a WebSocket connection.
Args:
websocket: WebSocket connection
"""
self.active_connections.discard(websocket)
logger.info(f"WebSocket disconnected. Total connections: {len(self.active_connections)}")
async def send_personal_message(self, message: dict, websocket: WebSocket):
"""
Send a message to a specific WebSocket.
Args:
message: Message to send
websocket: WebSocket connection
"""
try:
await websocket.send_json(message)
except Exception as e:
logger.error(f"Error sending message: {e}")
self.disconnect(websocket)
async def broadcast(self, message: dict):
"""
Broadcast a message to all connected WebSockets.
Args:
message: Message to broadcast
"""
disconnected = set()
for connection in self.active_connections:
try:
await connection.send_json(message)
except Exception as e:
logger.error(f"Error broadcasting to connection: {e}")
disconnected.add(connection)
# Clean up disconnected clients
for connection in disconnected:
self.disconnect(connection)
# Global connection manager instance
manager = ConnectionManager()
@router.websocket("")
async def websocket_endpoint(websocket: WebSocket):
"""
WebSocket endpoint for real-time scan updates.
Args:
websocket: WebSocket connection
"""
await manager.connect(websocket)
try:
# Send welcome message
await manager.send_personal_message({
'type': 'connected',
'message': 'Connected to network scanner',
'timestamp': datetime.utcnow().isoformat()
}, websocket)
# Keep connection alive and handle incoming messages
while True:
try:
# Receive messages from client
data = await websocket.receive_text()
# Parse and handle client messages
try:
message = json.loads(data)
await handle_client_message(message, websocket)
except json.JSONDecodeError:
await manager.send_personal_message({
'type': 'error',
'message': 'Invalid JSON format',
'timestamp': datetime.utcnow().isoformat()
}, websocket)
except WebSocketDisconnect:
break
except Exception as e:
logger.error(f"Error in WebSocket loop: {e}")
break
finally:
manager.disconnect(websocket)
async def handle_client_message(message: dict, websocket: WebSocket):
"""
Handle messages from client.
Args:
message: Client message
websocket: WebSocket connection
"""
message_type = message.get('type')
if message_type == 'ping':
# Respond to ping
await manager.send_personal_message({
'type': 'pong',
'timestamp': datetime.utcnow().isoformat()
}, websocket)
elif message_type == 'subscribe':
# Handle subscription requests
scan_id = message.get('scan_id')
if scan_id:
await manager.send_personal_message({
'type': 'subscribed',
'scan_id': scan_id,
'timestamp': datetime.utcnow().isoformat()
}, websocket)
else:
logger.warning(f"Unknown message type: {message_type}")
async def broadcast_scan_update(scan_id: int, update_type: str, data: dict):
"""
Broadcast scan update to all connected clients.
Args:
scan_id: Scan ID
update_type: Type of update
data: Update data
"""
message = {
'type': update_type,
'scan_id': scan_id,
'data': data,
'timestamp': datetime.utcnow().isoformat()
}
await manager.broadcast(message)
async def send_scan_progress(scan_id: int, progress: float, current_host: str = None):
"""
Send scan progress update.
Args:
scan_id: Scan ID
progress: Progress value (0.0 to 1.0)
current_host: Currently scanning host
"""
await broadcast_scan_update(scan_id, 'scan_progress', {
'progress': progress,
'current_host': current_host
})
async def send_host_discovered(scan_id: int, host_data: dict):
"""
Send host discovered notification.
Args:
scan_id: Scan ID
host_data: Host information
"""
await broadcast_scan_update(scan_id, 'host_discovered', host_data)
async def send_scan_completed(scan_id: int, summary: dict):
"""
Send scan completed notification.
Args:
scan_id: Scan ID
summary: Scan summary
"""
await broadcast_scan_update(scan_id, 'scan_completed', summary)
async def send_scan_failed(scan_id: int, error: str):
"""
Send scan failed notification.
Args:
scan_id: Scan ID
error: Error message
"""
await broadcast_scan_update(scan_id, 'scan_failed', {'error': error})