-
Notifications
You must be signed in to change notification settings - Fork 6
/
target.c
139 lines (112 loc) · 2.45 KB
/
target.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
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <linux/fs.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define DEBUG 1
#define debug(fmt, ...)\
do{\
if(DEBUG)\
fprintf(stderr, "%s(%d): " fmt "\n", __FUNCTION__, __LINE__, __VA_ARGS__);\
}while(0)
/* Let the size of the data to be sufficiently large */
#define READ_DATA_SIZE (1024)*(1024)
#define SAMPLING_TIME 1
#define SLEEP_TIME 1
long
get_read_addr(int drive_fd)
{
int ret = 0;
long rand_num;
off_t drive_size;
ret = ioctl(drive_fd, BLKGETSIZE64, &drive_size);
if(ret == -1)
{
perror("ioctl ");
return -1;
}
//debug("Size of disk : %ld", drive_size);
while(true)
{
rand_num = random();
if(rand_num < drive_size)
break;
}
return rand_num;
}
void
synchronize()
{
time_t start = time(NULL);
debug("Synchronizing %ld...",start);
while(start % 10 != 5)
{
sleep(1);
start = time(NULL);
}
debug("covert communication started %ld...", start);
}
int
send_bit(int disk_fd, char bit)
{
long read_addr;
time_t start;
char read_buff[READ_DATA_SIZE];
printf("Sending bit -------> (%c)\n", bit);
if(bit == '0')
{
sleep(SLEEP_TIME);
}
else
{
read_addr = get_read_addr(disk_fd);
start = time(NULL);
while(time(NULL) <= (start + SAMPLING_TIME))
{
lseek(disk_fd, read_addr, SEEK_SET);
read(disk_fd, read_buff, READ_DATA_SIZE);
}
}
return 0;
}
int
send_data(int disk_fd)
{
//char *data = "1010101110101010111010101010100011100001111";
char *data = "00010001001000100100";
int len, i;
len = strlen(data);
for(i=0; i<len; i++)
send_bit(disk_fd, data[i]);
return 0;
}
int
main(int argc, char *argv[])
{
off_t drive_size;
int dfd = -1;
int ret = 0, read_bit;
long read_addr = 0;
if(argc < 2)
{
printf("Usage : target <drive-for-communication>\n");
return -1;
}
dfd = open(argv[1], O_RDWR);
if(dfd == -1)
{
perror("open");
return -1;
}
printf("Waiting ...\n");
sleep(15);
printf("----------------- Sending data over covert channel --------------------\n");
synchronize();
send_data(dfd);
return 0;
}