Re: Windows Filtering Platform
Posted: Sat Oct 05, 2013 8:48 am
No problem and anytime..
damn... I'm still running on low battery!
damn... I'm still running on low battery!

http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
#DIVERT_LAYER_NETWORK = 0
#DIVERT_PRIORITY_DEFAULT = -1000
#DIVERT_FLAG_SNIFF = 1
#MAXBUF = $FFFF
Structure DIVERT_ADDRESS
IfIdx.l
SubIfIdx.l
Direction.a
EndStructure
Structure DIVERT_IPHDR
StructureUnion
HdrLength.a
Version.a
EndStructureUnion
TOS.a
Length.u
Id.u
FragOff0.u
TTL.a
Protocol.a
Checksum.u
SrcAddr.l
DstAddr.l
EndStructure
Structure DIVERT_TCPHDR
SrcPort.u
DstPort.u
SeqNum.l
AckNum.l
StructureUnion
Reserved1.a
HdrLength.a
EndStructureUnion
StructureUnion
Fin.a
Syn.a
Rst.a
Psh.a
Ack.a
Urg.a
Reserved2.a
EndStructureUnion
Window.u
Checksum.u
UrgPtr.u
EndStructure
Structure PAYLOAD
Id.u
*ppData
pDataLen.l
EndStructure
Prototype protoDivertOpen(filter.s, layer, priority.u, flags.q)
Global DivertOpen.protoDivertOpen
Prototype.b protoDivertRecv(handle, *pPacket, packetLen, pAddr, recvLen)
Global DivertRecv.protoDivertRecv
Prototype.b protoDivertHelperParsePacket(*pPacket, packetLen, *ppIpHdr, *ppIpv6Hdr, *ppIcmpHdr, *ppIcmpv6Hdr, *ppTcpHdr, *ppUdpHdr, *ppData, pDataLen)
Global DivertHelperParsePacket.protoDivertHelperParsePacket
Prototype.b protoDivertClose(handle)
Global DivertClose.protoDivertClose
Prototype.b protoDivertSetParam(handle, param, value.q)
Global DivertSetParam.protoDivertSetParam
Global Dim pPL.PAYLOAD(0)
Procedure BuildPayload()
SortStructuredArray(pPL(), #PB_Sort_Ascending, OffsetOf(PAYLOAD\Id), TypeOf(PAYLOAD\Id))
For pCount = 0 To ArraySize(pPL()) ;- 1
Debug Str(pPL(pCount)\Id) + " (" + Str(pPL(pCount)\pDataLen) + ")"
Debug "---------------"
If *Payload = #Null
plSize = 0
*Payload = AllocateMemory(pPL(pCount)\pDataLen)
Else
plSize = MemorySize(*Payload)
*Payload = ReAllocateMemory(*Payload, plSize + pPL(pCount)\pDataLen)
EndIf
CopyMemory(pPL(pCount)\ppData, *Payload + plSize, pPL(pCount)\pDataLen)
Next
If CreateFile(0, "PureBasic.deflate")
WriteData(0, *Payload, MemorySize(*Payload))
CloseFile(0)
EndIf
FreeMemory(*Payload)
EndProcedure
#DIVERT_PARAM_QUEUE_LEN = 0
#DIVERT_PARAM_QUEUE_TIME = 1
WinDivert = OpenLibrary(#PB_Any, "WinDivert.dll")
If IsLibrary(WinDivert)
DivertOpen = GetFunction(WinDivert, "DivertOpen")
DivertSetParam = GetFunction(WinDivert, "DivertSetParam")
DivertRecv = GetFunction(WinDivert, "DivertRecv")
DivertHelperParsePacket = GetFunction(WinDivert, "DivertHelperParsePacket")
DivertClose = GetFunction(WinDivert, "DivertClose")
filter.s = "inbound && (ip.SrcAddr == 88.191.144.148) && tcp.Ack"; && tcp.PayloadLength > 0"
hWndDivert = DivertOpen(filter, #DIVERT_LAYER_NETWORK, #DIVERT_PRIORITY_DEFAULT, #DIVERT_FLAG_SNIFF)
If hWndDivert <> #INVALID_HANDLE_VALUE
pAddr.DIVERT_ADDRESS
*ppIpHdr.DIVERT_IPHDR
*ppTcpHdr.DIVERT_TCPHDR
DivertSetParam(WinDivert, #DIVERT_PARAM_QUEUE_LEN, 8192)
DivertSetParam(WinDivert, #DIVERT_PARAM_QUEUE_TIME, 1024)
RunProgram("iexplore", "http://www.purebasic.com/", "")
Repeat
*pPacket = AllocateMemory(#MAXBUF)
If DivertRecv(hWndDivert, *pPacket, #MAXBUF, @pAddr, @recvLen)
DivertHelperParsePacket(*pPacket, recvLen, @*ppIpHdr, #Null, #Null, #Null, @*ppTcpHdr, #Null, @*ppData, @pDataLen)
If *ppIpHdr = #Null : Debug "warning: junk packet" : Continue : EndIf
IPHdrLength.a = PeekA(@*ppIpHdr\HdrLength) & %1111
IPLength.u = ntohs_(PeekU(@*ppIpHdr\Length))
If *ppTcpHdr
TCPHdrResv2.a = PeekA(@*ppTcpHdr\Reserved2)
EndIf
If *ppData And *ppTcpHdr ;And pDataLen
ReDim pPL(pCount)
pPL(pCount)\Id = ntohs_(PeekU(@*ppIpHdr\Id))
pPL(pCount)\ppData = AllocateMemory(pDataLen)
CopyMemory(*ppData, pPL(pCount)\ppData, pDataLen)
pPL(pCount)\pDataLen = pDataLen
pCount + 1
EndIf
If (TCPHdrResv2 & %1) : Debug "We know this Ended" : Break : EndIf
EndIf
FreeMemory(*pPacket)
ForEver : Debug "Finished"
DivertClose(hWndDivert)
EndIf
CloseLibrary(WinDivert)
RunProgram("sc", "stop WinDivert1.0", "", #PB_Program_Hide)
RunProgram("sc", "delete WinDivert1.0", "", #PB_Program_Hide)
BuildPayload()
EndIf
JHPJHP wrote:I think this needs a fresh pair of eyes / fresh perspective (bare in mind that the code is in "Mid-Test-State"):
This line is to exit the Forever loop when the last packet is received - we will have to come up with a clean Break later - modify it to fit your needs.- BuildPayload() Procedure addedCode: Select all
If pCount = 13 : Break : EndIf
-- orders the packets
-- combines the packet memory (all or whatever count you set @ the Break)
-- creates a file: PureBasic.deflate (this could be the problem - needs to be decoded in memory?)
Website I'm using to test with (GZIP-compatible encoding needs to be checked - I guess?): http://i-tools.org/gzip
(this could be the problem - compression is not compatible?)
- testing data @ the website to confirm that I'm on the right track, but only receiving a garbled mess; I wasn't expecting a fully intact webpage, but I was hoping for some partially readable text.
Code: Select all
#DIVERT_LAYER_NETWORK = 0 #DIVERT_PRIORITY_DEFAULT = 0 #DIVERT_FLAG_SNIFF = 1 #MAXBUF = $FFFF Structure DIVERT_ADDRESS IfIdx.l SubIfIdx.l Direction.a EndStructure Structure DIVERT_IPHDR StructureUnion HdrLength.a Version.a EndStructureUnion TOS.a Length.u Id.u FragOff0.u TTL.a Protocol.a Checksum.u SrcAddr.l DstAddr.l EndStructure Structure DIVERT_TCPHDR SrcPort.u DstPort.u SeqNum.l AckNum.l StructureUnion Reserved1.a HdrLength.a EndStructureUnion StructureUnion Fin.a Syn.a Rst.a Psh.a Ack.a Urg.a Reserved2.a EndStructureUnion Window.u Checksum.u UrgPtr.u EndStructure Structure PAYLOAD Id.u *ppData pDataLen.l EndStructure Prototype protoDivertOpen(filter.s, layer, priority.u, flags.q) Global DivertOpen.protoDivertOpen Prototype.b protoDivertRecv(handle, *pPacket, packetLen, pAddr, recvLen) Global DivertRecv.protoDivertRecv Prototype.b protoDivertHelperParsePacket(*pPacket, packetLen, *ppIpHdr, *ppIpv6Hdr, *ppIcmpHdr, *ppIcmpv6Hdr, *ppTcpHdr, *ppUdpHdr, *ppData, pDataLen) Global DivertHelperParsePacket.protoDivertHelperParsePacket Prototype.b protoDivertClose(handle) Global DivertClose.protoDivertClose Global Dim pPL.PAYLOAD(0) Procedure BuildPayload() SortStructuredArray(pPL(), #PB_Sort_Ascending, OffsetOf(PAYLOAD\Id), TypeOf(PAYLOAD\Id)) For pCount = 0 To ArraySize(pPL()) - 1 Debug Str(pPL(pCount)\Id) + " (" + Str(pPL(pCount)\pDataLen) + ")" Debug "---------------" If *Payload = #Null plSize = 0 *Payload = AllocateMemory(pPL(pCount)\pDataLen) Else plSize = MemorySize(*Payload) *Payload = ReAllocateMemory(*Payload, plSize + pPL(pCount)\pDataLen) EndIf CopyMemory(pPL(pCount)\ppData, *Payload + plSize, pPL(pCount)\pDataLen) Next If CreateFile(0, "PureBasic.deflate") WriteData(0, *Payload, MemorySize(*Payload)) CloseFile(0) EndIf FreeMemory(*Payload) EndProcedure WinDivert = OpenLibrary(#PB_Any, "WinDivert.dll") If IsLibrary(WinDivert) DivertOpen = GetFunction(WinDivert, "DivertOpen") DivertSetParam = GetFunction(WinDivert, "DivertSetParam") DivertRecv = GetFunction(WinDivert, "DivertRecv") DivertHelperParsePacket = GetFunction(WinDivert, "DivertHelperParsePacket") DivertClose = GetFunction(WinDivert, "DivertClose") filter.s = "(ip.SrcAddr == 88.191.144.148 || ip.DstAddr == 88.191.144.148) && tcp.PayloadLength > 0" hWndDivert = DivertOpen(filter, #DIVERT_LAYER_NETWORK, #DIVERT_PRIORITY_DEFAULT, #DIVERT_FLAG_SNIFF) If hWndDivert <> #INVALID_HANDLE_VALUE pAddr.DIVERT_ADDRESS *ppIpHdr.DIVERT_IPHDR *ppTcpHdr.DIVERT_TCPHDR RunProgram("iexplore", "http://www.purebasic.com/", "") Repeat *pPacket = AllocateMemory(#MAXBUF) If DivertRecv(hWndDivert, *pPacket, #MAXBUF, @pAddr, @recvLen) DivertHelperParsePacket(*pPacket, recvLen, @*ppIpHdr, #Null, #Null, #Null, @*ppTcpHdr, #Null, @*ppData, @pDataLen) If *ppData PacketData.s = PeekS(*ppData, pDataLen, #PB_UTF8) If FindString(PacketData, "HTTP") = 0 Redim pPL(pCount) pPL(pCount)\Id = ntohs_(PeekU(@*ppIpHdr\Id)) pPL(pCount)\ppData = *ppData pPL(pCount)\pDataLen = pDataLen pCount + 1 If pCount = 13 : Break : EndIf EndIf EndIf EndIf FreeMemory(*pPacket) ForEver DivertClose(hWndDivert) EndIf CloseLibrary(WinDivert) RunProgram("sc", "stop WinDivert1.0", "", #PB_Program_Hide) RunProgram("sc", "delete WinDivert1.0", "", #PB_Program_Hide) BuildPayload() EndIf
Code: Select all
If FindString(PacketData, "HTTP") = 0
Code: Select all
If FindString(PacketData, "HTTP") > 0
Code: Select all
#DIVERT_LAYER_NETWORK = 0
#DIVERT_PRIORITY_DEFAULT = 0
#DIVERT_FLAG_SNIFF = 1
#MAXBUF = $FFFF
Structure DIVERT_ADDRESS
IfIdx.l
SubIfIdx.l
Direction.a
EndStructure
Structure DIVERT_IPHDR
StructureUnion
HdrLength.a
Version.a
EndStructureUnion
TOS.a
Length.u
Id.u
FragOff0.u
TTL.a
Protocol.a
Checksum.u
SrcAddr.l
DstAddr.l
EndStructure
Structure DIVERT_TCPHDR
SrcPort.u
DstPort.u
SeqNum.l
AckNum.l
StructureUnion
Reserved1.a
HdrLength.a
EndStructureUnion
StructureUnion
Fin.a
Syn.a
Rst.a
Psh.a
Ack.a
Urg.a
Reserved2.a
EndStructureUnion
Window.u
Checksum.u
UrgPtr.u
EndStructure
Structure PAYLOAD
Id.u
AckNum.l
*ppData
pDataLen.l
EndStructure
Prototype protoDivertOpen(filter.s, layer, priority.u, flags.q)
Global DivertOpen.protoDivertOpen
Prototype.b protoDivertRecv(handle, *pPacket, packetLen, pAddr, recvLen)
Global DivertRecv.protoDivertRecv
Prototype.b protoDivertHelperParsePacket(*pPacket, packetLen, *ppIpHdr, *ppIpv6Hdr, *ppIcmpHdr, *ppIcmpv6Hdr, *ppTcpHdr, *ppUdpHdr, *ppData, pDataLen)
Global DivertHelperParsePacket.protoDivertHelperParsePacket
Prototype.b protoDivertClose(handle)
Global DivertClose.protoDivertClose
Global Dim pPL.PAYLOAD(0), AckNum
Procedure BuildPayload()
pRange.b = #False
SortStructuredArray(pPL(), #PB_Sort_Ascending, OffsetOf(PAYLOAD\Id), TypeOf(PAYLOAD\Id))
For pCount = 0 To ArraySize(pPL()) - 1
PacketData.s = PeekS(pPL(pCount)\ppData, pPL(pCount)\pDataLen, #PB_UTF8)
If FindString(PacketData, "Content-Type: text/html") > 0 Or pRange
If pRange
If pPL(pCount)\Id > pId + 2 : Break : Else : pId = pPL(pCount)\Id : EndIf
Else
pRange = #True
pId = pPL(pCount)\Id
; Continue
EndIf
Debug Str(pPL(pCount)\Id) + " (" + Str(pPL(pCount)\pDataLen) + ")"
If AckNum = 0
Debug "Acknowledgment Number FIRST"
Else
If pPL(pCount)\AckNum = AckNum : Debug "Acknowledgment Number MATCH" : Else : Debug "Acknowledgment Number ERROR" : EndIf
EndIf
AckNum = pPL(pCount)\AckNum
Debug "---------------"
If *Payload
plSize = MemorySize(*Payload)
*Payload = ReAllocateMemory(*Payload, plSize + pPL(pCount)\pDataLen)
Else
plSize = 0
*Payload = AllocateMemory(pPL(pCount)\pDataLen)
EndIf
CopyMemory(pPL(pCount)\ppData, *Payload + plSize, pPL(pCount)\pDataLen)
If pPL(pCount)\pDataLen < 1460 : Break : EndIf
EndIf
Next
If CreateFile(0, "tPacket.txt")
WriteData(0, *Payload, MemorySize(*Payload))
CloseFile(0)
EndIf
FreeMemory(*Payload)
EndProcedure
WinDivert = OpenLibrary(#PB_Any, "WinDivert.dll")
If IsLibrary(WinDivert)
DivertOpen = GetFunction(WinDivert, "DivertOpen")
DivertSetParam = GetFunction(WinDivert, "DivertSetParam")
DivertRecv = GetFunction(WinDivert, "DivertRecv")
DivertHelperParsePacket = GetFunction(WinDivert, "DivertHelperParsePacket")
DivertClose = GetFunction(WinDivert, "DivertClose")
filter.s = "inbound && ip.SrcAddr == 88.191.144.148 && tcp.Ack"
hWndDivert = DivertOpen(filter, #DIVERT_LAYER_NETWORK, #DIVERT_PRIORITY_DEFAULT, #DIVERT_FLAG_SNIFF)
If hWndDivert <> #INVALID_HANDLE_VALUE
pAddr.DIVERT_ADDRESS
*ppIpHdr.DIVERT_IPHDR
*ppTcpHdr.DIVERT_TCPHDR
RunProgram("iexplore", "http://www.purebasic.com/", "")
Repeat
*pPacket = AllocateMemory(#MAXBUF)
If DivertRecv(hWndDivert, *pPacket, #MAXBUF, @pAddr, @recvLen)
DivertHelperParsePacket(*pPacket, recvLen, @*ppIpHdr, #Null, #Null, #Null, @*ppTcpHdr, #Null, @*ppData, @pDataLen)
If *ppIpHdr And *ppTcpHdr
If *ppData
ReDim pPL(pCount)
pPL(pCount)\Id = ntohs_(PeekU(@*ppIpHdr\Id))
pPL(pCount)\AckNum = ntohl_(PeekL(@*ppTcpHdr\AckNum))
pPL(pCount)\ppData = AllocateMemory(pDataLen)
CopyMemory(*ppData, pPL(pCount)\ppData, pDataLen)
pPL(pCount)\pDataLen = pDataLen
pCount + 1
EndIf
If PeekA(@*ppTcpHdr\Reserved2) & %1 : Break : EndIf
EndIf
EndIf
FreeMemory(*pPacket)
ForEver
DivertClose(hWndDivert)
EndIf
CloseLibrary(WinDivert)
RunProgram("sc", "stop WinDivert1.0", "", #PB_Program_Hide)
RunProgram("sc", "delete WinDivert1.0", "", #PB_Program_Hide)
BuildPayload()
EndIf
Code: Select all
#DIVERT_LAYER_NETWORK = 0
#DIVERT_PRIORITY_DEFAULT = 0
#DIVERT_FLAG_SNIFF = 1
#MAXBUF = $FFFF
Structure DIVERT_ADDRESS
IfIdx.l
SubIfIdx.l
Direction.a
EndStructure
Structure DIVERT_IPHDR
StructureUnion
HdrLength.a
Version.a
EndStructureUnion
TOS.a
Length.u
Id.u
FragOff0.u
TTL.a
Protocol.a
Checksum.u
SrcAddr.l
DstAddr.l
EndStructure
Structure DIVERT_TCPHDR
SrcPort.u
DstPort.u
SeqNum.l
AckNum.l
StructureUnion
Reserved1.a
HdrLength.a
EndStructureUnion
StructureUnion
Fin.a
Syn.a
Rst.a
Psh.a
Ack.a
Urg.a
Reserved2.a
EndStructureUnion
Window.u
Checksum.u
UrgPtr.u
EndStructure
Structure PAYLOAD
HdrLength.a
Length.u
Id.u
AckNum.l
*ppData
pDataLen.l
EndStructure
Prototype protoDivertOpen(filter.s, layer, priority.u, flags.q)
Global DivertOpen.protoDivertOpen
Prototype.b protoDivertRecv(handle, *pPacket, packetLen, pAddr, recvLen)
Global DivertRecv.protoDivertRecv
Prototype.b protoDivertHelperParsePacket(*pPacket, packetLen, *ppIpHdr, *ppIpv6Hdr, *ppIcmpHdr, *ppIcmpv6Hdr, *ppTcpHdr, *ppUdpHdr, *ppData, pDataLen)
Global DivertHelperParsePacket.protoDivertHelperParsePacket
Prototype.b protoDivertClose(handle)
Global DivertClose.protoDivertClose
Global Dim pPL.PAYLOAD(0)
Procedure BuildPayload()
pRange.b = #False
SortStructuredArray(pPL(), #PB_Sort_Ascending, OffsetOf(PAYLOAD\Id), TypeOf(PAYLOAD\Id))
For pCount = 0 To ArraySize(pPL()) - 1
PacketData.s = PeekS(pPL(pCount)\ppData, pPL(pCount)\pDataLen, #PB_UTF8)
If FindString(PacketData, "Content-Type: text/html") > 0 Or pRange
If pPL(pCount)\pDataLen <> (pPL(pCount)\Length - pPL(pCount)\HdrLength) : Debug "ERROR: Length" : Break : EndIf
If pRange
If pPL(pCount)\Id > pId + 2 : Break : Else : pId = pPL(pCount)\Id : EndIf
If pPL(pCount)\AckNum <> pPL(pCount - 1)\AckNum : Break : EndIf
Else
pRange = #True
pId = pPL(pCount)\Id
; Continue
EndIf
Debug Str(pPL(pCount)\Id) + " (" + Str(pPL(pCount)\pDataLen) + ")"
Debug "---------------"
If *Payload
plSize = MemorySize(*Payload)
*Payload = ReAllocateMemory(*Payload, plSize + pPL(pCount)\pDataLen)
Else
plSize = 0
*Payload = AllocateMemory(pPL(pCount)\pDataLen)
EndIf
CopyMemory(pPL(pCount)\ppData, *Payload + plSize, pPL(pCount)\pDataLen)
EndIf
Next
If CreateFile(0, "tPacket.txt")
WriteData(0, *Payload, MemorySize(*Payload))
CloseFile(0)
EndIf
FreeMemory(*Payload)
EndProcedure
WinDivert = OpenLibrary(#PB_Any, "WinDivert.dll")
If IsLibrary(WinDivert)
DivertOpen = GetFunction(WinDivert, "DivertOpen")
DivertSetParam = GetFunction(WinDivert, "DivertSetParam")
DivertRecv = GetFunction(WinDivert, "DivertRecv")
DivertHelperParsePacket = GetFunction(WinDivert, "DivertHelperParsePacket")
DivertClose = GetFunction(WinDivert, "DivertClose")
filter.s = "inbound && ip.SrcAddr == 88.191.144.148 && tcp.Ack"
hWndDivert = DivertOpen(filter, #DIVERT_LAYER_NETWORK, #DIVERT_PRIORITY_DEFAULT, #DIVERT_FLAG_SNIFF)
If hWndDivert <> #INVALID_HANDLE_VALUE
pAddr.DIVERT_ADDRESS
*ppIpHdr.DIVERT_IPHDR
*ppTcpHdr.DIVERT_TCPHDR
RunProgram("iexplore", "http://www.purebasic.com/", "")
Repeat
*pPacket = AllocateMemory(#MAXBUF)
If DivertRecv(hWndDivert, *pPacket, #MAXBUF, @pAddr, @recvLen)
DivertHelperParsePacket(*pPacket, recvLen, @*ppIpHdr, #Null, #Null, #Null, @*ppTcpHdr, #Null, @*ppData, @pDataLen)
If *ppIpHdr And *ppTcpHdr
If *ppData
ReDim pPL(pCount)
pPL(pCount)\HdrLength = (PeekA(@*ppIpHdr\Version) & %1111 * 32 / 8) + (PeekA(@*ppTcpHdr\HdrLength) >> 4 & %1111 * 4)
pPL(pCount)\Length = ntohs_(PeekU(@*ppIpHdr\Length))
pPL(pCount)\Id = ntohs_(PeekU(@*ppIpHdr\Id))
pPL(pCount)\AckNum = ntohl_(PeekL(@*ppTcpHdr\AckNum))
pPL(pCount)\ppData = AllocateMemory(pDataLen)
CopyMemory(*ppData, pPL(pCount)\ppData, pDataLen)
pPL(pCount)\pDataLen = pDataLen
pCount + 1
EndIf
If PeekA(@*ppTcpHdr\Reserved2) & %1 : Break : EndIf
EndIf
EndIf
FreeMemory(*pPacket)
ForEver
DivertClose(hWndDivert)
EndIf
CloseLibrary(WinDivert)
RunProgram("sc", "stop WinDivert1.0", "", #PB_Program_Hide)
RunProgram("sc", "delete WinDivert1.0", "", #PB_Program_Hide)
BuildPayload()
EndIf
I can't say for certain, but I think you may be wrong on this one; the exact line is:You not realizing this yet, but you stop dealing with the other segments when you doing the reassembling in...
If FindString(PacketData, "Content-Type: text/html") > 0
Code: Select all
If FindString(PacketData, "Content-Type: text/html") > 0 Or pRange
JHPJHP wrote:I can't say for certain, but I think you may be wrong on this one; the exact line is:You not realizing this yet, but you stop dealing with the other segments when you doing the reassembling in...
If FindString(PacketData, "Content-Type: text/html") > 0If FindString(PacketData, "Content-Type: text/html") > 0 is only to determine where the first segment (header) starts, then it ignores that and loops for the other segments based on Or pRange.Code: Select all
If FindString(PacketData, "Content-Type: text/html") > 0 Or pRange
JHPJHP wrote:It's not often (or to date) that you've been wrong...![]()
-----------------------------------------------------------------------------
I haven't taken Padding into account yet, will it have an effect on decompressing the packet?