-
Notifications
You must be signed in to change notification settings - Fork 1
/
v002PacketCapturePlugIn.m
224 lines (169 loc) · 6.1 KB
/
v002PacketCapturePlugIn.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
//
// v002PacketCapturePlugIn.m
// v002PacketCapture
//
// Created by vade on 6/20/08.
// Copyright (c) 2008 __MyCompanyName__. All rights reserved.
//
/* It's highly recommended to use CGL macros instead of changing the current context for plug-ins that perform OpenGL rendering */
#import <OpenGL/CGLMacro.h>
#import "v002PacketCapturePlugIn.h"
// OS X Security framework - do things the right way
#import <SecurityFoundation/SFAuthorization.h>
#import <SystemConfiguration/SystemConfiguration.h>
// for libpcap
// see http://www.tcpdump.org/pcap.htm
// and http://yuba.stanford.edu/~casado/pcap/section1.html
#import <stdio.h>
#import <stdlib.h>
#import <pcap.h>
#import <errno.h>
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>
#import <netinet/if_ether.h>
#import "v002PacketCaptureProtocol.h"
#import "DistributedPacket.h"
#define kQCPlugIn_Name @"v002 Packet Capture"
#define kQCPlugIn_Description @"v002 Packet Capture grabs IP data, source, destination, hostname info and outputs as a structure of strings"
@interface v002PacketCapturePlugIn ()
@property (atomic, readwrite, retain) id proxyObjectFromHelper; // our packet info from helper tool
@property (atomic, readwrite, retain) SFAuthorization* authorization;
@property (atomic, readwrite, assign) AuthorizationRef myAuthorizationRef; // for authorizing packet capturing.
@property (atomic, readwrite, assign) AuthorizationItem* authItems; // requested rights. need admin/root for raw socket/promiscuous mode. sounds sexy right?
@end
@implementation v002PacketCapturePlugIn
@dynamic inputInterfaceIndex;
@dynamic outputPacketInfo;
@dynamic outputIPList;
+ (NSDictionary*) attributes
{
return [NSDictionary dictionaryWithObjectsAndKeys:kQCPlugIn_Name, QCPlugInAttributeNameKey, kQCPlugIn_Description, QCPlugInAttributeDescriptionKey, nil];
}
+ (NSDictionary*) attributesForPropertyPortWithKey:(NSString*)key
{
NSMutableArray * deviceNames = [NSMutableArray new];
NSArray* devices = (NSArray*) SCNetworkInterfaceCopyAll();
for(id intefrace in devices)
{
[deviceNames addObject:(NSString*)SCNetworkInterfaceGetBSDName((SCNetworkInterfaceRef)intefrace)];
}
[devices release];
[deviceNames autorelease];
if([key isEqualToString:@"inputInterfaceIndex"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Interface Device", QCPortAttributeNameKey,
[NSNumber numberWithUnsignedInt:0], QCPortAttributeDefaultValueKey,
[NSNumber numberWithUnsignedInt:[deviceNames count] - 1], QCPortAttributeMaximumValueKey,
deviceNames, QCPortAttributeMenuItemsKey,
nil];
}
if([key isEqualToString:@"outputPacketInfo"])
{
return [NSDictionary dictionaryWithObjectsAndKeys:@"Captured Packet Info", QCPortAttributeNameKey, nil];
}
if([key isEqualToString:@"outputIPList"])
return @{QCPortAttributeNameKey: @"IP List"};
return nil;
}
+ (QCPlugInExecutionMode) executionMode
{
return kQCPlugInExecutionModeProvider;
}
+ (QCPlugInTimeMode) timeMode
{
return kQCPlugInTimeModeIdle;
}
- (id) init
{
if(self = [super init])
{
}
return self;
}
- (void) finalize
{
[self.authorization invalidateCredentials];
[super finalize];
}
- (void) dealloc
{
[self.authorization invalidateCredentials];
[super dealloc];
}
- (BOOL) testPcap
{
// launch our background process as root somehow.
NSString* pathToHelperTool = [[NSBundle bundleForClass:[self class]] pathForResource:@"v002PacketCaptureHelperTool" ofType:nil];
NSLog(@"Helper tool path is: %@", pathToHelperTool);
FILE *myCommunicationsPipe = NULL;
char *myArguments[] = {NULL};
// char path[[pathToHelperTool length] +1];
// strcmp(path, [pathToHelperTool lossyCString]);
// this needs to be looked at
OSStatus myStatus = AuthorizationExecuteWithPrivileges(self.authorization.authorizationRef, [pathToHelperTool cStringUsingEncoding:NSASCIIStringEncoding], kAuthorizationFlagDefaults, myArguments, &myCommunicationsPipe);
if (myStatus == errAuthorizationSuccess)
{
NSLog(@"Launched helper tool - connecting to Shared Port for IPC");
return YES;
}
else
NSLog(@"Error: %i", myStatus);
return NO;
}
@end
@implementation v002PacketCapturePlugIn (Execution)
- (BOOL) startExecution:(id<QCPlugInContext>)context
{
NSError* error;
// SFAuthorization *auth = [SFAuthorization authorizationWithFlags:kAuthorizationFlagInteractionAllowed|kAuthorizationFlagExtendRights|kAuthorizationFlagPreAuthorize
// rights:NULL
// environment:kAuthorizationEmptyEnvironment];
self.authorization = [SFAuthorization authorization];
if(![self.authorization obtainWithRight:"info.v002.v002PacketCapture.init"
flags:kAuthorizationFlagExtendRights
//kAuthorizationFlagExtendRights | //kAuthorizationFlagInteractionAllowed// | kAuthorizationFlagPreAuthorize
error:&error])
{
AuthorizationExternalForm authExtForm;
OSStatus status = AuthorizationMakeExternalForm(self.authorization.authorizationRef, &authExtForm); // 4
if (errAuthorizationSuccess == status)
{
NSLog(@"SUCESS - we authorized some shit to do..");
if([self testPcap])
NSLog(@"Was able to test pcap!!!");
else
NSLog(@"failed testing pcap:(");
}
else
NSLog(@"Denied!");
}
else
NSLog(@"Not a fucking chance: %@", error);
return YES;
}
- (void) enableExecution:(id<QCPlugInContext>)context
{
}
- (BOOL) execute:(id<QCPlugInContext>)context atTime:(NSTimeInterval)time withArguments:(NSDictionary*)arguments
{
if(!self.proxyObjectFromHelper)
{
NSConnection* theConnection = [NSConnection connectionWithRegisteredName:@"info.vade.packetCaptureHelperTool" host:nil];
self.proxyObjectFromHelper = [[theConnection rootProxy] retain];
NSLog(@"Proxy description %@", self.proxyObjectFromHelper);
[self.proxyObjectFromHelper setProtocolForProxy:@protocol(v002PacketCaptureProtocol)];
}
self.outputPacketInfo = [self.proxyObjectFromHelper packetArray];
self.outputIPList = [self.proxyObjectFromHelper ipArray];
return YES;
}
- (void) disableExecution:(id<QCPlugInContext>)context
{
}
- (void) stopExecution:(id<QCPlugInContext>)context
{
[self.proxyObjectFromHelper quitHelperTool];
[self.authorization invalidateCredentials];
}
@end