search
HomeBackend DevelopmentPHP Tutorial 请问php怎么使用自己构造的IP包头

请教php如何使用自己构造的IP包头

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php //数据
$dns_id = "\x80\xc1";
$dns_flags = "\x01\x00";
$dns_count_queries = "\x00\x01";
$dns_count_answers = "\x00\x00";
$dns_count_auth_rr = "\x00\x00";
$dns_count_add_rr = "\x00\x00";
$dns_qry_name = "\x02\x69\x70\x02\x63\x6e\x00";
$dns_qry_type = "\x00\x01";
$dns_qry_class = "\x00\x01";
$dns = $dns_id . $dns_flags . $dns_count_queries . $dns_count_answers . $dns_count_auth_rr . $dns_count_add_rr . $dns_qry_name . $dns_qry_type . $dns_qry_class;

//IP头
$ip_version = "\x45"; //4位版本
$ip_hdr_len = ""; // 4位首部长度
$ip_dsfield = "\x00"; //8位服务类型
$ip_len = pack('n*',28 + strlen($dns)); //16位总长度
$ip_id = "\x55\xf0"; //16位标识
$ip_flags = "\x00"; //3位标识
$ip_frag_offset = "\x00"; // 13位偏移
$ip_ttl = "\x80"; //8位生存时间
$ip_proto = "\x11"; //8位协议
$ip_src = ipto16("192.168.1.100"); //32位源IP
$ip_dst = ipto16("8.8.4.4"); //32位目的IP
$ip_checksum = ipchecksum($ip_version,$ip_hdr_len,$ip_dsfield,$ip_len,$ip_id,$ip_flags,$ip_frag_offset,$ip_ttl,$ip_proto,$ip_src,$ip_dst); //16位首部效验和
$ip_header = $ip_version . $ip_hdr_len . $ip_dsfield . $ip_len . $ip_id . $ip_flags . $ip_frag_offset . $ip_ttl . $ip_proto . $ip_checksum . $ip_src . $ip_dst;


//UDP头
$udp_srcport = "\xfc\x39"; //16位源端口
$udp_dstport = "\x00\x35"; //16位目的端口
$udp_length = pack('n*',8 + strlen($dns)); //16位UDP长度
$udp_checksum = udpchecksum($ip_src,$ip_dst,$ip_proto,$udp_length,$udp_srcport,$udp_dstport,$dns); //16位UDP效验和
$udp = $udp_srcport . $udp_dstport . $udp_length . $udp_checksum;


//$buf = $ip_header . $udp . $dns;
$buf = $udp . $dns;

function send($host,$port,$buf,$dns_flags)
{
//    $socket = socket_create(AF_INET, SOCK_PACKET, getprotobyname("ip"));
    $socket = socket_create(AF_INET, SOCK_RAM,1);
    $timeout = array('sec'=>1,'usec'=>500000);
//    socket_set_option($socket,IPPROTO_IP,IP_HDRINCL,SO_RCVTIMEO,$timeout);
    socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);
//    while(1)
        socket_sendto($socket, $buf, strlen($buf), 0, $host, $port);
}

function udpchecksum($ip_src,$ip_dst,$ip_proto,$udp_length,$udp_srcport,$udp_dstport,$dns)
{
    $data = $ip_src . $ip_dst . "\x00" . $ip_proto . $udp_length . $udp_srcport . $udp_dstport . $udp_length . "\x00\x00" . $dns;
    if (strlen($data)%2)
    $data .= "\x00";
    $bit = unpack('n*', $data);
    $sum = array_sum($bit);
    while ($sum >> 16)
        $sum = ($sum >> 16) + ($sum & 0xffff);
    return pack('n*', ~$sum);
}

function ipchecksum($ip_version,$ip_hdr_len,$ip_dsfield,$ip_len,$ip_id,$ip_flags,$ip_frag_offset,$ip_ttl,$ip_proto,$ip_src,$ip_dst)
{
    $data = $ip_version . $ip_hdr_len . $ip_dsfield . $ip_len . $ip_id . $ip_flags . $ip_frag_offset . $ip_ttl . $ip_proto . "\x00\x00" . $ip_src . $ip_dst;
    $bit = unpack('n*', $data);
    $sum = array_sum($bit);
    while ($sum >> 16)
        $sum = ($sum >> 16) + ($sum & 0xffff);
    return pack('n*', ~$sum);
}

function ipto16($ip)
{
    $tmp = explode(".",$ip);
    $ip = "";
    foreach($tmp as $a => $b)
    {
        $tmp = dechex($b);
        if (strlen($tmp)==1)
            $tmp = "0" . $tmp;
        $ip .= $tmp;
    }
    $ip = pack("h*",$ip);
    return $ip;
}

send("8.8.4.4",53,$buf,$dns_flags);







这是个查询向指定dns服务器查询指定域名IP信息的程序,用Wireshark抓包测试可用,但是现在没有用自己的网络层,也就是IP头,我要用自己构造的IP头,

$buf = $ip_header . $udp . $dns;
//$buf = $udp . $dns;
改成
$buf = $ip_header . $udp . $dns;
//$buf = $udp . $dns;
结果还是不行,应该是send那函数的问题,请问下应该怎么修改

------解决方案--------------------
不知道你在干什么?利用 sock 包的肯定是在用户层了
封装 ip 应该在下一层
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.