-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbb.c
310 lines (253 loc) · 7.84 KB
/
bb.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (C) 2006 by Martin J. Muench <[email protected]>
*
* Bluebug utility
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include "bt.h"
#include "at.h"
#include "wrap.h"
#include "debug.h"
#define VERSION "1.1"
#define SAFE_DELETE(x) if(x) free(x);
/* local functions */
static void Usage(const char *);
static void execute_command(FILE *, int, char *[]);
static void sig_handler(int);
int opterr = 0; /* shutup getopt() */
FILE *output;
char *target_addr=NULL, *name=NULL, *device=NULL;
FILE *rfcomm_fp = NULL;
int sock=-1, dev_id=-1, hci_sock=-1;
/*
* Small wrapper to hack mobile phones (for dummies)
*/
int main(int argc, char *argv[])
{
int c, timeout=5000, name_lookup=1, channel=17;
char filename[FILENAME_MAX];
bdaddr_t bdtmp;
bdaddr_t *bdaddr;
/* default output = stdout */
output=stdout;
printf("\nbluebugger %s ( MaJoMu | www.codito.de )\n" \
"-----------------------------------------\n\n", VERSION);
if (geteuid() != 0)
Usage("You need (e)uid 0 (e.g. be root)\n");
while((c = getopt(argc, argv, "m:a:t:d:i:no:c:")) != -1) {
switch(c) {
case 'a':
target_addr = (char *)Malloc(strlen(optarg)+1);
strcpy(target_addr, optarg);
break;
case 'd':
device = (char *)Malloc(strlen(optarg)+1);
strcpy(device, optarg);
break;
case 'm':
name = (char *)Malloc(strlen(optarg)+1);
strcpy(name, optarg);
break;
case 't':
timeout = atoi(optarg) * 1000;
break;
case 'n':
name_lookup = 0;
break;
case 'c':
channel = atoi(optarg);
break;
case 'o':
if((output = fopen(optarg, "w+")) == NULL) {
fprintf(stderr, "Cannot open '%s': %s\n", optarg, strerror(errno));
exit(EXIT_FAILURE);
}
break;
default:
Usage("Invalid arguments");
}
}
/* get rid of parsed args */
argv += optind;
argc -= optind;
/* Get bt address */
if(target_addr == NULL)
Usage("You have to set the target address");
fprintf(output, "Target Device:\t'%s'\n", target_addr);
str2ba(target_addr, &bdtmp);
bdaddr = &bdtmp;
/* Get device ID */
if ((dev_id = hci_get_route(NULL)) == -1) {
fprintf(stderr, "hci_get_route() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
/* Set signal handler */
signal(SIGINT, sig_handler);
/* Setup socket */
if ((hci_sock = hci_open_dev(dev_id)) == -1) {
fprintf(stderr, "hci_open_dev() failed: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
/* Get target device name */
if(name_lookup) {
char asc_name[256]="";
hci_read_remote_name(hci_sock, bdaddr, sizeof(asc_name)-1, asc_name, timeout);
fprintf(output, "Target Name: \t'%s'\n\n", asc_name);
}
else
fprintf(output, "\n");
/* Configure hci socket */
bt_configure(dev_id, hci_sock, name);
/* Create rfcomm socket for communication */
if((sock = socket(AF_BLUETOOTH, SOCK_RAW, BTPROTO_RFCOMM)) == -1) {
fprintf(stderr, "socket() failed: %s\n", strerror(errno));
hci_close_dev(hci_sock);
exit(EXIT_SUCCESS);
}
/* bind rfcomm socket (create dev) */
if(bt_bind(sock, dev_id, bdaddr, channel)) {
fprintf(stderr, "bt_bind() failed: %s\n", strerror(errno));
hci_close_dev(hci_sock);
bt_release(sock, dev_id);
close(sock);
exit(EXIT_SUCCESS);
}
/* wait 1 sec to ensure dev is ready */
sleep(1);
/* Use rfcomm device */
snprintf(filename, FILENAME_MAX, "%s%u", device == NULL ? "/dev/rfcomm" : device, dev_id);
if (!(rfcomm_fp = fopen(filename, "r+"))) {
fprintf(stderr, "Cannot open '%s': %s\n", filename, strerror(errno));
hci_close_dev(hci_sock);
bt_release(sock, dev_id);
close(sock);
exit(EXIT_FAILURE);
}
/* Set rfcomm device options */
if (bt_rfcomm_config(fileno(rfcomm_fp)) == 0) {
/* execute main commands */
execute_command(rfcomm_fp, argc, argv);
}
else
fprintf(stderr, "bt_rfcomm_config() failed\n");
/* close rfcomm device */
fclose(rfcomm_fp);
/* Cleanup */
bt_release(sock, dev_id);
close(sock);
hci_close_dev(hci_sock);
/* Cleanup */
SAFE_DELETE(name);
SAFE_DELETE(device);
SAFE_DELETE(target_addr);
/* Close output file */
if(output != stdout)
fclose(output);
printf("...done\n\n");
exit(EXIT_SUCCESS);
}
static void execute_command(FILE *rfcomm_fp, int argc, char *argv[]) {
int i;
/* shut up */
at_disable_echo(rfcomm_fp);
/* Default = get info/phonebook/messages */
if(!argc) {
at_parse_manufacturer_identification(rfcomm_fp);
at_parse_identification(rfcomm_fp);
at_parse_phonebook_list(rfcomm_fp);
at_parse_message_list(rfcomm_fp);
return;
}
/* Process given commands */
for(i = 0; i < argc ; i++) {
debug(("+ Command: AT%s\n\n", argv[i]));
if(!strcmp(argv[i], "info")) {
at_parse_manufacturer_identification(rfcomm_fp);
at_parse_identification(rfcomm_fp);
}
else if(!strcmp(argv[i], "phonebook")) {
at_parse_phonebook_list(rfcomm_fp);
}
else if(!strcmp(argv[i], "messages")) {
at_parse_message_list(rfcomm_fp);
}
else if(!strcmp(argv[i], "dial")) {
if(i + 1 >= argc)
Usage("You need to specify the number for 'dial'");
at_dial(rfcomm_fp, argv[++i]);
}
/*
else if(!strcmp(argv[i], "sms")) {
if(i + 2 >= argc)
Usage("You need to specify the number and text for 'sms'");
at_sms(rfcomm_fp, argv[i + 1], argv[i + 2]);
i = i + 2;
}
*/
else {
at_custom(rfcomm_fp, argv[i]);
}
}
return;
}
/*
* Misc funcs
*/
/* Usage */
static void Usage(const char *err_msg)
{
printf("Usage: bluebugger [OPTIONS] -a <addr> [MODE] \n\n" \
" -a <addr> = Bluetooth address of target \n\n");
printf(" Options: \n" \
" -------- \n");
printf(" -m <name> = Name to use when connecting (default: '') \n" \
" -d <device> = Device to use (default: '/dev/rfcomm') \n" \
" -c <channel> = Channelto use (default: 17) \n" \
" -n = No device name lookup \n" \
" -t <timeout> = Timeout in seconds for name lookup (default: 5) \n" \
" -o <file> = Write output to <file> \n");
printf("\n");
printf(" Mode: \n" \
" ----- \n");
printf(" info = Read Phone Info (default) \n" \
" phonebook = Read Phonebook (default) \n" \
" messages = Read SMS Messages (default) \n" \
" dial <num> = Dial number \n" \
" ATCMD = Custom Command (e.g. '+GMI') \n");
printf(" \n");
printf(" Note: Modes can be combined, e.g. 'info phonebook +GMI' \n");
printf("\n* %s\n\n", err_msg);
exit(EXIT_FAILURE);
}
static void
sig_handler(int signo)
{
/* close rfcomm device */
if(rfcomm_fp)
fclose(rfcomm_fp);
/* Cleanup */
if(sock != -1 && dev_id != -1) {
bt_release(sock, dev_id);
close(sock);
}
if(hci_sock != -1)
hci_close_dev(hci_sock);
/* Cleanup */
SAFE_DELETE(name);
SAFE_DELETE(device);
SAFE_DELETE(target_addr);
exit(EXIT_FAILURE);
}
/* EOF */