Modify packet [DELPHI]

Home Forums Discussions Support Portal Modify packet [DELPHI]

This topic contains 8 replies, has 5 voices, and was last updated by  SergeySW 6 years, 1 month ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #4856

    rdx83
    Participant

    Hi,

    I need to modify packet, but how to calculate the new checksum in Delphi

    I have tried to translate C++ example, but it doesn’t work 🙁

    Thank for your help

    (sorry for my english).

    #5575

    heilong
    Participant

    Try it. It is universal function.

    p – pointer to begin of header.
    Size – size of header in words.
    Attention: Field Checksum must be = 0 before call function.

    function Checksum(p: PWORD; Size: word): WORD;
    var
    i: Integer;
    Sum: DWORD;
    begin
    Sum := 0;
    for i := 0 to Size – 1 do
    begin
    Sum := Sum + ntohs(p^);
    inc(p);
    end;
    while (Sum shr 16) > 0 do
    Sum := (Sum and $FFFF) + (Sum shr 16);
    Sum := not Sum;
    Result := Sum;
    end;

    Examples:
    1. Calc checksum for IPHeader. For uses without IPOptions.

    IPHeader.Checksum := 00;
    IPHeader.Checksum := htons(Checksum(Pword(IPHeader), sizeof(IP_HDR) div 2));

    2. Calc checksum for ICMPHeader.
    IcmpHeader.Checksum := 00;
    IcmpHeader.Checksum := htons(Checksum(Pword(IcmpHeader),
    (sizeof(ICMP_HDR)+Length(Data)) div 2));

    Don`t forget call htons for result of function Checksum.
    Sorry for my English.

    #5576

    rdx83
    Participant

    Thank for your help 🙂

    But how to calculate TCP checksum ?

    I’ve tried your function but the checksum is incorrect

    Thanks

    #5577

    heilong
    Participant

    Checksum = calculate (pointer, (header+data) div 2), as for ICMP as TCP

    Example:


    Packet = ETH_HDR+IP_HDR+TCP_HDR+Data;
    IP_HDR.Checksum := 00;
    IP_HDR.Checksum := htons(Checksum(Pword(IP_HDR), sizeof(IP_HDR) div 2));

    TCP.Checksum := 00;
    TCP_HDR.Checksum := htons(Checksum(Pword(TCP_HDR), sizeof((TCP_HDR+Length(Data)) div 2));

    For more: See ICMP example in previous reply

    #5578

    PLY
    Participant

    Why i can’y calc TCP checksum? Please help

    first, i decare this


    TIPHeaderPtr = ^TIPHeader;
    TIPHeader = packed record
    VerLen: Byte; //HL
    TOS: Byte;
    TotalLen: Word;
    Identifer: Word;
    FragOffsets: Word;
    TTL: Byte;
    Protocol: Byte;
    CheckSum: Word;
    SourceIp: DWORD;
    DestIp: DWORD;
    // Options: DWORD;
    end;

    TTCPHeaderPtr = ^TTCPHeader;
    TTCPHeader = packed record
    SourcePort:Word;
    DestPort:Word;
    SequenceNumber:DWord;
    AcknowledgementNumber:DWord;
    Offset:Byte; //only left 4 bits. Header length in 32-bit segments
    Flags:Byte;
    Window:Word;
    Checksum:Word; //includes speudo header instead of TCP header.
    UrgentPointer:Word;
    end;

    pTCPData: PChar;

    after that i get IP header and calc IP check sum is collect but why i can’t calc TCP checksum. the code like below.


    pTCPHeader.Checksum := 00;
    pTCPHeader.Checksum := htons(Checksum(Pword(pTCPHeader), (sizeof(TTCPHeader)+length(pTCPData)) div 2));

    i forgot something? or i should not use PChar for get the TCP data?

    #5579

    PLY
    Participant

    @ply wrote:

    Why i can’y calc TCP checksum? Please help

    first, i declare this


    TIPHeaderPtr = ^TIPHeader;
    TIPHeader = packed record
    VerLen: Byte; //HL
    TOS: Byte;
    TotalLen: Word;
    Identifer: Word;
    FragOffsets: Word;
    TTL: Byte;
    Protocol: Byte;
    CheckSum: Word;
    SourceIp: DWORD;
    DestIp: DWORD;
    // Options: DWORD;
    end;

    TTCPHeaderPtr = ^TTCPHeader;
    TTCPHeader = packed record
    SourcePort:Word;
    DestPort:Word;
    SequenceNumber:DWord;
    AcknowledgementNumber:DWord;
    Offset:Byte; //only left 4 bits. Header length in 32-bit segments
    Flags:Byte;
    Window:Word;
    Checksum:Word; //includes speudo header instead of TCP header.
    UrgentPointer:Word;
    end;

    pTCPData: PChar;

    after that i get IP header and calc IP check sum is collect but why i can’t calc TCP checksum. the code like below.


    pTCPHeader.Checksum := 00;
    pTCPHeader.Checksum := htons(Checksum(Pword(pTCPHeader), (sizeof(TTCPHeader)+length(pTCPData)) div 2));

    i forgot something? or i should not use PChar for get the TCP data?

    #5580

    heilong
    Participant

    I not absolutely understand a question, therefore I shall answer how I have understood it.


    ...
    var
    ReadRequest: ETH_REQUEST;
    Buffer: INTERMEDIATE_BUFFER;
    pEthernetHeader: PETHERNET_HDR;
    pIpHeader: PIP_HDR;
    pTCPHeader: PTCP_HDR;
    pData: Pbyte;
    DataSize: Integer;
    ...

    const
    HSIZE = sizeof(ETHERNET_HDR) + sizeof(IP_HDR)+ sizeof(ICMP_HDR);

    ...

    ReadRequest.EthPacket.Buffer := @Buffer;
    ReadRequest.hAdapterHandle := hAdapter;

    ...

    if ReadPacket (hFilt, @ReadRequest) <> 0 then
    begin
    ...
    pEthernetHeader := @ReadRequest.EthPacket.Buffer.m_IBuffer;
    pIPHeader := Pointer(integer(pEthernetHeader)+sizeof(ETHERNET_HDR));
    pTCPHeader := Pointer(integer(pIPHeader)+sizeof(IP_HDR));
    DataSize := ReadRequest.EthPacket.Buffer.m_Length - HSIZE;

    PData := Pointer(integer(pEthernetHeader)+HSIZE);
    // or so
    //PData := Pointer(integer(pTCPHeader)+sizeof(TCP_HDR));
    ...

    // Change Port Destination to 4032

    pTCPHeader.Destination := htons(4032);

    // Tne IPHeader checksum it is not recalculated therefore as the size of a package has not changed

    pTCPHeader.CheckSum := 00;
    pTCPHeader.Checksum := htons(Checksum(Pword(pTCPHeader), (sizeof(TCP_HDR) + DataSize) div 2));

    //If the size of a packet has changed, it is necessary to align a packet to word and recalc IP Header and accordingly TCP Header
    ...

    SendPacketToAdapter(hFilt, @ReadRequest);
    end;
    ...

    In a code there can be mistakes, for what beforehand apologize.

    #5581

    esfahan
    Participant

    Hi
    i write a program with delphi
    but i have problem Correct Calculate TCP CheckSum
    i used code write by “heilong”
    give correct on IPcheksum And ICMPcheksum
    but on UDP & TCP incorect result
    i think this code need “pseudo header”
    See Picture :

    i read all other post in froums about CheckSum ,
    and read all fucntion in C++ to calculatechecksum

    i can convert this code to DLL and export fucntion:
    http://www.netfor2.com/tcpsum.htm
    but I do not know used parammerter in delphi

    But i can’t wirte a correct algoritm in delphi 🙁
    or give me any solution in DELPHI to i can correct TCP & UDP checksum Calculate
    .

    Edited @00:14 AM , used 🙂 OKY 😯
    دهنمو گاید ، ولی بلاخره شد ، ای مردشور هرچی پوینتر هست ببرن 🙂

    #5582

    SergeySW
    Participant

    function RecalculateTCPChecksum(aLen: Integer; aSrcAddr, aDstAddr: Cardinal; buff: PByteArray): Word;
    var
    w16,padd: word;
    i,sum: ulong;
    sIP,dIP: in_addr;
    begin
    padd := 0;
    sum := 0;
    if (aLen div 2) * 2 <> aLen then begin
    padd := 1;
    buff[aLen] := 0;
    end;
    i := 0;
    while i < aLen+padd do begin
    w16 := ((buff shl 8)and $FF00) + (buff[i+1]and $FF);
    sum := sum + dword(w16);
    i := i + 2;
    end;

    //add IP length
    sIP := in_addr(aSrcAddr);
    dIP := in_addr(aDstAddr);
    sum := sum + ntohs(sIP.S_un_w.s_w1) + ntohs(sIP.S_un_w.s_w2);
    sum := sum + ntohs(dIP.S_un_w.s_w1) + ntohs(dIP.S_un_w.s_w2);
    sum := sum + IPPROTO_TCP + word(aLen);

    while (sum shr 16)>0 do
    sum := (sum and $FFFF)+(sum shr 16);

    sum := not sum;
    sum := htons(sum);
    Result := sum;
    end;

    aaLen := ntohs(pIPHeader.TotalLen) - ((pIPHeader.VerLen and $F)*4);
    pTCPHeader.Checksum := 0;
    testSum := RecalculateTCPChecksum(aaLen, pIPHeader.SourceIp, pIPHeader.DestIp, byteArray(pTCPHeader));
    pTCPHeader.Checksum := testSum;

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.