File size: 11,178 Bytes
12d42ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import re
import json
import time
import asyncio
import aiohttp
import requests
import instaloader
import face_recognition
import numpy as np
from PIL import Image
from io import BytesIO
from typing import Dict, List, Any, Union
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from holehe.core import *
from sherlock import sherlock
from geopy.geocoders import Nominatim
from waybackpy import WaybackMachineCDXServerAPI
import phonenumbers
from phonenumbers import geocoder, carrier, timezone
import whois
from datetime import datetime

class OSINTEngine:
    """OSINT capabilities for advanced information gathering"""
    
    def __init__(self):
        self.chrome_options = Options()
        self.chrome_options.add_argument('--headless')
        self.chrome_options.add_argument('--no-sandbox')
        self.chrome_options.add_argument('--disable-dev-shm-usage')
        self.setup_apis()

    def setup_apis(self):
        """Initialize API clients"""
        self.instagram = instaloader.Instaloader()
        self.geolocator = Nominatim(user_agent="intelligent_search")

    async def search_username(self, username: str) -> Dict[str, Any]:
        """Search for username across multiple platforms"""
        results = {}
        
        # Sherlock search
        sherlock_results = await self.sherlock_search(username)
        results['platforms'] = sherlock_results
        
        # Email search
        email_results = await self.search_email(f"{username}@gmail.com")
        results['email'] = email_results
        
        return results

    async def sherlock_search(self, username: str) -> List[Dict[str, str]]:
        """Search username using Sherlock"""
        results = []
        sites = sherlock.site_data()
        
        async with aiohttp.ClientSession() as session:
            tasks = []
            for site_name, site_data in sites.items():
                task = self.check_username(session, username, site_name, site_data)
                tasks.append(task)
            
            results = await asyncio.gather(*tasks)
            return [r for r in results if r is not None]

    async def check_username(self, session, username: str, site_name: str, site_data: Dict) -> Dict[str, str]:
        """Check username on a specific platform"""
        url = site_data.get('url', '').format(username=username)
        if not url:
            return None
            
        try:
            async with session.get(url) as response:
                if response.status == 200:
                    return {
                        'platform': site_name,
                        'url': url,
                        'found': True
                    }
        except:
            pass
        return None

    async def search_email(self, email: str) -> Dict[str, Any]:
        """Search for email presence on various platforms"""
        results = {}
        modules = get_functions()
        
        for module in modules:
            try:
                out = await module(email)
                if out:
                    results[module.__name__] = out
            except:
                continue
                
        return results

    async def search_image(self, image_url: str) -> Dict[str, Any]:
        """Reverse image search and face recognition"""
        results = {}
        
        try:
            # Download image
            response = requests.get(image_url)
            img = Image.open(BytesIO(response.content))
            
            # Convert to face_recognition format
            img_array = np.array(img)
            face_locations = face_recognition.face_locations(img_array)
            face_encodings = face_recognition.face_encodings(img_array, face_locations)
            
            results['faces_found'] = len(face_locations)
            results['face_locations'] = face_locations
            
            # Perform reverse image search
            results['reverse_search'] = await self.reverse_image_search(image_url)
            
        except Exception as e:
            results['error'] = str(e)
            
        return results

    async def reverse_image_search(self, image_url: str) -> List[Dict[str, str]]:
        """Perform reverse image search"""
        results = []
        
        try:
            driver = webdriver.Chrome(
                service=Service(ChromeDriverManager().install()),
                options=self.chrome_options
            )
            
            # Google Images
            search_url = f"https://lens.google.com/uploadbyurl?url={image_url}"
            driver.get(search_url)
            time.sleep(3)
            
            # Extract results (simplified)
            results.append({
                'source': 'Google Lens',
                'url': driver.current_url
            })
            
            driver.quit()
            
        except Exception as e:
            results.append({'error': str(e)})
            
        return results

    async def gather_personal_info(self, data: Dict[str, str]) -> Dict[str, Any]:
        """Gather personal information from various sources"""
        results = {}
        
        if 'phone' in data:
            results['phone'] = self.analyze_phone_number(data['phone'])
            
        if 'location' in data:
            results['location'] = await self.analyze_location(data['location'])
            
        if 'domain' in data:
            results['domain'] = self.analyze_domain(data['domain'])
            
        return results

    def analyze_phone_number(self, phone: str) -> Dict[str, Any]:
        """Analyze phone number information"""
        try:
            number = phonenumbers.parse(phone)
            return {
                'valid': phonenumbers.is_valid_number(number),
                'type': phonenumbers.number_type(number),
                'country': geocoder.description_for_number(number, "en"),
                'carrier': carrier.name_for_number(number, "en"),
                'timezone': timezone.time_zones_for_number(number)
            }
        except Exception as e:
            return {'error': str(e)}

    async def analyze_location(self, location: str) -> Dict[str, Any]:
        """Analyze location information"""
        try:
            location_data = self.geolocator.geocode(location)
            if location_data:
                return {
                    'address': location_data.address,
                    'latitude': location_data.latitude,
                    'longitude': location_data.longitude,
                    'raw': location_data.raw
                }
        except Exception as e:
            return {'error': str(e)}
        return None

    def analyze_domain(self, domain: str) -> Dict[str, Any]:
        """Analyze domain information"""
        try:
            domain_info = whois.whois(domain)
            return {
                'registrar': domain_info.registrar,
                'creation_date': domain_info.creation_date,
                'expiration_date': domain_info.expiration_date,
                'last_updated': domain_info.updated_date,
                'status': domain_info.status
            }
        except Exception as e:
            return {'error': str(e)}

    async def search_social_media(self, username: str, platform: str = None) -> Dict[str, Any]:
        """Search for user information on social media platforms"""
        results = {}
        
        if platform:
            platforms = [platform]
        else:
            platforms = ['instagram', 'twitter', 'reddit']
            
        for platform in platforms:
            try:
                if platform == 'instagram':
                    results['instagram'] = await self.search_instagram(username)
                elif platform == 'twitter':
                    results['twitter'] = await self.search_twitter(username)
                elif platform == 'reddit':
                    results['reddit'] = await self.search_reddit(username)
            except Exception as e:
                results[platform] = {'error': str(e)}
                
        return results

    async def search_instagram(self, username: str) -> Dict[str, Any]:
        """Search Instagram for user information"""
        try:
            profile = instaloader.Profile.from_username(self.instagram.context, username)
            return {
                'username': profile.username,
                'full_name': profile.full_name,
                'biography': profile.biography,
                'followers': profile.followers,
                'following': profile.followees,
                'is_private': profile.is_private,
                'is_verified': profile.is_verified,
                'external_url': profile.external_url,
                'posts_count': profile.mediacount,
                'profile_pic_url': profile.profile_pic_url
            }
        except Exception as e:
            return {'error': str(e)}

    async def search_historical_data(self, url: str) -> List[Dict[str, Any]]:
        """Search for historical data using Wayback Machine"""
        results = []
        
        try:
            user_agent = "Mozilla/5.0"
            cdx = WaybackMachineCDXServerAPI(url, user_agent)
            
            for snapshot in cdx.snapshots():
                results.append({
                    'timestamp': snapshot.timestamp,
                    'url': snapshot.archive_url,
                    'status': snapshot.status_code,
                    'mime_type': snapshot.mime_type
                })
                
        except Exception as e:
            results.append({'error': str(e)})
            
        return results

    def solve_captcha(self, image_url: str) -> str:
        """Solve CAPTCHA using OCR (simplified version)"""
        try:
            response = requests.get(image_url)
            img = Image.open(BytesIO(response.content))
            # Add your CAPTCHA solving logic here
            return "CAPTCHA solution placeholder"
        except Exception as e:
            return f"Error: {str(e)}"

# Helper function to create document from gathered information
def create_report(data: Dict[str, Any], template: str = "default") -> str:
    """Create a formatted report from gathered information"""
    if template == "default":
        report = "# OSINT Investigation Report\n\n"
        report += f"Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
        
        for section, content in data.items():
            report += f"## {section.title()}\n"
            if isinstance(content, dict):
                for key, value in content.items():
                    report += f"* {key}: {value}\n"
            elif isinstance(content, list):
                for item in content:
                    report += f"* {item}\n"
            else:
                report += f"{content}\n"
            report += "\n"
            
        return report
    else:
        raise ValueError(f"Template '{template}' not found")