-
Notifications
You must be signed in to change notification settings - Fork 3
/
FollowTCPStream.cpp
133 lines (112 loc) · 2.86 KB
/
FollowTCPStream.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
bool checkIPMatch(unsigned char ip1[],unsigned char ip2[],unsigned char ipForMatch1[],unsigned char ipForMatch2[])
{
if(isMatchIP(ip1,ipForMatch1))
{
if(isMatchIP(ip2,ipForMatch2))
{
return true;
}
else return false;
}
else
{
if(isMatchIP(ip1,ipForMatch2))
{
if(isMatchIP(ip2,ipForMatch1))
{
return true;
}else return false;
}
}
}
bool isMatchPort(unsigned char port1[],unsigned char port2[])
{
for(int i=0;i<2;i++)
{
if(port1[i]!= port2[i]) return false;
}
return true;
}
bool checkPortMatch(unsigned char port1[],unsigned char port2[], unsigned char portForMatch1[],unsigned char portForMatch2[])
{
if(isMatchPort(port1,portForMatch1))
{
if(isMatchPort(port2,portForMatch2))
{
return true;
}
else return false;
}
else
{
if(isMatchPort(port1,portForMatch2))
{
if(isMatchPort(port2,portForMatch1))
{
return true;
}else return false;
}
}
}
void followTCPstream(int numberOfPackets,int x)
{
unsigned char clientIP[4];
unsigned char ip1[4];
unsigned char ip2[4];
unsigned char port1[2];
unsigned char port2[2];
bool firstMatch=false;
//saving ip;
for(int i=0;i<4;i++)
{
ip1[i] = sourceIpAddress[x-1][i];
ip2[i] = destinationIpAddress[x-1][i];
}
//saving port
for(int i=0;i<2;i++)
{
port1[i] = tcpHdr[x-1].sourcePort[i];
port2[i] = tcpHdr[x-1].destPort[i];
}
for(int i=0;i<numberOfPackets;i++)
{
if(checkIPMatch(ip1,ip2,sourceIpAddress[tcpPacket[i]],destinationIpAddress[tcpPacket[i]]))
{
if(checkPortMatch(port1,port2,tcpHdr[tcpPacket[i]].sourcePort,tcpHdr[tcpPacket[i]].destPort))
{
if(!firstMatch)
{
clientIP[0] = sourceIpAddress[tcpPacket[i]][0];
clientIP[1] = sourceIpAddress[tcpPacket[i]][1];
clientIP[2] = sourceIpAddress[tcpPacket[i]][2];
clientIP[3] = sourceIpAddress[tcpPacket[i]][3];
///determining client & server
firstMatch = true;
}
if(dataPayload[tcpPacket[i]]>=66)
{
if(isMatchIP(clientIP,sourceIpAddress[tcpPacket[i]]))
{
cout<<COLOR_RED;
for(int j=((((int)tcpHdr[tcpPacket[i]].headerLength)/4)-20);j<dataPayload[tcpPacket[i]]-(34+(((int)tcpHdr[tcpPacket[i]].headerLength)/4))+12;j++)
{
//if size less than 66 bytes than after urgent point all are padding
if(isprint(data[tcpPacket[i]][j])) printf("%c",data[tcpPacket[i]][j]);
else printf(".");
}cout<<COLOR_RESET;
}
else
{
cout<<COLOR_BLUE;
for(int j=((((int)tcpHdr[tcpPacket[i]].headerLength)/4)-20);j<dataPayload[tcpPacket[i]]-(34+(((int)tcpHdr[tcpPacket[i]].headerLength)/4))+12;j++)
{
//if size less than 66 bytes than after urgent point all are padding
if(isprint(data[tcpPacket[i]][j])) printf("%c",data[tcpPacket[i]][j]);
else printf(".");
}cout<<COLOR_RESET;
}cout<<endl;
}
}
}
}
}