【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志

简介: 【Azure 应用服务】App Service For Linux 如何在 Web 应用实例上住抓取网络日志

问题描述

在App Service For Windows的环境中,我们可以通过ArmClient 工具发送POST请求在Web应用的实例中抓取网络日志,但是在App Service For Linux的环境中如何抓取网络包呢?

抓取Windows的网络包可参考博文:【应用服务 App Service】App Service中抓取网络日志

 

问题解决

通过SSH方式登录到Linux实例,使用tcpdump的工具抓取网络包, 通过IP地址和端口443来进行过滤,生成的网络包写入到tmp目录下的 appnetworktrace.pcap  文件。 命令如下:

tcpdump -i any host <your app service inbound ip address> and tcp port 443 -n -v -s 0 -w /tmp/appnetworktrace.pcap  

  • 如果在登录SSH的时候出现 SSH CONNECTION CLOSE - Error: Timed out while waiting for handshakeError: connect ECONNREFUSED 错误,则是因为使用自定义容器的方式发布的Docker镜像中没有启动SSH。只需要根据官方文档启动即可。

启用 SSH

SSH 实现容器和客户端之间的安全通信。 为了使自定义容器支持 SSH,你必须将其添加到 Docker 映像本身。

1) 将 sshd_config 文件 添加到存储库,如以下示例中所示。

Port            2222
ListenAddress       0.0.0.0
LoginGraceTime      180
X11Forwarding       yes
Ciphers aes128-cbc,3des-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
MACs hmac-sha1,hmac-sha1-96
StrictModes         yes
SyslogFacility      DAEMON
PasswordAuthentication  yes
PermitEmptyPasswords    no
PermitRootLogin     yes
Subsystem sftp internal-sftp

备注: 此文件配置 OpenSSH 并且必须包括以下项:

  • Port 必须设置为 2222。
  • Ciphers 必须至少包含此列表中的一项:aes128-cbc,3des-cbc,aes256-cbc
  • MACs 必须至少包含此列表中的一项:hmac-sha1,hmac-sha1-96

 

2) 向存储库添加 ssh_setup 脚本文件,以使用 ssh-keygen 创建 SSH 密钥。

#!/bin/sh
if [ ! -f "/etc/ssh/ssh_host_rsa_key" ]; then
    # generate fresh rsa key
    ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
fi
if [ ! -f "/etc/ssh/ssh_host_dsa_key" ]; then
    # generate fresh dsa key
    ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
fi
if [ ! -f "/etc/ssh/ssh_host_ecdsa_key" ]; then
    # generate fresh ecdsa key
    ssh-keygen -f /etc/ssh/ssh_host_ecdsa_key -N '' -t dsa
fi
if [ ! -f "/etc/ssh/ssh_host_ed25519_key" ]; then
    # generate fresh ecdsa key
    ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N '' -t dsa
fi
#prepare run dir
    if [ ! -d "/var/run/sshd" ]; then
    mkdir -p /var/run/sshd
fi

 

3) 在 Dockerfile 中,添加以下命令:

# Install OpenSSH and set the password for root to "Docker!". In this example, "apk add" is the install instruction for an Alpine Linux-based image.
RUN apk add openssh \
     && echo "root:Docker!" | chpasswd 
# Copy the sshd_config file to the /etc/ssh/ directory
COPY sshd_config /etc/ssh/
# Copy and configure the ssh_setup file
RUN mkdir -p /tmp
COPY ssh_setup.sh /tmp
RUN chmod +x /tmp/ssh_setup.sh \
    && (sleep 1;/tmp/ssh_setup.sh 2>&1 > /dev/null)
# Open port 2222 for SSH access
EXPOSE 80 2222

 

4) 在容器的启动脚本中启动 SSH 服务器。

/usr/sbin/sshd

 

 

参考资料

为 Azure 应用服务配置自定义容器, 启动SSH: https://docs.microsoft.com/zh-cn/azure/app-service/configure-custom-container?pivots=container-linux#enable-ssh

 

相关文章
|
20天前
|
安全 前端开发 API
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
【Azure 应用服务】Azure Web App 服务默认支持一些 Weak TLS Ciphers Suite,是否有办法自定义修改呢?
|
20天前
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
【Azure 应用服务】Web App Service 中的 应用程序配置(Application Setting) 怎么获取key vault中的值
|
20天前
|
应用服务中间件 Linux 网络安全
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
【Azure 应用服务】App Service for Linux 环境中为Tomcat页面修改默认的Azure 404页面
|
20天前
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
【Azure Web Job】Azure Web Job执行Powershell脚本报错 The term 'Select-AzContext' is not recognized as the name
|
20天前
|
存储 Linux 开发工具
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
【Azure App Service】本地Git部署Python Flask应用上云(Azure App Service For Linux)关键错误
|
20天前
|
存储 Linux 网络安全
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
【Azure App Service】.NET代码实验App Service应用中获取TLS/SSL 证书 (App Service Linux/Linux Container)
|
20天前
|
JavaScript Linux
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
【Azure App Service for Linux】NodeJS镜像应用启动失败,遇见 RangeError: Incorrect locale information provided
|
20天前
|
Shell PHP Windows
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
【Azure App Service】Web Job 报错 UNC paths are not supported. Defaulting to Windows directory.
|
20天前
|
Kubernetes Linux Docker
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
【Azure 应用服务】使用Docker Compose创建App Service遇见"Linux Version is too long. It cannot be more than 4000 characters"错误
|
Oracle 关系型数据库 MySQL
linux service命令常见使用方法
service命令,顾名思义,就是用于管理Linux操作系统中服务的命令。 1. 声明:这个命令不是在所有的linux发行版本中都有。主要是在redhat、fedora、mandriva和centos中。
1241 0