Что нового

[Сеть, интернет] WinPcap - нужна помощь с PcapSendPacket

MeGaPoLiS

Новичок
Сообщения
2
Репутация
1
Всем привет.
Никак не пойму как отправлять пакеты при помощи _PcapSendPacket. Есть игра, надо что-бы автоит отправлял пакеты пойманные к примеру через WPE. Как это сделать? Не помешал бы простой примерчик посыла пакета. Вот скрипт который я нарыл на одном немецком форуме по ботоводству в автоите:

Код:
#include <Array.au3>
#include <Winpcap.au3>
$port = 4013
$nosip = "79.110.84.77"

$filter = "tcp port " & $port

$winpcap = _PcapSetup() ; initialize winpcap
If ($winpcap = -1) Then
    MsgBox(16, "Pcap error !", "WinPcap not found !")
    Exit
EndIf

$pcap_devices = _PcapGetDeviceList() ; get devices list
If ($pcap_devices = -1) Then
    MsgBox(16, "Pcap error !", _PcapGetLastError())
    Exit
EndIf

$int = $pcap_devices[2][0]

$pcap = _PcapStartCapture($int, $filter, 0);start capture
If ($pcap = -1) Then
    MsgBox(16, "Pcap error !", _PcapGetLastError())
EndIf
Local $counterabc = 1

$in = InputBox("", "")
                senden ($in)

;While 1 ;die schleife hab ich mal rausgemacht (kannst du auch wieder reinmachen aber ich würde ne möglichkeit zum abschalten einabauen)
    If IsPtr($pcap) Then
        $time0 = TimerInit()
        While (TimerDiff($time0) < 500) ; Retrieve packets from queue for maximum 500ms before returning to main loop, not to "hang" the window for user
            $packet = _PcapGetPacket($pcap)
            ;If IsInt($packet) Then ExitLoop ;das is doch dämlich, die Zeile bedeutet dass wenn er nichts empfangen hat die schleife verlässt.
            ;deshalb lieber so:
            If Not IsInt($packet) Then
            If $counterabc <> 5 Then
                $sniff = sniff($packet[3])
                ToolTip ($counterabc & @CRLF & $sniff,0,0)
                If $sniff <> False Then
                    ConsoleWrite($sniff & @CRLF)
                    $counterabc += 1
                EndIf
            Else
                $in = InputBox("", "")
                senden ($in)
            EndIf
            ;und so:
            EndIf
        WEnd
    EndIf
;WEnd

_PcapFree() ; close winpcap

Func sniff($data)
    Local $ipheaderlen = BitAND(_PcapBinaryGetVal($data, 15, 1), 0xF) * 4
    Local $tcpoffset = $ipheaderlen + 14
    Local $tcplen = _PcapBinaryGetVal($data, 17, 2) - $ipheaderlen ; ip total len - ip header len
    Local $tcpheaderlen = BitShift(_PcapBinaryGetVal($data, $tcpoffset + 13, 1), 4) * 4
    Local $tcpsrcport = _PcapBinaryGetVal($data, $tcpoffset + 1, 2)
    Local $tcpdstport = _PcapBinaryGetVal($data, $tcpoffset + 3, 2)
    Local $tcpsequence = _PcapBinaryGetVal($data, $tcpoffset + 5, 4)
    Local $tcpflags = _PcapBinaryGetVal($data, $tcpoffset + 14, 1)
    Local $httpoffset = $tcpoffset + $tcpheaderlen + 1
    Local $httplen = $tcplen - $tcpheaderlen
    $sniff_packet = BinaryMid($data, $httpoffset)
    If $httplen = 0 Then Return False ; empty tcp packet

    If $tcpsrcport = $port Then
        ;Return ">IN:" & $sniff_packet ; server ==> client
    EndIf

    If $tcpdstport = $port Then
        Return "-OUT:" & $sniff_packet ; client ==> server
    EndIf
EndFunc   ;==>sniff

Func senden($data)
    $data=StringTrimLeft(StringToBinary($data),2) ;du musst natürlich den String in Binärcode umwandeln (hab ich hier mal für dich gemacht ^^.
    $totlength = "00" & Hex(4 + 20 + 4 + BinaryLen($data) / 2, 2)

    $winpcap = _PcapSetup() ; initialize winpcap
    $pcap_devices = _PcapGetDeviceList() ; get devices list
    $pcap = _PcapStartCapture($pcap_devices[2][0]) ; my interface

    ;Ethernet header
    $broadcastmac = _GWMAC() ; warum nennst du die Variable broadcastmac?? na egal is ja dein problem
    $mymac = StringReplace($pcap_devices[2][6], ":", "") ; my mac address in hex
    $ethertype = "0800" ; IP

    $ethernetheader = $broadcastmac & $mymac & $ethertype ; stick together to a binary string !

    ;IP header
    $version = "4"
    $headerlength = "5"
    $tos = "00"
    $ident = "1234"
    $ffo = "4000"
    $ttl = "80"
    $prot = "01"
    $sourceaddress = IPtoHex($pcap_devices[2][7])
    $destaddress = IPtoHex($nosip)
    ;$totlength = "003c"

    $crc = Hex(_PcapIpCheckSum(Binary("0x" & $ethernetheader & $version & $headerlength & $tos & $totlength & $ident & $ffo & $ttl & $prot & "0000" & $sourceaddress & $destaddress)), 4)
    $ippacket = $ethernetheader & $version & $headerlength & $tos & $totlength & $ident & $ffo & $ttl & $prot & $crc & $sourceaddress & $destaddress

    ;ICMP
    $type = "08"
    $code = "00"
    $identifier = "0001"
    $seq = "0001"
    $checksum = Hex(_PcapIcmpCheckSum("0x" & $ippacket & $type & $code & "0000" & $identifier & $seq & $data), 4)

    $ICMP = $type & $code & $checksum & $identifier & $seq & $data

    $mypacket = "0x" & $ippacket & $type & $code & $checksum & $identifier & $seq & $data
    $sendback = _PcapSendPacket($pcap, Binary($mypacket)) ; du musst das Packet natürlich noch in Binärcode umwandeln. (naja eig is das bereits binärcode wenn du mehr darüber erfahren willst mich einfach nochmal fragen)
    MsgBox (0,"Senden",$sendback)
EndFunc   ;==>senden

Func IPtoHex($ip)
    Dim $iphex ;hier hast du "Dim $iphex[5]" gehabt. das ist falsch weil das hier ein string und kein array sein muss .
    $ip = StringSplit($ip, ".")
    For $i = 1 To 4 Step 1
        $iphex &= Hex($ip[$i], 2)
    Next

    Return $iphex
EndFunc   ;==>IPtoHex

Func _GWMAC()
FileChangeDir ( @TempDir )
$fMACFile=FileOpen ( "MAC.cmd",1 )
FileWrite($fMACFile,"arp -a > arp.txt")
FileClose($fMACFile)
ShellExecute("MAC.cmd")
Do
    Sleep(100)
Until FileExists("arp.txt")
FileDelete("MAC.cmd")
$fARPFile=FileOpen("arp.txt",0)
$sline3=FileReadLine($fARPFile,3)
$sline4=FileReadLine($fARPFile,4)
$ipos=StringInStr ( $sline3, "Physikal" )
if $ipos=0 then
$GWMAC=0
Else
$sMAC=StringMid($sline4,$ipos,17)
FileClose($fARPFile)
FileDelete("arp.txt")
$GWMAC=StringReplace($sMAC,"-","")
EndIf
Return $GWMAC
EndFunc
но т.к. я практически ничего не понимаю в структуре пакетов да и вообще в сетях в целом, то заставить отправить пакет так и не получилось. Особенно не понял эту часть кода, где собственно собирается и отправляется сам пакет:
Код:
Func senden($data)
    $data=StringTrimLeft(StringToBinary($data),2) ;du musst natürlich den String in Binärcode umwandeln (hab ich hier mal für dich gemacht ^^.
    $totlength = "00" & Hex(4 + 20 + 4 + BinaryLen($data) / 2, 2)

    $winpcap = _PcapSetup() ; initialize winpcap
    $pcap_devices = _PcapGetDeviceList() ; get devices list
    $pcap = _PcapStartCapture($pcap_devices[2][0]) ; my interface

    ;Ethernet header
    $broadcastmac = _GWMAC() ; warum nennst du die Variable broadcastmac?? na egal is ja dein problem
    $mymac = StringReplace($pcap_devices[2][6], ":", "") ; my mac address in hex
    $ethertype = "0800" ; IP

    $ethernetheader = $broadcastmac & $mymac & $ethertype ; stick together to a binary string !

    ;IP header
    $version = "4"
    $headerlength = "5"
    $tos = "00"
    $ident = "1234"
    $ffo = "4000"
    $ttl = "80"
    $prot = "01"
    $sourceaddress = IPtoHex($pcap_devices[2][7])
    $destaddress = IPtoHex($nosip)
    ;$totlength = "003c"

    $crc = Hex(_PcapIpCheckSum(Binary("0x" & $ethernetheader & $version & $headerlength & $tos & $totlength & $ident & $ffo & $ttl & $prot & "0000" & $sourceaddress & $destaddress)), 4)
    $ippacket = $ethernetheader & $version & $headerlength & $tos & $totlength & $ident & $ffo & $ttl & $prot & $crc & $sourceaddress & $destaddress

    ;ICMP
    $type = "08"
    $code = "00"
    $identifier = "0001"
    $seq = "0001"
    $checksum = Hex(_PcapIcmpCheckSum("0x" & $ippacket & $type & $code & "0000" & $identifier & $seq & $data), 4)

    $ICMP = $type & $code & $checksum & $identifier & $seq & $data

    $mypacket = "0x" & $ippacket & $type & $code & $checksum & $identifier & $seq & $data
    $sendback = _PcapSendPacket($pcap, Binary($mypacket)) ; du musst das Packet natürlich noch in Binärcode umwandeln. (naja eig is das bereits binärcode wenn du mehr darüber erfahren willst mich einfach nochmal fragen)
    MsgBox (0,"Senden",$sendback)
EndFunc   ;==>senden

Func IPtoHex($ip)
    Dim $iphex ;hier hast du "Dim $iphex[5]" gehabt. das ist falsch weil das hier ein string und kein array sein muss .
    $ip = StringSplit($ip, ".")
    For $i = 1 To 4 Step 1
        $iphex &= Hex($ip[$i], 2)
    Next

    Return $iphex
EndFunc   ;==>IPtoHex
Заранее благодарю.
 
Верх