-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnative_host.py
executable file
·192 lines (164 loc) · 5.51 KB
/
native_host.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
#!/usr/bin/env python3
import sys
import json
import struct
import subprocess
import tempfile
import queue
import threading
import requests
import os
import hashlib
from urllib.parse import urlparse
# Global queues and caches
url_queue = queue.Queue()
result_queue = queue.Queue()
url_cache = set() # Store URL hashes
result_cache = {} # Store content hash -> findings mapping
# save all stderr to a file
sys.stderr = open('/tmp/webtrufflehog.log', 'w')
# Helper function to send messages to the extension
def send_message(message):
try:
encoded_content = json.dumps(message).encode('utf-8')
sys.stdout.buffer.write(struct.pack('I', len(encoded_content)))
sys.stdout.buffer.write(encoded_content)
sys.stdout.buffer.flush()
except Exception as e:
print(f"Error sending message: {str(e)}", file=sys.stderr)
# Helper function to read messages from the extension
def read_message():
try:
raw_length = sys.stdin.buffer.read(4)
if not raw_length:
return None
message_length = struct.unpack('I', raw_length)[0]
message = sys.stdin.buffer.read(message_length).decode('utf-8')
return json.loads(message)
except Exception:
return None
def get_url_hash(url):
return hashlib.md5(url.encode()).hexdigest()
def get_content_hash(content):
return hashlib.md5(content.encode()).hexdigest()
def download_url(url):
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
return response.text
except Exception as e:
print(f"Error downloading {url}: {str(e)}", file=sys.stderr)
return None
def scan_with_trufflehog(content, temp_dir):
if not content:
return []
temp_file = os.path.join(temp_dir, 'scan_target.txt')
try:
with open(temp_file, 'w') as f:
f.write(content)
result = subprocess.run(
['trufflehog', 'filesystem', temp_file, '--json'],
capture_output=True,
text=True
)
findings = [json.loads(line) for line in result.stdout.splitlines() if line.strip()]
return findings
except Exception as e:
print(f"Error scanning with trufflehog: {str(e)}", file=sys.stderr)
return []
finally:
try:
if os.path.exists(temp_file):
os.remove(temp_file)
except Exception as e:
print(f"Error removing temp file: {str(e)}", file=sys.stderr)
def worker():
while True:
temp_dir = None
try:
temp_dir = tempfile.mkdtemp()
job = url_queue.get()
if job is None: # Poison pill
break
url = job['url']
job_id = job['id']
# Check URL cache
url_hash = get_url_hash(url)
if url_hash in url_cache:
url_queue.task_done()
continue
content = download_url(url)
if content:
content_hash = get_content_hash(content)
# Check result cache
if content_hash in result_cache:
findings = result_cache[content_hash]
else:
findings = scan_with_trufflehog(content, temp_dir)
result_cache[content_hash] = findings
if findings:
result_queue.put({
'id': job_id,
'url': url,
'findings': findings
})
url_cache.add(url_hash)
url_queue.task_done()
except Exception as e:
print(f"Worker error: {str(e)}", file=sys.stderr)
finally:
try:
if temp_dir and os.path.exists(temp_dir):
os.rmdir(temp_dir)
except Exception as e:
print(f"Error removing temp dir: {str(e)}", file=sys.stderr)
def append_result(result,filename):
with open(filename, 'a') as f:
f.write(json.dumps(result) + '\n')
def result_sender():
while True:
try:
result = result_queue.get()
if result is None: # Poison pill
break
append_result(result, '/tmp/results.json')
send_message(result)
result_queue.task_done()
except Exception as e:
print(f"Result sender error: {str(e)}", file=sys.stderr)
def main():
# Start worker threads
num_workers = 10
workers = []
for _ in range(num_workers):
t = threading.Thread(target=worker)
t.daemon = True
t.start()
workers.append(t)
# Start result sender thread
sender = threading.Thread(target=result_sender)
sender.daemon = True
sender.start()
try:
while True:
message = read_message()
if not message:
break
if 'url' in message:
url_queue.put({
'id': message.get('id'),
'url': message['url']
})
if 'status' in message:
# return number of queue items
send_message({'status': url_queue.qsize()})
finally:
# Clean shutdown
for _ in workers:
url_queue.put(None)
result_queue.put(None)
for w in workers:
w.join()
sender.join()
if __name__ == "__main__":
main()