При попытке подделать ip клиента в сокетной программе выдается ошибка выполнения «NO ERROR» - PullRequest
0 голосов
/ 01 апреля 2019

Когда я запускаю приведенный ниже код с помощью компилятора MINGW64, он выдает ошибку времени выполнения с сообщением об ошибке «no error» в методе sendto при использовании IPPROTO_TCP или IPPROTO_UDP при создании Raw Socket. Когда я использую значение по умолчанию 0 при создании сокета, например, sd = socket (AF_INET, SOCK_RAW, 0). Не выдает ошибку, но соединение с сервером никогда не происходит.

Серверная программа на Java:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class FileServer {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        ServerSocket serverSocket = null;
        serverSocket = new ServerSocket(9090);

        Socket clientSocket = null;
        clientSocket = serverSocket.accept();
        BufferedReader b = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        InputStream in = clientSocket.getInputStream();
        DataInputStream D = new DataInputStream(in); 
        // String fileName = clientData.readUTF();  
        //BufferedWriter out = new BufferedWriter(new FileWriter("E:\\"+fileName));
        System.out.println(clientSocket.getInetAddress());
        String a = b.readLine();

        System.out.println(a);
        while(a!=null)
        {
            //out.append(a);
            //out.newLine();
            //out.flush();
            a = b.readLine();
            System.out.println(a);
        }
        // out.flush();
        // out.close();
        b.close();

    }
}

Клиентская программа на C ++:

#include <jni.h>
#include <iostream>
#include <stdio.h> 
#include <unistd.h>
#include <winsock2.h> 
#include <windows.h>
#include <Ws2tcpip.h>
#include <stdlib.h>
#include <string.h> 
#include "IP_Spoof_JNI.h"
using namespace std;
#define PCKT_LEN 8192
// May create separate header file (.h) for all
// headers' structures
// IP header's structure
struct ipheader {
unsigned char iph_ihl:5, /* Little-endian */
iph_ver:4;
unsigned char iph_tos;
unsigned short int iph_len;
unsigned short int iph_ident;
unsigned char iph_flags;
unsigned short int iph_offset;
unsigned char iph_ttl;
unsigned char iph_protocol;
unsigned short int iph_chksum;
unsigned int iph_sourceip;
unsigned int iph_destip;
};
/* Structure of a TCP header */
struct tcpheader {
unsigned short int tcph_srcport;
unsigned short int tcph_destport;
unsigned int tcph_seqnum;
unsigned int tcph_acknum;
unsigned char tcph_reserved:4, tcph_offset:4;
// unsigned char tcph_flags;
unsigned int
tcp_res1:4, /little-endian/
tcph_hlen:4, /length of tcp header in 32-bit words/
tcph_fin:1, /Finish flag "fin"/
tcph_syn:1, /Synchronize sequence numbers to start a connection/
tcph_rst:1, /*Reset flag */
tcph_psh:1, /Push, sends data to the application/
tcph_ack:1, /acknowledge/
tcph_urg:1, /urgent pointer/
tcph_res2:2;
unsigned short int tcph_win;
unsigned short int tcph_chksum;
unsigned short int tcph_urgptr;

}
;
// Simple checksum function, may use others such as
// Cyclic Redundancy Check, CRC
unsigned short csum(unsigned short *buf, int len)
{
unsigned long sum;
for(sum=0; len>0; len--)
sum += *buf++;
sum = (sum >> 16) + (sum &0xffff);
sum += (sum >> 16);
return (unsigned short)(~sum);
}
JNIEXPORT jint JNICALL Java_IP_1Spoof_1JNI_disp
(JNIEnv *env, jobject thisObj, jstring a){
    const char *in = env->GetStringUTFChars(a, NULL);
    int sd;
// No data, just datagram
char buffer[PCKT_LEN];
// The size of the headers
struct ipheader *ip = (struct ipheader *) buffer;
struct tcpheader *tcp = (struct tcpheader *) (buffer + sizeof(struct ipheader));
struct sockaddr_in sin, din;
int one = 1;
const char val = (char) &one;
memset(buffer, 0, PCKT_LEN);
/*if(argc != 5)
{
printf("- Invalid parameters!!!\n");
printf("- Usage: %s <source hostname/IP> <source port> <target hostname/IP> <target port>\n", argv[0]);
exit(-1);
}*/
sd = socket(AF_INET, SOCK_RAW, 0);
if(sd < 0)
{
perror("socket() error");
exit(-1);
}
else
printf("socket()-SOCK_RAW and tcp protocol is OK.\n");
// The source is redundant, may be used later if needed
// Address family
sin.sin_family = AF_INET;
din.sin_family = AF_INET;
// Source port, can be any, modify as needed
sin.sin_port = htons(9090);
din.sin_port = htons(9090);
// Source IP, can be any, modify as needed
sin.sin_addr.s_addr = inet_addr("172.24.127.115");
din.sin_addr.s_addr = inet_addr("172.24.127.115");
// IP structure
ip->iph_ihl = 5;
ip->iph_ver = 4;
ip->iph_tos = 16;
ip->iph_len = sizeof(struct ipheader) + sizeof(struct tcpheader);
ip->iph_ident = htons(54321);
ip->iph_offset = 0;
ip->iph_ttl = 64;
ip->iph_protocol = 6; // TCP
ip->iph_chksum = 0; // Done by kernel
// Source IP, modify as needed, spoofed, we accept through
// command line argument
ip->iph_sourceip = inet_addr("172.24.127.115");
// Destination IP, modify as needed, but here we accept through
// command line argument
ip->iph_destip = inet_addr("172.24.127.115");
// TCP structure
// The source port, spoofed, we accept through the command line
tcp->tcph_srcport = htons(9090);
// The destination port, we accept through command line
tcp->tcph_destport = htons(9090);
tcp->tcph_seqnum = htonl(1);
tcp->tcph_acknum = 0;
tcp->tcph_offset = 5;
tcp->tcph_syn = 1;
tcp->tcph_ack = 0;
tcp->tcph_win = htons(32767);
tcp->tcph_chksum = 0; // Done by kernel
tcp->tcph_urgptr = 0;
// IP checksum calculation
ip->iph_chksum = csum((unsigned short *) buffer, (sizeof(struct ipheader) + sizeof(struct tcpheader)));
// Inform the kernel do not fill up the headers'
// structure, we fabricated our own
if(setsockopt(sd, IPPROTO_IP, IP_HDRINCL, val, sizeof(one)) < 0)
{
perror("setsockopt() error");
exit(-1);
}
else
printf("setsockopt() is OK\n");
printf("Using:::::Source IP: %s port: %u, Target IP: %s port: %u.\n %u \n", "172.24.127.115", 9090, "172.24.127.115", 9090, sd);
unsigned char data[0];
memcpy(buffer, &ip, sizeof(ip));
memcpy(buffer + sizeof(ip), &tcp, sizeof(tcp));
memcpy(buffer + sizeof(ip) + sizeof(tcp), data, sizeof(data));
// sendto() loop, send every 2 second for 50 counts
unsigned int count;
if (connect(sd, (struct sockaddr *)&din, sizeof(din)) < 0) 
    { 
        perror("Connection Failed"); 
        return -1; 
    }
    else{
        printf("connected");
    }
for(count = 0; count < 10; count++)
{
if(sendto(sd, buffer, ip->iph_len, 0, (struct sockaddr *)&din, sizeof(din)) < 0)
// Verify
{
perror("sendto() error");
}
else
printf("Count #%u - sendto() is OK\n", count);
sleep(2);
shutdown(sd,SD_SEND);
}
close(sd);
return 0;
}
...