tanzina akter

tanzina akter

  • NA
  • 1
  • 3.1k

drop packet

Aug 28 2015 12:53 AM
i'm doing my final year project on Retransmission Steganography. i need to drop a captured tcp packet. i write the code. but cann't understand why this is not working. i made the connection and capturing packet with SharpPcap(c#) and use WinDivert for packet dropping(visual c++).now i find the problem is that my dropcode enters to an invalid handle exception.i use GetLastError() to know why this is happening. but doesn't show anything.program stops here.so i actualy donn't understand whats the problem. should i need to pass the captured packet to dropTcpPacket.cpp file.but how? plz help.
 
capture packet code:
static void device_OnPaketArrival(object sender, CaptureEventArgs e)
{
Packet packet = Packet.ParsePacket(e.Packet.LinkLayerType, e.Packet.Data);
var tcpPacket = TcpPacket.GetEncapsulated(packet);
if (tcpPacket != null)
{
String SPort = tcpPacket.SourcePort.ToString();
var DPort = tcpPacket.DestinationPort.ToString();
var Data = Encoding.Unicode.GetString(tcpPacket.PayloadData); //Encoding.ASCII.GetString(tcpPacket.PayloadData).ToString();
var ack = tcpPacket.Ack.ToString();
if (OptionTCP.UnsafeCompareByte(tcpPacket.PayloadData, KeyWord))
{
try
{
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "\"F:\\c#project\\serversteg\\Debug\\dropTcpPacket.exe\"";
proc.Start();
}
catch (Exception exp)
{
Console.WriteLine("errro .. {0}", exp.ToString());
}
}
else
{
Console.WriteLine("nnnnnnnnoo: {0}", Encoding.Unicode.GetString(KeyWord));

Console.WriteLine("Sport: {0}, DPort: {1}, Ack:{2} Data: {3}", SPort, DPort, ack, Data);
Console.WriteLine("==========================================================");
}

 
DropTcpPacket code:
#include "stdafx.h"
#include <winsock2.h>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "windivert.h"

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "WinDivert.lib")

#define MAXBUF 0xFFFF
#define BUFSTEG 13

typedef struct
{
WINDIVERT_IPHDR ip;
WINDIVERT_TCPHDR tcp;
} TCPPACKET, *PTCPPACKET;

int _tmain(int argc, _TCHAR* argv[]){

HANDLE handle;
WINDIVERT_ADDRESS addr;
char packet[MAXBUF];
UINT packetLen;
int countCapture = 0;
PWINDIVERT_TCPHDR tcp_header;

//int countCapture = srand(time(NULL)) % 4;
srand(time(NULL));
countCapture = rand()%4;

if (argv[0] == NULL){
printf("no prb");
fprintf(stderr, "get parametrs..\n");
exit(2);
}

handle = WinDivertOpen("SrcPort=140", WINDIVERT_LAYER_NETWORK , 0, 0); \\140 is the port from where the connection made.

if (handle == INVALID_HANDLE_VALUE)
{
printf("%s\n",GetLastError());\\The program aborts here.
exit(1);
}
while (countCapture != 0)
{
if (!WinDivertRecv(handle, packet, sizeof(packet), &addr, &packetLen))
{
printf("%s\n",GetLastError());
continue;
}
else {
WinDivertHelperParsePacket(packet, packetLen, NULL,NULL, NULL, NULL, &tcp_header,NULL, NULL, NULL);
printf("SrcPort = %u\t DstPort = %u\t AckNum = %u\t \n ================================================\n ",
ntohs(tcp_header->SrcPort), ntohs(tcp_header->DstPort), ntohl(tcp_header->AckNum));
// printf("%s\n", );
countCapture -= 1;
}

}

}

 thanks in advance