shell script that uses the openssl command to retrieve the certificate of an external site through a proxy
#!/bin/bash
# Set the proxy server details
PROXY_HOST="proxy.example.com"
PROXY_PORT="8080"
PROXY_USER="proxy_username"
PROXY_PASS="proxy_password"
# Set the URL of the site to retrieve the certificate for
SITE_URL="www.example.com"
# Set the path to the output file for the certificate
CERT_FILE="example.com.crt"
# Set the OpenSSL configuration file for the proxy
PROXY_CONFIG=$(cat << EOF
[proxy]
http_proxy = http://$PROXY_USER:$PROXY_PASS@$PROXY_HOST:$PROXY_PORT
https_proxy = https://$PROXY_USER:$PROXY_PASS@$PROXY_HOST:$PROXY_PORT
EOF
)
# Create a temporary OpenSSL configuration file for the proxy
echo "$PROXY_CONFIG" > proxy.conf
# Use the OpenSSL command to retrieve the certificate for the site through the proxy
openssl s_client -connect $SITE_URL -servername $SITE_URL -proxy $PROXY_HOST:$PROXY_PORT -proxy_type http -showcerts </dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $CERT_FILE
# Remove the temporary OpenSSL configuration file for the proxy
rm proxy.conf
# Print a message indicating the location of the output file
echo "Certificate saved to $CERT_FILE"
Replace "proxy.example.com" with the hostname or IP address of your proxy server, "8080" with the port number of your proxy server, "proxy_username" and "proxy_password" with your proxy authentication credentials, and "www.example.com" with the hostname of the site you want to retrieve the certificate for.
The script uses the openssl command to connect to the site through the proxy and retrieve the certificate, then saves it to the file specified by the "CERT_FILE" variable.
The script also creates a temporary OpenSSL configuration file for the proxy, which specifies the proxy server details and is passed to the openssl command using the "-proxy" option. After the command has run, the temporary file is removed.
To run the script, save it as a file with a .sh extension (e.g. get-cert-from-proxy.sh), make it executable with the command "chmod +x get-cert-from-proxy.sh", then run it with the command "./get-cert-from-proxy.sh".
Comments
Post a Comment