-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdarwin_control_wrapper.m
254 lines (199 loc) · 7.57 KB
/
darwin_control_wrapper.m
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
/*
This file is part of Kismet
Kismet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Kismet is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Kismet; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define __IN_OBJC_FILE__
#include "config.h"
#ifdef SYS_DARWIN
// Is this a bad idea, to try to merge obj-c and c++ code into one codebase?
// Yes, probably.
// Code derived from kismac by KF
#import <Foundation/Foundation.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "apple80211.h"
#include <Carbon/Carbon.h>
#include "darwin_control_wrapper.h"
#import "darwin_wificontrol.h"
int darwin_bcom_testmonitor()
{
NSDictionary *dict;
NSData *fileData;
NSString *error;
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
fileData = [NSData dataWithContentsOfFile:@"/System/Library/PrivateFrameworks/AppleTV.framework/Versions/Current/Resources/Info.plist"];
dict = [NSPropertyListSerialization propertyListFromData:fileData mutabilityOption:kCFPropertyListImmutable format:NULL errorDescription:&error];
if(!dict)
{
NSLog(@"%s", error);
}
else
{
if (strcmp([[dict valueForKeyPath:@"CFBundleExecutable"] cString], "AppleTV") == 0) return 1;
}
// This may work on AppleTV 1.1 also but I am not sure. This is a quick hack to force 1.0 to work.
fileData = [NSData dataWithContentsOfFile:@"/System/Library/MonitorPanels/AppleDisplay.monitorPanels/Contents/Resources/TVOptions.monitorPanel/Contents/Info.plist"];
dict = [NSPropertyListSerialization propertyListFromData:fileData mutabilityOption:kCFPropertyListImmutable format:NULL errorDescription:&error];
if(!dict)
{
NSLog(@"%s", error);
}
else
{
if (strcmp([[dict valueForKeyPath:@"CFBundleExecutable"] cString], "TVOptions") == 0) return 1;
}
fileData = [NSData dataWithContentsOfFile:@"/System/Library/Extensions/AppleAirPort2.kext/Contents/Info.plist"];
dict = [NSPropertyListSerialization propertyListFromData:fileData mutabilityOption:kCFPropertyListImmutable format:NULL errorDescription:&error];
if(!dict)
{
NSLog(@"%s", error);
}
else
{
if ([[dict valueForKeyPath:@"IOKitPersonalities.Broadcom PCI.APMonitorMode"] boolValue]) return 1;
}
fileData = [NSData dataWithContentsOfFile:@"/System/Library/Extensions/IO80211Family.kext/Contents/PlugIns/AppleAirPortBrcm4311.kext/Contents/Info.plist"];
dict = [NSPropertyListSerialization propertyListFromData:fileData mutabilityOption:kCFPropertyListImmutable format:NULL errorDescription:&error];
if(!dict)
{
NSLog(@"%s", error);
}
else
{
if ([[dict valueForKeyPath:@"IOKitPersonalities.Broadcom PCI.APMonitorMode"] boolValue]) return 1;
}
return -1;
}
int darwin_bcom_enablemonitorfile(const char *c_filename)
{
NSDictionary *dict;
NSData *data;
NSString *fileName;
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
fileName = [[NSString alloc] initWithCString:c_filename];
if (chmod([fileName cString],
(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) < 0 &&
errno != ENOENT) {
return -1;
}
data = [NSData dataWithContentsOfFile:fileName];
if(!data) return 0;
dict = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:kCFPropertyListMutableContainers format:NULL errorDescription:Nil];
if(!dict) return 0;
[dict setValue:[NSNumber numberWithBool:true] forKeyPath:@"IOKitPersonalities.Broadcom PCI.APMonitorMode"];
[[NSPropertyListSerialization dataFromPropertyList:dict format:kCFPropertyListXMLFormat_v1_0 errorDescription:nil] writeToFile:fileName atomically:NO];
if (chmod([fileName cString],
(S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0 && errno != ENOENT) {
return -1;
}
return 1;
}
int darwin_bcom_enablemonitor()
{
int ret, i;
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
char cmd[1024];
ret = darwin_bcom_enablemonitorfile("/System/Library/Extensions/AppleAirPort2.kext/Contents/Info.plist") ||
darwin_bcom_enablemonitorfile("/System/Library/Extensions/IO80211Family.kext/Contents/PlugIns/AppleAirPortBrcm4311.kext/Contents/Info.plist");
if (ret == 0) {
return -1;
}
if (unlink("/System/Library/Extensions.kextcache") < 0 && errno != ENOENT)
return -1;
snprintf(cmd, 1024, "/usr/sbin/kextcache -k /System/Library/Extensions");
if (system(cmd) != 0)
return -1;
if (unlink("/System/Library/Extensions.mkext") < 0 && errno != ENOENT)
return -1;
/* Throw a warning at the user and wait */
fprintf(stderr, "ATTENTION: Kismet has enabled rfmon on your devices, however "
"to activate it, the kernel modules must be reloaded. There have "
"been reports of this causing a system crash. Kismet will wait 10 "
"seconds before attempting to reload the kernel modules. Press "
"control-c now to cancel reloading modules and reboot manually if "
"you do not want to proceed!\n\n");
sleep(10);
/* we don't check the failure codes since we don't know which driver we're
* using... Also according to geordi we have to thrash the unload because
* sometimes it just refuses to unload the module. Highly inelegant. */
for (i = 0; i < 10; i++) {
snprintf(cmd, 1024, "/sbin/kextunload -b com.apple.driver.AppleAirPort2"
">/dev/null 2>/dev/null");
system(cmd);
snprintf(cmd, 1024, "/sbin/kextunload -b "
"com.apple.driver.AppleAirPortBrcm4311 >/dev/null 2>/dev/null");
system(cmd);
}
/* Try to reload them */
snprintf(cmd, 1024, "/sbin/kextload /System/Library/Extensions/AppleAirPort2.kext"
">/dev/null 2>/dev/null");
system(cmd);
snprintf(cmd, 1024, "/sbin/kextload "
"/System/Library/Extensions/AppleAirPortBrcm4311.kext "
">/dev/null 2>/dev/null");
system(cmd);
fprintf(stderr, "ATTENTION: Completed trying to reload the kernel modules. "
"Sometimes this doesn't work, if Kismet does not start properly "
"you will need to manually reboot.\n");
sleep(5);
return 1;
}
void *darwin_allocate_interface(const char *in_iface) {
DarwinWifi *darwin = [[DarwinWifi alloc] initWithInterface:[[NSString alloc] initWithUTF8String:in_iface]];
return (void *) darwin;
}
void darwin_free_interface(void *in_darwin) {
// TODO
}
int darwin_set_channel(unsigned int in_channel, char *ret_err, void *in_darwin) {
DarwinWifi *darwin = (DarwinWifi *) in_darwin;
BOOL result;
NSError *err;
result = [darwin setChannel:in_channel error:ret_err];
if (!result) {
return -1;
}
return 1;
}
int darwin_get_corewifi(void *in_darwin) {
DarwinWifi *darwin = (DarwinWifi *) in_darwin;
return [darwin getCoreWireless];
}
void darwin_disassociate(void *in_darwin) {
DarwinWifi *darwin = (DarwinWifi *) in_darwin;
[darwin disAssociate];
}
int darwin_get_channels(const char *in_iface, int **ret_channels) {
NSArray *supported;
int *ret = NULL;
int x = 0;
DarwinWifi *darwin = [[DarwinWifi alloc] initWithInterface:[[NSString alloc] initWithUTF8String:in_iface]];
supported = [darwin getSupportedChannels];
if (supported == nil) {
*ret_channels = NULL;
return 0;
}
ret = (int *) malloc(sizeof(int) * [supported count]);
for (id sup in supported) {
ret[x++] = [sup intValue];
}
*ret_channels = ret;
x = [supported count];
return x;
}
#endif