-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
284 lines (224 loc) · 8.02 KB
/
main.c
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "main.h"
void usage( void ){
printf( "\nUsage: 'data_stream' | app network_adapter_id\n" );
printf( "=======\n" );
printf( "Transfers remote sniffed frames to a local network adapter using pcap.\n\n" );
printf( "How to:\n" );
printf( "=======\n" );
#ifdef __WIN32__
printf( "- Add a MS loopback adapter to your Windows box ( let's image that its id will be '1' );\n" );
printf( "- Disable all network properties of this new adapter;\n" );
printf( "- Reinstall winpcap or reboot your computer in order to add this new adapter to the pcap interfaces list;\n" );
printf( "- Connect to your linux remote box like this:\n\n" );
printf( "plink.exe -ssh -pw password [email protected] \"tcpdump -n -s 0 -i eth0 -w - not port ssh\" | c:\\users\\user\\Downloads\\pipe2cap.exe 1\n\n" );
printf( "=> it will transfer frames captured by tcpdump on 192.168.1.1 (except ssh) to your local MS loopback adapter identified by its id ( 1 ).\n\n" );
printf( "- Start your favorite tools and listen to your MS loopback adapter.\n\n" );
#else
printf( "- Add a dummy iface: sudo modprobe dummy;\n" );
printf( "- Start the iface: sudo ifconfig dummy0 up;\n" );
printf( "- Connect to the remote linux box like this:\n\n" );
printf( "sshpass -p password ssh [email protected] \"tcpdump -n -s 0 -i eth0 -w - not port ssh\" | sudo ./pipe2cap 1\n\n" );
printf( "=> it will transfer frames captured by tcpdump on 192.168.1.1 (except ssh) to your local adapter (dummy0) identified by its id ( 1 ).\n\n" );
printf( "- Start your favorite tools and listen to dummy0.\n\n" );
#endif // __WIN32__
printf("Enjoy!!!\n\n");
iface_listing();
}
int show_status( int status, int iFramesCpt ){
int newStatus = 0;
newStatus = status;
switch( newStatus ){
case 0:
printf( "\r-" );
break;
case 1:
printf( "\r/" );
break;
case 2:
printf( "\r|" );
break;
case 3:
printf( "\r\\" );
break;
}
printf( " : %d", iFramesCpt );
newStatus++;
if ( newStatus == 4 ){
newStatus = 0;
}
return newStatus;
}
int read_stream (int fd, void *d, int iLength ){
int iCpt = 0;
int result;
//printf( "R: %d\n", iLength );
for ( iCpt = 0; iCpt < iLength; ){
result = read ( fd,(( char* ) d ) + iCpt,( iLength - iCpt ) );
if ( result == -1 ){
return -1;
}
iCpt += result;
}
return iLength;
}
void print_packet( unsigned char * pkt_data, int iLength){
unsigned char *ptr = NULL;
int i=0;
ptr = pkt_data;
i = iLength;
do{
printf( "%s%02X",( i == iLength ) ? " " : ":", *ptr++ );
}while( --i > 0 );
printf( "\n" );
}
void iface_listing( void ){
pcap_if_t *alldevs;
pcap_if_t *d;
char errbuf[ PCAP_ERRBUF_SIZE+1 ] = { 0 };
int iCpt = 0;
printf( "Available interfaces:\n" );
printf( "=====================\n" );
if( pcap_findalldevs( &alldevs, errbuf ) == -1 ){
printf( "No network interface found!" );
return;
}
// Scan the list printing every entry
for( d = alldevs; d; d = d->next ){
if ( d->name != NULL ){
printf( " %d : %s\t(%s)\n", iCpt, ( const char* )d->name, ( const char* )d->description );
}
iCpt++;
}
printf( "\n\n" );
// Free the device list
pcap_freealldevs(alldevs);
}
int pcap_send( pcap_t *fp, unsigned char *pkt, int iLength ){
if ( iLength > MAX_MTU ){
return 1;
}
if ( pcap_sendpacket( fp, pkt, iLength ) != 0 ){
return 1;
}
return 0;
}
int main( int argc, char *argv[] ){
pcap_if_t *alldevs;
pcap_if_t *d;
int iCpt = 0;
char errbuf[ PCAP_ERRBUF_SIZE ] = { 0 };
int status = 0;
int iFramesCpt = 0;
int iReturn = 0;
#ifdef __WIN32__
struct pcap_file_header stFileHeader;
unsigned char buf[ MAX_MTU ] = { 0 };
assert ( sizeof ( stFileHeader ) == 24 );
#else
pcap_t *pcap_handle_offline_adapter;
char errbufoffline[ PCAP_ERRBUF_SIZE ] = { 0 };
struct pcap_pkthdr *header;
const unsigned char *pkt_data;
int res;
#endif // __WIN32__
#ifdef __WIN32__
iReturn = system( "cls" );
#else
iReturn = system( "clear" );
#endif // __WIN32__
if ( iReturn == -1 ){
exit( EXIT_FAILURE );
}
printf( "\n%s %s ( %s )\n", APPNAME, VERSION, RELEASEDATE );
for ( iCpt = 0 ; iCpt < ( strlen( APPNAME ) + strlen( VERSION ) + strlen( RELEASEDATE ) + 6 ) ; iCpt++ ){
printf("=");
}
printf("\n");
printf( "%s ( [email protected] )\n\n", DEVNAME );
assert ( sizeof ( unsigned int ) == 4 );
assert ( sizeof ( unsigned short ) == 2 );
if ( NULL == ( pstProject = ( stProject* )malloc( sizeof( stProject ) ) ) )
exit( EXIT_FAILURE );
pstProject->pIfaceName = NULL;
pstProject->iPcapLoopTimer = 1;
if ( argc == 1 ){
usage();
exit( EXIT_SUCCESS );
}
if ( ( pstProject->iIfaceId = atoi(argv[ 1 ] ) ) < 0 ){
usage();
exit( EXIT_FAILURE );
}
if( pcap_findalldevs( &alldevs, errbuf ) == -1 ){
printf( "No network interface found!" );
exit( EXIT_FAILURE );
}
iCpt=0;
for( d = alldevs; d; d = d->next ){
if ( pstProject->iIfaceId == iCpt ){
printf( "Iface2: %s (%s)\n\n", d->name, d->description );
pstProject->pIfaceName = strdup( d->name );
}
iCpt++;
}
errbuf[ 0 ] = '\0';
memset( &errbuf, 0, PCAP_ERRBUF_SIZE );
if( NULL== ( pstProject->pcap_handle_adapter = pcap_open_live( pstProject->pIfaceName,BUFSIZ, 1, pstProject->iPcapLoopTimer, errbuf ) ) ){
printf( "Error: cannot pcap_open_live %s!\n", pstProject->pIfaceName );
exit( EXIT_FAILURE );
}
#ifdef __WIN32__
if ( setmode( fileno( stdin ), O_BINARY ) == -1 ){
printf( "Error: cannot setmode stdin to O_BINARY\n" );
exit( EXIT_FAILURE );
}
// first we need to skip 24 bytes of file header
read_stream( STDIN_FILENO, &stFileHeader, 24 );
while ( 1 ){
struct pcap_pkthdr stPktHeader;
buf[ 0 ]='\0';
memset( &buf, 0, PCAP_ERRBUF_SIZE );
read_stream( STDIN_FILENO, &stPktHeader, sizeof ( stPktHeader ) );
if ( stPktHeader.caplen > 0){
//printf("%d // %d\n", stPktHeader.caplen, stPktHeader.len );
if ( stPktHeader.caplen <= MAX_MTU ){
iFramesCpt++;
status = show_status( status, iFramesCpt );
if ( iFramesCpt == 0xffffffff - 5 ){
iFramesCpt = 0;
printf( "+%d\n", 0xffffffff-5 );
}
read_stream( STDIN_FILENO, buf, stPktHeader.caplen );
//print_packet( buf, stPktHeader.caplen );
pcap_send( pstProject->pcap_handle_adapter, buf, stPktHeader.len );
}
else{
printf( "W: MTU=%d\n", stPktHeader.caplen );
}
}
else{
printf( "W: caplen < 0\n" );
}
}
#else
if( NULL == ( pcap_handle_offline_adapter = pcap_open_offline( "-", errbufoffline ) ) ){
printf( "Error: cannot pcap_open_offline!\n" );
exit( EXIT_FAILURE );
}
while( ( res = pcap_next_ex( pcap_handle_offline_adapter, &header, &pkt_data ) ) >= 0 ) {
if(res == 0)
continue;
if (pkt_data != NULL){
iFramesCpt++;
status = show_status( status, iFramesCpt );
if ( iFramesCpt == 0xffffffff - 5 ){
iFramesCpt = 0;
printf( "+%d\n", 0xffffffff-5 );
}
pcap_send( pstProject->pcap_handle_adapter, (unsigned char *)pkt_data, header->len );
}
}
#endif // __WIN32__
printf( "The End!!!\n" );
return 0;
}