首页 文章

尽管有防火墙规则,但无法连接到Google Cloud Compute实例上的端口80

提问于
浏览
0

总之,尽管我设置了允许tcp:80的防火墙规则,但是我的GCE实例(在“默认”网络上)不接受与端口80的连接 . 看来只有端口22在我的实例上打开 . 我可以ping它,但不能在64跳以下追踪到它 .

接下来是我的调查让我得出了这些结论 .

gcloud beta compute firewall-rules list

NAME                    NETWORK  DIRECTION  PRIORITY  ALLOW                         DENY
default-allow-http      default  INGRESS    1000      tcp:80
default-allow-https     default  INGRESS    1000      tcp:443
default-allow-icmp      default  INGRESS    65534     icmp
default-allow-internal  default  INGRESS    65534     tcp:0-65535,udp:0-65535,icmp
default-allow-rdp       default  INGRESS    65534     tcp:3389
default-allow-ssh       default  INGRESS    65534     tcp:22
temp                    default  INGRESS    1000      tcp:8888


gcloud compute instances list
NAME   ZONE        MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
ssrf3  us-west1-c  f1-micro      true         10.138.0.4   35.197.33.182  RUNNING


gcloud compute instances describe ssrf3
...
name: ssrf3
networkInterfaces:
- accessConfigs:
  - kind: compute#accessConfig
    name: external-nat
    natIP: 35.197.33.182
    type: ONE_TO_ONE_NAT
  kind: compute#networkInterface
  name: nic0
  network: https://www.googleapis.com/compute/v1/projects/hack-170416/global/networks/default
  networkIP: 10.138.0.4
  subnetwork: https://www.googleapis.com/compute/v1/projects/hack-170416/regions/us-west1/subnetworks/default
...
tags:
  fingerprint: 6smc4R4d39I=
  items:
  - http-server
  - https-server

我ssh到35.197.33.182(这是ssrf3实例)并运行:

sudo nc -l -vv -p 80

在我的本地机器上,我运行:

nc 35.197.33.182 80 -vv
hey

但没有任何反应 . 所以我试着ping主机 . 看起来很 Health :

ping 35.197.33.182 
PING 35.197.33.182 (35.197.33.182): 56 data bytes
64 bytes from 35.197.33.182: icmp_seq=0 ttl=57 time=69.172 ms
64 bytes from 35.197.33.182: icmp_seq=1 ttl=57 time=21.509 ms

Traceroute在64跳之后退出,但没有到达35.197.33.182目的地 .

所以我用nmap检查哪些端口是打开的:

nmap 35.197.33.182

Starting Nmap 7.12 ( https://nmap.org ) at 2017-06-18 16:39 PDT
Note: Host seems down. If it is really up, but blocking our ping probes, try -Pn
Nmap done: 1 IP address (0 hosts up) scanned in 3.06 seconds



nmap 35.197.33.182 -Pn

Starting Nmap 7.12 ( https://nmap.org ) at 2017-06-18 16:39 PDT
Nmap scan report for 182.33.197.35.bc.googleusercontent.com (35.197.33.182)
Host is up (0.022s latency).
Not shown: 999 filtered ports
PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 6.84 seconds

......即使我在35.197.33.182上运行 nc -l -p 80 .

2 回答

  • 1

    确保VM级防火墙没有干预 . 例如,与所有其他默认图像相比,Container-Optimized OS有点特殊:

    默认情况下,容器优化的OS主机防火墙仅允许传出连接,并仅通过SSH服务接受传入连接 . 要接受Container优化的OS实例上的传入连接,您必须打开服务正在侦听的端口 .

    https://cloud.google.com/container-optimized-os/docs/how-to/firewall

  • 0

    快速浏览一下,您的设置似乎是正确的 .

    • 您已为默认网络中的所有实例允许INGRESS tcp:80 .

    • 您的VM位于默认网络上 .

    由于不幸使用SDN,虚拟网络和一大堆中间网络基础设施,因此当您在 Cloud 提供商上运行虚拟机时,Traceroute不会给出良好的指示 .

    我注意到的一件事是你的实例有2个标签 http-serverhttps-server . 这些可能被某些其他防火墙规则使用,这些规则可能以某种方式阻止到VM的tcp:80端口的流量 .

    您的设置中还有其他变量,如果需要,我很乐意进行调试 .

    基于标记的防火墙规则

    您可以尝试基于标记的防火墙规则,该规则仅将防火墙规则应用于具有指定目标标记的实例 .

    网络使用网络标记来标识哪些实例受某些防火墙规则和网络路由的约束 . 例如,如果您有多个为大型网站提供服务的VM实例,请使用共享的单词或术语标记这些实例,然后使用该标记应用允许HTTP访问这些实例的防火墙规则 . 标签也会反映在元数据服务器中,因此您可以将它们用于在您的实例上运行的应用程序 . 创建防火墙规则时,可以提供sourceRanges或sourceTags,但不能同时提供两者 .

    # Add a new tag based firewall rule to allow ingress tcp:80
    gcloud compute firewall-rules create rule-allow-tcp-80 --source-ranges 0.0.0.0/0 --target-tags allow-tcp-80 --allow tcp:80
    
    # Add the allow-tcp-80 target tag to the VM ssrf3
    gcloud compute instances add-tags ssrf3 --tags allow-tcp-80
    

    可能需要几秒钟到几分钟才能使更改生效 .

    NOTE: 由于您已将外部IP连接到Internet,因此请根据在这些端口上运行的应用程序的需要,相应地限制访问 .

相关问题