我已经写了一个 bash 脚本来按照此ServerFault link自动生成IIS7证书。

我想通过cURL自动将代码签名请求(CSR)发送到内部Microsoft证书颁发机构(MS CA),以下代码很有希望,并且已成功将CSR提交给MS CA:

$ curl -k -u '<Domain>\<Username>':<Password> --ntlm
'https://<InternalMSCA>/certsrv/certfnsh.asp'
-H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
-H 'Accept-Encoding: gzip, deflate' -H 'Accept-Language: en-US,en;q=0.5'
-H 'Connection: keep-alive'
-H 'Host: <InternalMSCA>'
-H 'Referer: https://<InternalMSCA>/certsrv/certrqxt.asp'
-H 'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'
-H 'Content-Type: application/x-www-form-urlencoded'
--data 'Mode=newreq&CertRequest=-----BEGIN+CERTIFICATE+REQUEST-----%0D%0AMIIDBjCCAe4CAQAwaDELMAkGA1UEBhMCQVUxDDAKBgNVBAgTA05TVzEPMA0GA1UE%0D%0ABxMGU3lkbmV5MQwwCgYDVQQKEwNZdW0xDjAMBgNVBAsTBVl1bUlTMRwwGgYDVQQD%0D%0AExN0ZXN0LmF1LmludC50Z3IubmV0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB%0D%0ACgKCAQEAygZvKhfs0mw4tModevTxOIz7eYYM%2B1axNv8FqoNyKr7xtqSbOMiNzf8R3rZ%0D%0A4cTcu5nv7oC7GHPMhnF7AdsO4XexwnKfnCkofECGkO6O4oTmRfUPLa38nV1%2BmytB%0D%0AlrQAl272jQdM9LSxTYW0OR9qO4mjAH1tvLF3IcC1OKOh6UNubdRFfE7dEXWnk%2BSF%0D%0AM8tgl0t3SFsRxrZL3vkgL%2B%2FEmvdOKXeoIey%2F7UMNeWRcwTkS1mw30HjvitJdQGZi%0D%0AgYJ6ldXrrITVKe9QXvVTxSl9NfzPHYp4yf%2FZvAJQmGLZ16aQo0PBeEfjkgkrcY5j%0D%0AMnVI2Q8yC%2BW9Bg%3D%3D%0D%0A-----END+CERTIFICATE+REQUEST-----&CertAttrib=CertificateTemplate%3A*WebServer%0D%0AUserAgent%3AMozilla%2F5.0+%28Windows+NT+6.3%3B+WOW64%3B+Trident%2F7.0%3B+rv%3A11.0%29+like+Gecko%0D%0A&FriendlyType=Saved-Request+Certificate+%287%2F7%2F2015%2C+3%3A46%3A39+PM%29&ThumbPrint=&TargetStoreFlags=0&SaveCert=yes'
| firefox "data:text/html;base64,$(base64 -w 0 <&0)"

我有兴趣在修改后重播此请求:
  • 解码--data(确定)
  • 修改--data(确定)
  • 重新编码...(不正确)

  • 编码方式:



    解码后:
     Mode=newreq&CertRequest=-----BEGIN CERTIFICATE REQUEST-----
     MIIDBjCCAe4CAQAwaDELMAkGA1UEBhMCQVUxDDAKBgNVBAgTA05TVzEPMA0GA1UE
     BxMGU3lkbmV5MQwwCgYDVQQKEwNZdW0xDjAMBgNVBAsTBVl1bUlTMRwwGgYDVQQD
     ExN0ZXN0LmF1LmludC50Z3IubmV0MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
     CgKCAQEAygZvKhfs0mw4tModevTxOIz7eYYM+1axNv8FqoNyKr7xtqSbOMiNzf8R3rZ
     4cTcu5nv7oC7GHPMhnF7AdsO4XexwnKfnCkofECGkO6O4oTmRfUPLa38nV1+mytB
     lrQAl272jQdM9LSxTYW0OR9qO4mjAH1tvLF3IcC1OKOh6UNubdRFfE7dEXWnk+SF
     M8tgl0t3SFsRxrZL3vkgL+/EmvdOKXeoIey/7UMNeWRcwTkS1mw30HjvitJdQGZi
     gYJ6ldXrrITVKe9QXvVTxSl9NfzPHYp4yf/ZvAJQmGLZ16aQo0PBeEfjkgkrcY5j
     MnVI2Q8yC+W9Bg==
     -----END CERTIFICATE REQUEST-----&CertAttrib=CertificateTemplate:*WebServer
     UserAgent:Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0)
     like Gecko &FriendlyType=Saved-Request Certificate (7/7/2015, 3:46:39
     PM)&ThumbPrint=&TargetStoreFlags=0&SaveCert=yes
    

    重新编码:(URLEncode1URLEncode2URLEncode3):



    上面链接的三个网站(重新编码)都无法正确重新编码。
    棘手的部分是,不应对“ = ”和“”进行编码。
    URL Encode Simple:
            CR LF           %0D%0A (Not %)
            Space           + (Not %20)
            -               - (Not %2D)
            &               & (Not %26)
            =               = (Not %3D)
            + (in CSR)      %2B
            \ (in CSR)      %2F
            (               %28
            )               %29
    

    在这个示例中,我可以特别是使用sed,但是我想知道是否有一种方法可以知道服务器期望的编码,并自动使用正确的字符集进行编码。这可能吗 ?

    最佳答案

    我在Linux上通过bash和curl解决了问题:

    #!/bin/sh
    
    # tested on SUSE Linux 12 SP1
    
    # $1 - CN Object name
    # $2 - username
    # $3 - password
    
    MSCA='HOSTNAME'  # Internal Microsoft Certification Authority
    Username=$2
    Password=$3
    
    function show_usage()
    {
        echo "Scrip for retrive certificate from MS SubCA"
        echo "Usage: $0 <CN> [domain\\\\username] [password]"
        echo " "
        echo "Example: $0 example.com workgroup\\\\foo bar"
        exit 0
    }
    
    if [ -z "$1" ]
    then
        show_usage
        exit 0
    fi
    
    if [ -z "$2" ]
    then
        Username="workgroup\\foo"
        Password="bar"
    fi
    
    
    echo -e "\e[32m1. Generate private key...\e[0m"
    openssl req -new -nodes -out $1.pem -keyout $1.key -subj "/C=RU/ST=State/L=City/O=Org/CN=$1/[email protected]"
    CERT=`cat $1.pem | tr -d '\n\r'`
    DATA="Mode=newreq&CertRequest=${CERT}&C&TargetStoreFlags=0&SaveCert=yes"
    CERT=`echo ${CERT} | sed 's/+/%2B/g'`
    CERT=`echo ${CERT} | tr -s ' ' '+'`
    CERTATTRIB="CertificateTemplate:Server%0D%0A"
    
    echo -e "\e[32m2. Request cert...\e[0m"
    OUTPUTLINK=`curl -k -u "${Username}":${Password} --ntlm \
    "https://${MSCA}/certsrv/certfnsh.asp" \
    -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
    -H 'Accept-Encoding: gzip, deflate' \
    -H 'Accept-Language: en-US,en;q=0.5' \
    -H 'Connection: keep-alive' \
    -H "Host: ${MSCA}" \
    -H "Referer: https://${MSCA}/certsrv/certrqxt.asp" \
    -H 'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko' \
    -H 'Content-Type: application/x-www-form-urlencoded' \
    --data "Mode=newreq&CertRequest=${CERT}&CertAttrib=${CERTATTRIB}&TargetStoreFlags=0&SaveCert=yes&ThumbPrint=" | grep -A 1 'function handleGetCert() {' | tail -n 1 | cut -d '"' -f 2`
    CERTLINK="https://${MSCA}/certsrv/${OUTPUTLINK}"
    
    echo -e "\e[32m3. Retrive cert: $CERTLINK\e[0m"
    curl -k -u "${Username}":${Password} --ntlm $CERTLINK \
    -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' \
    -H 'Accept-Encoding: gzip, deflate' \
    -H 'Accept-Language: en-US,en;q=0.5' \
    -H 'Connection: keep-alive' \
    -H "Host: ${MSCA}" \
    -H "Referer: https://${MSCA}/certsrv/certrqxt.asp" \
    -H 'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko' \
    -H 'Content-Type: application/x-www-form-urlencoded' > $1.crt
    
    echo -e "\e[32m4. Verifying cert for $1\e[0m"
    openssl verify -verbose $1.crt
    if [ "0" -eq "$?" ] ;
        then
            echo -e "\e[32mWell done. Have a nice day.\e[0m"
            exit 0
        else
            echo -e "\e[31;47mError code: $?. Stopping.\e[0m"
            exit 1
    fi
    

    关于iis - 将Base64 CSR提交到Microsoft CA(通过cURL),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31283476/

    10-12 17:38