处理与CURL的TLSv1.2连接

时间:2017-10-13 12:51:52

标签: php ssl curl tls1.2

我正在尝试从其嵌入式Web服务器(自签名证书)自动配置x IP摄像机。因此,如果您尝试以正常方式(无脚本)通过浏览器连接到相机,则必须添加例外,工作正常。 我想自动执行此操作,我的所有脚本PHP都在Powershell CLI中运行。

我有以下PHP脚本:

<?php
include('C:\wamp64\bin\php\php7.0.10\run\Librairie\LIB_parse.php');
include('C:\wamp64\bin\php\php7.0.10\run\Librairie\LIB_http.php');
include('C:\wamp64\bin\php\php7.0.10\run\Librairie\LIB_resolve_addresses.php');

$TableauIP = fopen('C:\wamp64\bin\php\php7.0.10\run\x\Ipcamera.txt', 'r');
$count = 0;

while (($URLcamera = fgets($TableauIP, 4096)) !== false){
    $IP_unparsed = $URLcamera;
    $Ipcamera = return_between($IP_unparsed, "//", "/", EXCL);
    echo("Automatic configuration for : ".$Ipcamera."\n");
    echo("...............\n\n");
    echo("Downloading page : ".$IP_unparsed."\n\n");

    $web_page =http_get($IP_unparsed, $ref = "");
    echo "ERROR \n";
    var_dump($web_page['ERROR']);

    $head_section = return_between($string=$web_page['FILE'], $start="<head>", $end="</head>", $type=EXCL);
    $meta_tag_array = parse_array($head_section, $beg_tag="<meta", $close_tag=">");

    for($xx=0; $xx<count($meta_tag_array); $xx++){
        echo $meta_tag_array[$xx]."\n";

    }

    for($xx=0; $xx<count($meta_tag_array); $xx++){
        $meta_attribute = get_attribute($meta_tag_array[$xx], $attribute="http-equiv");
        if(strtolower($meta_attribute)=="refresh"){

            $new_page = return_between($meta_tag_array[$xx], $start="URL", $end=">", $type=EXCL);
            $new_page = trim(str_replace("", "", $new_page));
            $new_page = str_replace("=", "", $new_page);
            $new_page = str_replace("\"", "", $new_page);
            $new_page = resolve_address($new_page, $IP_unparsed);
        }
        break;
    }
    echo "HTML Head redirection detected<br>\n\n";
    echo "Redirect page = ".$new_page."\n";

    $web_page2 = http_get($new_page, $ref = "");
    //$web_page = http_get($IP_unparsed.'/login.cs', $ref = "");

    echo "FILE CONTENT \n";
    var_dump($web_page2['FILE']);

    echo "FILE ERROR \n";
    var_dump($web_page2['ERROR']);


    // for($xx=0; $xx<count($web_page); $xx++){
    //  echo($web_page[$xx]);
    // }

    // echo "ERROR \n";
    // var_dump($new_page['ERROR']);

    //*******************************

    // $web_page = file($new_page);

    // for($xx = 0; $xx < count($web_page); $xx++)
    // echo $web_page[$xx];

    //********************************

    // $file_handle = fopen($new_page, "r");

    // while (!feof($file_handle))
    //  {
    //  echo fgets($file_handle, 4096);
    //  }
    // fclose($file_handle);


    $count++;
}


?>

(我离开了评论,我尝试了不同的方式来显示网页) 如您所见,我在基本的Windows 7上使用引擎WampServer_x64。 我正在重定向到https://x.x.x.x/login.cs页面。

重要的部分是下载网页2.

这里是LIB_parse库(只是必要的行),在PHP函数中包装curl选项:

function http_get($target, $ref)
{
return http($target, $ref, $method="GET", $data_array="", EXCL_HEAD);
}


function http($target, $ref, $method, $data_array, $incl_head)
{
# Initialize PHP/CURL handle
$ch = curl_init();

# Prcess data, if presented
if(is_array($data_array))
    {
    # Convert data array into a query string (ie animal=dog&sport=baseball)
    foreach ($data_array as $key => $value)
        {
        if(strlen(trim($value))>0)
            $temp_string[] = $key . "=" . urlencode($value);
        else
            $temp_string[] = $key;
        }
    $query_string = join('&', $temp_string);
    }

# HEAD method configuration
if($method == HEAD)
    {
    curl_setopt($ch, CURLOPT_HEADER, TRUE);                // No http head
    curl_setopt($ch, CURLOPT_NOBODY, TRUE);                // Return body
    }
else
    {
    # GET method configuration
    if($method == GET)
        {
        if(isset($query_string))
            $target = $target . "?" . $query_string;
        curl_setopt ($ch, CURLOPT_HTTPGET, TRUE);
        curl_setopt ($ch, CURLOPT_POST, FALSE);
        }
    # POST method configuration
    if($method == POST)
        {
        if(isset($query_string))
            curl_setopt ($ch, CURLOPT_POSTFIELDS, $query_string);
        curl_setopt ($ch, CURLOPT_POST, TRUE);
        curl_setopt ($ch, CURLOPT_HTTPGET, FALSE);
        }
    curl_setopt($ch, CURLOPT_HEADER, $incl_head);   // Include head as needed
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);        // Return body
    }

curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE_FILE);   // Cookie management.
curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE_FILE);
curl_setopt($ch, CURLOPT_TIMEOUT, CURL_TIMEOUT);    // Timeout
curl_setopt($ch, CURLOPT_USERAGENT, WEBBOT_NAME);   // Webbot name
curl_setopt($ch, CURLOPT_URL, $target);             // Target site
curl_setopt($ch, CURLOPT_REFERER, $ref);            // Referer value
curl_setopt($ch, CURLOPT_VERBOSE, FALSE);           // Minimize logs

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // No certificate
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);     // Follow redirects
curl_setopt($ch, CURLOPT_MAXREDIRS, 4);             // Limit redirections to four
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);     // Return in string

# Create return array
$return_array['FILE']   = curl_exec($ch);
$return_array['STATUS'] = curl_getinfo($ch);
$return_array['ERROR']  = curl_error($ch);

# Close PHP/CURL handle
curl_close($ch);

# Return results
return $return_array;
}

我不知道如何使用cURL处理TLS连接。我已经用不同的东西试了几个小时..我有这个问题:加密警报: whireshark capture TCP and TLS exchange

我已将此行添加到原始库中:

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//curl_setopt($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);

我无法访问该网页。 显然,SSL版本是1.0.2h。 我尝试了很多不同的东西..有许多不同的错误类型,但总是围绕SSL证书的东西..

我没有更多想法在哪里看.. 如果你们能给我一条路!那会很不错

0 个答案:

没有答案