[PHP]เรียกหน้าเว็บ โดยปลอม header ทำไมเป็นแบบนี้ครับ

เริ่มโดย Hortensia, 17 กรกฎาคม 2011, 15:58:54

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

Hortensia

[php]เรียกหน้าเว็บ โดยปลอม header ทำไมเป็นแบบนี้ครับ

class เรียกหน้าเว็บ

<?php

class me_class {

public function display_array($arr){
echo '<pre>';
print_r($arr);
echo '</pre>';
}

public function request_file($url$host){
for ($i 1$i <= 3$i++){
$fake_header $this->fake_header($host);
$req = @file_get_contents($urlnull$fake_header);
if ($req !== FALSE){
break;
}
sleep(1);
}
if ($req === FALSE){
return false;
}else{
return $req;
}
}

public function fake_header($host){
$header "Host: {$host}\n";
$header .= "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0\n";
$header .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\n";
$header .= "Accept-Language: en-us,en;q=0.5\n";
$header .= "Accept-Encoding: gzip, deflate\n";
$header .= "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n";
$header .= "Connection: keep-alive\n";
$make_header = array("http" => array("method" => "GET""header" => $header));
$new_header stream_context_create($make_header);
return $new_header;
}

}

?>



ไฟล์เรียกหน้าเว็บ

<?php

require_once 'me-class.php';
$me = new me_class();

/* $header = $me->fake_header("www.google.com");
var_dump($header); */

$keyword urlencode("loans");
$keyword2 urlencode("ipad");
$url "http://www.google.com/search?hl=en&biw=1280&bih=307&gl=us&q={$keyword}&aq=f&aqi=&aql=&oq=";
$url2 "http://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords={$keyword2}&x=0&y=0";
$host "www.google.com";
$host2 "www.amazon.com";
$google $me->request_file($url2$host2);
echo 
$google;

?>



ผลลัพธ์ครับ

zenithsoul

view ดู encode ของเพจแล้วหรือยังครับว่าใส่แท็ก encode ภาษาลงไปด้วยหรือป่าว

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

Hortensia

อ้างถึงจาก: zenithsoul ใน 17 กรกฎาคม 2011, 16:03:06
view ดู encode ของเพจแล้วหรือยังครับว่าใส่แท็ก encode ภาษาลงไปด้วยหรือป่าว

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
ใส่แล้วครับ

header("Content-Type: text/html; charset=utf-8");

picharnan

อ้างถึงจาก: zenithsoul ใน 17 กรกฎาคม 2011, 16:03:06
view ดู encode ของเพจแล้วหรือยังครับว่าใส่แท็ก encode ภาษาลงไปด้วยหรือป่าว

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

ตามนั้นครับ  แล้วก็อย่าลืม convert ไฟล์เป็น UTF-8 ด้วยนะครับ  ง่ายๆก็ notepad++ แล้วไปที่ Encoding > Encode in UTF-8 without BOM

picharnan

ผมว่าลองหันไปใช้ CURL ดูอาจจะง่ายกว่าก็ได้นะครับ

curl_setopt($conn, CURLOPT_REFERER, 'foo.com');

:P :P


ohmohm

ลอง curl แบบ command line ( มันแก้ง่ายดี ) หรือ telnet port 80 เข้าไปเลยซิ

TG_Min

echo $google;

เป็น

echo iconv("UTF-8", "TIS-620",$google);

ลองดูครับ เปลี่ยนจาก UTF-8 ต้นฉบับ เป็น TIS-620 เพื่อแสดงผล
หรือสลับดู
[direct=https://www.kasetorganic.com/shop/]Shop[/direct] [direct=https://www.dek-d.com/member/profile/beautisora/blog]บล็อกของ Sora[/direct] [direct=https://krabi.vwander.com]เที่ยวกระบี่ไปได้ทุกวัน[/direct]

ohmohm

กลับมาดูอีกที สังเกตว่ามี
Accept-Encoding: gzip, deflate

สิ่งที่ตอบกลับมาแล้วเราอ่านไม่รู้เรื่อง มันอาจเป็น html ที่ถูกบีบย่อไว้ ผมลองใช้ nc amazon.com 80 จากนั้นแล้ว paste...

GET / HTTP/1.1
HOST: [url=http://www.amazon.com]www.amazon.com[/url]
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive


( จะใช้ telnet แทน nc ก็น่าจะได้ )
ปรากฎว่า อ่านรู้เรื่อง คือไม่ถูกบีบ
เอา Accept-Encoding ออก น่าจะหายครับ

มาคิดอีกทีหนึ่ง ถ้าได้ใช้ Accept-Encoding ก็น่าจะประหยัด bandwidth ได้ แต่เราต้องอ่าน  Content-Encoding ที่ web server ตอบกลับมา และต้องคลายการบีบย่อได้ด้วย
http://tools.ietf.org/html/rfc2616#section-3.5
gzip ใช้ RFC 1952 ซึ่งน่าจะใช้ gzdecode() ได้ http://www.php.net/manual/en/function.gzdecode.php

ทดลองเมื่อกี้
ดู header ที่ตอบกลับมาซิครับ ชัดเจนมาก

HTTP/1.1 200 OK
...
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Type: text/html; charset=ISO-8859-1
...


แนะนำใช้ curl ดีกว่า มันคลาย gzip เองได้ด้วย