forked from Pirrandi/htb-presence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htb-presence.py
247 lines (214 loc) · 9.4 KB
/
htb-presence.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
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
#!/usr/bin/env python3
## htb-presence.py - RichPresence for HackTheBox on Discord
## Author: @Pirrandi (https://github.com/Pirrandi)
## Translator: @wh0crypt (https://github.com/wh0crypt)
## Additional Contributions: @sealldev (https://github.com/sealldeveloper)
from pypresence import Presence
import psutil
import requests
import time
import os
import sys
import atexit
import traceback
from dotenv import load_dotenv
from tempfile import gettempdir
from pathlib import Path
from platform import system
# Load environment variables
load_dotenv()
# Load appropriate language
lang = os.getenv('LANGUAGE') if os.getenv('LANGUAGE') else 'EN' # default is english
if lang == 'EN':
from translations.en import *
elif lang == 'ES':
from translations.es import *
lock_file = os.path.join(Path("/tmp" if system() == "Darwin" else gettempdir()),'test.py.lock')
def acquire_lock():
if os.path.exists(lock_file):
try:
with open(lock_file, 'r') as f:
pid = int(f.read())
if pid_exists(pid):
print(another_instance_running_str)
sys.exit(1)
else:
os.remove(lock_file)
except Exception:
pass
with open(lock_file, 'w') as f:
f.write(str(os.getpid()))
def release_lock():
if os.path.exists(lock_file):
os.remove(lock_file)
def pid_exists(pid):
try:
os.kill(pid, 0)
return True
except OSError:
return False
if __name__ == "__main__":
atexit.register(release_lock)
acquire_lock()
# HackTheBox and Discord APIs configuration
client_id = os.getenv('CLIENT_ID') if os.getenv('CLIENT_ID') else '1125543074861432864' # default is '1125543074861432864'
htb_api_token = os.getenv('HTB_API_TOKEN') if os.getenv('HTB_API_TOKEN') else None
if not htb_api_token or htb_api_token == 'HTB_TOKEN_HERE':
print(htb_api_token_not_set)
sys.exit()
RPC_status=0
RPC = Presence(client_id)
connection=0
test=1
while test==1:
def is_discord_open():
for process in psutil.process_iter(attrs=['pid', 'name']):
if 'discord' in process.info['name'].lower():
return 1
return 0
discord_status= is_discord_open()
print(discord_status)
if is_discord_open():
print(discord_running_str)
else:
print(discord_not_running_str)
while is_discord_open():
# HackTheBox API configuration
htb_machine_api = 'https://www.hackthebox.com/api/v4/machine/active'
htb_user_api = 'https://www.hackthebox.com/api/v4/user/info'
htb_connection_api = 'https://www.hackthebox.com/api/v4/user/connection/status'
headers = {
'User-Agent': 'HTB Discord Rich Presence',
'Authorization': f'Bearer {htb_api_token}'
}
# Media
htb_logo = 'https://yt3.googleusercontent.com/ytc/AOPolaR5R7bueWAUHc7ctRNCy5r63xddkeL17RDHOwxAlw=s900-c-k-c0x00ffffff-no-rj'
buttons = [
{
'label': label1_str,
'url': url1_str
},
{
'label': label2_str,
'url': url2_str
}
]
def closeDiscord_clearRPC_status():
global RPC_status
if discord_status==0:
RPC_status=0
# Variable that stores the Active Machine's name
active_machine_name = None
# Loop for continuous state update and verification
start_time=1
while is_discord_open:
try:
is_discord_open()
time.sleep(1)
closeDiscord_clearRPC_status()
# Retrieve the Active Machine's information from HackTheBox
response_machine = requests.get(htb_machine_api, headers=headers)
response_user = requests.get(htb_user_api, headers=headers)
response_connection = requests.get(htb_connection_api, headers=headers)
if RPC_status == 0:
print(connecting_rpc_str)
RPC.connect()
RPC_status=1
print(connected_rpc_str)
if response_machine.status_code == 200:
data_machine = response_machine.json()
data_user = response_user.json()
data_connection = response_connection.json()
connection = data_connection['status']
if data_machine:
user = data_user['info']
user_nickname = user['name']
user_avatar = user['avatar']
user_avatar = f"https://www.hackthebox.com{user['avatar']}"
print(user_info_retrieved_str)
print(discord_status)
print(RPC_status)
if discord_status==1 and connection == True and RPC_status == 1 and active_machine_name == None:
print(updating_rp_str)
RPC.update(
details=connected_htb_str,
state=waiting_state_str,
large_image=htb_logo,
large_text="Hack The Box",
small_image=user_avatar,
small_text=user_nickname,
buttons=buttons
)
machine = data_machine['info']
machine_name = machine['name']
machine_avatar = machine['avatar']
machine_avatar = f"https://www.hackthebox.com{machine['avatar']}"
print(machine_info_retrieved_str)
###
htb_get_api = f"https://www.hackthebox.com/api/v4/profile/activity/{user['id']}"
response_activity = requests.get(htb_get_api, headers=headers)
data_activity = response_activity.json()
print(apis_connected_str)
pwned = "🟢"
no_pwned = "🔴"
has_root = False
has_user = False
for record in data_activity["profile"]["activity"]:
if record["name"] == machine_name:
if record["type"] == "root":
has_root = True
elif record["type"] == "user":
has_user = True
if has_root == True:
root_flag = pwned
else:
root_flag = no_pwned
if has_user == True:
user_flag = pwned
else:
user_flag = no_pwned
print(machine_found_str)
RPC.update(
details=machine_str+machine_name,
large_image=machine_avatar,
large_text="Hack The Box",
small_image=user_avatar,
small_text=user_nickname,
state=f"User: {user_flag} | Root: {root_flag}"
)
# Check if the machine has changed
if machine_name != active_machine_name:
active_machine_name = machine_name
start_time = int(time.time())
# Update RichPresence's state
RPC.update(
details=machine_str+machine_name,
large_image=machine_avatar,
large_text="Hack The Box",
small_image=user_avatar,
small_text=user_nickname,
state=f"User: {user_flag} | Root: {root_flag}"
)
else:
active_machine_name = None
print(cleaning_rpc_str)
RPC.clear()
else:
print(request_error_str+response.status_code)
except Exception as e:
print(active_machine_warning_str)
active_machine_name = None
def is_discord_open():
for process in psutil.process_iter(attrs=['pid', 'name']):
if 'discord' in process.info['name'].lower():
return 1
return 0
discord_status= is_discord_open()
if discord_status==1 and connection==False:
print(cleaning_rpc_str)
RPC.clear()
continue
if discord_status==0:
print(discord_not_running_str)
continue
release_lock()