-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_fuz.h
272 lines (234 loc) · 6.14 KB
/
gen_fuz.h
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
#include<stdio.h>
#include<math.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <stdlib.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>
#include <bluetooth/rfcomm.h>
#include <bluetooth/sdp.h>
#include <bluetooth/sdp_lib.h>
#include <errno.h>
#define Maxsize 1500
#define CNT_MIN_LEN 7
#define DISCNT_MIN_LEN 3
#define PUT_MIN_LEN 3
#define GET_MIN_LEN 3
#define STPH_MIN_LEN 4
#define ABT_MIN_LEN 3
#define DEFAULT_MODE 1
void gen_buf(unsigned size,char buff[],unsigned mode,char c)
{
for (int i = 0; i < size; i++)
buff[i]=0;
pos:
switch (mode)
{
case 1://set rand data
for(int i=0;i<size;i++)
while(buff[i]==0)
buff[i]=rand();
break;
case 2://set 0
for(int i=0;i<size;i++)
buff[i]=0;
break;
case 3://set opposite
unsigned char mask=1;
unsigned char res=0;
if(size<=7&&size>=0)
{
mask=mask<<size;
buff[0]=(buff[0]&(~mask))|(buff[0]^mask);
}
break;
case 4://set given char
for(int i=0;i<size;i++)
buff[i]=c;
break;
default:
mode=DEFAULT_MODE;
goto pos;
}
}
void length_fuzz(int socket,int connectionId,int op)
{
char nameBuf[Maxsize]={0};
unsigned xx=pow(2,15)+1;
char sendBuf[xx];
char recvBuf[xx];
unsigned realLen=0;
int ieRl=0;
int minLen=0;
int len=0;
gen_buf(20,nameBuf,4,'A');
switch (op)
{
case OBEX_OP_PUT:
minLen=PUT_MIN_LEN;
break;
case OBEX_OP_SETPATH:
minLen=STPH_MIN_LEN;
break;
case OBEX_OP_CONNECT:
minLen=CNT_MIN_LEN;
break;
case OBEX_OP_DISCONNECT:
minLen=DISCNT_MIN_LEN;
break;
case OBEX_OP_GET:
minLen=GET_MIN_LEN;
break;
default:
break;
}
for(len=1;len<=minLen;len++)
{
realLen=0;
memset(sendBuf,0,xx);
ObexSetRequestPacket(sendBuf,&realLen,op);
ObexSetConnectionIdHeader(sendBuf+realLen,connectionId,&realLen);
ObexSetNameHeader(sendBuf+realLen,nameBuf,strlen(nameBuf),&realLen);
ObexSetlLengthHeader(sendBuf+realLen,6,&realLen);
ObexSetBodyHeader(sendBuf+realLen,"aaaaaaaaa",9,&realLen);
ObexSetRequestPacketLen(sendBuf,len);
send(socket,sendBuf,realLen,0);
printf("sending package %d \n",len);
// memset(recvBuf,0,OBEX_PACKET_MAX_LEN);
// ieRl=recv(socket,recvBuf,OBEX_PACKET_MAX_LEN,0);
// if(ieRl<0)
// {
// printf("Target down!");
// return;
// }
// else
// printf("have recived \n");
sleep(1);
}
}
void load_obex_namebuf(int socket,int connectionId,int op)
{
FILE*fp;
char nameBuf[Maxsize];
unsigned xx=pow(2,15)+1;
char sendBuf[xx];
char recvBuf[xx];
unsigned realLen=0;
int ieRl=0;
struct timeval timeout = {5,0}; //set timeout 5s
setsockopt(socket,SOL_SOCKET,SO_RCVTIMEO,(char *)&timeout,sizeof(struct timeval));
if ((fp = fopen("dir.txt", "r")) == NULL)
{
printf("Error! opening file");
// 文件指针返回 NULL 则退出
perror("open file");
}
for(int i=0;i<Maxsize;i++)
nameBuf[i]=0;
while(fscanf(fp,"%[^\n]",nameBuf)!=EOF)
{
printf("%s\n",nameBuf);
memset(sendBuf,0,xx);
realLen=0;
ObexSetRequestPacket(sendBuf,&realLen,op);
ObexSetConnectionIdHeader(sendBuf+realLen,connectionId,&realLen);
ObexSetNameHeader(sendBuf+realLen,nameBuf,strlen(nameBuf),&realLen);
ObexSetlLengthHeader(sendBuf+realLen,10,&realLen);
ObexSetBodyHeader(sendBuf+realLen,"aa",2,&realLen);
ObexSetRequestPacketLen(sendBuf,realLen);
send(socket,sendBuf,realLen,0);
fgetc(fp);//吸收掉最后的\n
for(int i=0;i<Maxsize;i++)
nameBuf[i]=0;
memset(recvBuf,0,OBEX_PACKET_MAX_LEN);
ieRl=recv(socket,recvBuf,OBEX_PACKET_MAX_LEN,0);
if(ieRl<0)
{
if(errno==EAGAIN){
printf("Target down!");
return;
}
}
sleep(0.5);
}
}
int overflow_fuzz(int socket,int connectionId,int op,int times)
{
//char nameBuf[]="\\Ebook";
char nameBuf[Maxsize]={0};
unsigned xx=pow(2,15)+1;
char sendBuf[xx];
unsigned realLen=0;
int ieRl=0;
fflush(stdout);
switch (op)
{
case OBEX_OP_SETPATH:
printf("[+]sending");
fflush(stdout);
for(int i=0;i<times;i++)
{
printf(".");
fflush(stdout);
gen_buf(1000,nameBuf,1,1);
memset(sendBuf,0,xx);
realLen=0;
ObexSetRequestPacket(sendBuf,&realLen,op);
ObexSetSetPathFlagConstant(sendBuf+realLen,&realLen);
ObexSetConnectionIdHeader(sendBuf+realLen,connectionId,&realLen);
ObexSetNameHeader(sendBuf+realLen,nameBuf,strlen(nameBuf),&realLen);
ObexSetRequestPacketLen(sendBuf,realLen);
ieRl=send(socket,sendBuf,realLen,0);
if(ieRl<0){
perror("\nconnection error:");
return -1;
}
for(int k=0;k<Maxsize;k++)
{nameBuf[k]=0;}
sleep(1);
}
break;
case OBEX_OP_PUT:
break;
default:
break;
}
return 1;
}
void wirte_dir(char sendBuf[])
{
FILE*fp;
if ((fp = fopen("dir.txt", "a+")) == NULL)
{
printf("Error! opening file");
perror("open file");
}
fputs(sendBuf,fp);
fputc('\n',fp);
fclose(fp);
}
void load_buff(int raw,char path[],char buff[],int buf_size)
{
FILE* fp;
int i;
if ((fp = fopen(path, "r")) == NULL)
{
printf("Error! opening file");
perror("open file");
}
for(int k=0;k<buf_size;k++)
buff[i]=0;
i=0;
while(fscanf(fp,"%[^\n]",*buff)!=EOF&&i<raw)
{
i++;
fgetc(fp);
}
}