-
Notifications
You must be signed in to change notification settings - Fork 0
/
blueblue.py
242 lines (196 loc) · 7.14 KB
/
blueblue.py
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
#!/usr/bin/python
import getopt
import sys
import os
import bluetooth
vernum = "0.1beta"
debug = False
verbose = False
listdevices = False
known_devices=[]
# Print help information and exit:
def version():
print "+----------------------------------------------------------------------+"
print "| "+ sys.argv[0] + " Version "+ vernum +" |"
print "| This program is free software; you can redistribute it and/or modify |"
print "| it under the terms of the GNU General Public License as published by |"
print "| the Free Software Foundation; either version 2 of the License, or |"
print "| (at your option) any later version. |"
print "| |"
print "| Author: Veronica Valeros, vero dot valeros at gmail dot com |"
print "+----------------------------------------------------------------------+"
print
# Print help information and exit:
def usage():
"""
This function prints the posible options of this program.
No parameters are needed.
"""
print "+----------------------------------------------------------------------+"
print "| "+ sys.argv[0] + " Version "+ vernum +" |"
print "| This program is free software; you can redistribute it and/or modify |"
print "| it under the terms of the GNU General Public License as published by |"
print "| the Free Software Foundation; either version 2 of the License, or |"
print "| (at your option) any later version. |"
print "| |"
print "| Author: Veronica Valeros, [email protected] |"
print "+----------------------------------------------------------------------+"
print
print "\nUsage: %s <options>" % sys.argv[0]
print "Options:"
print " -h, --help Show this help message and exit"
print " -V, --version Output version information and exit"
print " -v, --verbose Be verbose"
print " -D, --debug Debug"
print " -f, --filename Filename to send."
print " -l, --listdevices Just list devices, no send any file."
print
print
sys.exit(1)
def search_channel():
"""
This function searches the channel of a device in the tmp file generated by the previous executed command sdptool.
Without this channel is difficult to invocate the obexftp command to send files.
"""
global debug
global verbose
try:
data = open('tmp','r')
line = data.readline()
while line:
if 'OBEX Object Push' in line:
while line:
if 'Channel' in line:
channel = line.split(': ')[1]
return str(channel)
else:
line = data.readline()
else:
line = data.readline()
return -1
except KeyboardInterrupt:
# CTRL-C pretty handling
print 'Keyboard Interruption!. Exiting.'
sys.exit(1)
except Exception as inst:
print '[!] Exception in main() function'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # __str__ allows args to printed directly
x, y = inst # __getitem__ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
def bluecast(filename):
"""
This function list near devices using blutooth and send them a given file if it is given by the user.
"""
global debug
global verbose
global listdevices
global known_devices
try:
listdev = []
devs = []
# Discovering devices
listdev = bluetooth.discover_devices(lookup_names=True)
if debug:
print "[+] Discovering devices..."
print '- devices: {}'.format(listdev)
# Printing information
print '+------------------------------------------------+'
print '|NAME\t\t BDADDR\t\t\t CHANNEL |'
print '+------------------------------------------------+'
for [device,name] in listdev:
# Retrieving device channel with sdptool
os.system('sdptool browse '+device+'> tmp')
channel = search_channel()
os.system('rm tmp')
if (len(name) > 7 ):
print "{}\t {}\t {}".format(name,device,channel.strip('\n'))
else:
print "{}\t\t {}\t {}".format(name,device,channel.strip('\n'))
devs.append([device,name,channel.strip('\n')])
if not listdevices and len(devs)>0:
print 'Sending file....'
for [device,name,channel] in devs:
if [device,name,channel] not in known_devices:
print
print
print '[+] {} ({})'.format(device,name)
print ' File: {}'.format(filename)
print ' Command: obexftp --nopath --noconn --uuid none --bluetooth '+device+' --channel '+channel+' -p '+filename
status = os.system('obexftp --nopath --noconn --uuid none --bluetooth '+device+' --channel '+channel+' -p '+filename)
retval, sig = ((status >> 8) & 0xFF), (status & 0xFF)
if debug:
print '\nretval: {}, sig: {}'.format(retval,sig)
if retval == 255:
known_devices.append([device,name,channel])
print
print 'Succeded!'
else:
print
print 'Failed!'
else:
print 'Ignoring device {} ({})'.format(device,name)
print
print
except KeyboardInterrupt:
# CTRL-C pretty handling
print 'Keyboard Interruption!. Exiting.'
return -2
except Exception as inst:
print '[!] Exception in main() function'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # __str__ allows args to printed directly
x, y = inst # __getitem__ allows args to be unpacked directly
print 'x =', x
print 'y =', y
return -1
def main():
"""
blueblue.py is a tool for bluetooth marketing and sharing. It allows to broadcast files in the bluetooth neighborhood.
Example: blueblue.py -f image.jpg
"""
global debug
global verbose
global listdevices
filename = ""
try:
opts, args = getopt.getopt(sys.argv[1:], "hVdvf:l", ["help","version","debug","verbose","file","list-devices"])
except getopt.GetoptError: usage()
for opt, arg in opts:
if opt in ("-h", "--help"): usage()
if opt in ("-V", "--version"): version();sys.exit(1)
if opt in ("-D", "--debug"): debug=True
if opt in ("-v", "--verbose"): verbose=True
if opt in ("-f", "--file"): filename=arg
if opt in ("-l", "--list-devices"): listdevices=True
try:
version()
print "blueblue.py is a tool for bluetooth marketing and sharing."
print
if not filename and not listdevices:
print 'You must specify a file to send or use the --list-devices option instead.'
sys.exit(1)
while True:
code = bluecast(filename)
if code == -2:
sys.exit(1)
except KeyboardInterrupt:
# CTRL-C pretty handling
print
print 'Bye'
sys.exit(1)
except Exception as inst:
print '[!] Exception in main() function'
print type(inst) # the exception instance
print inst.args # arguments stored in .args
print inst # __str__ allows args to printed directly
x, y = inst # __getitem__ allows args to be unpacked directly
print 'x =', x
print 'y =', y
sys.exit(1)
if __name__ == '__main__':
main()