Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# THMBot (For Educational Purpose Only)
# THMBot
## TryHackMe bot for maintaining your streak

[![Python 3.10.4](https://img.shields.io/badge/python-3.10.4-yellow.svg)](https://www.python.org/)
Expand Down Expand Up @@ -101,4 +101,4 @@ When THMBot runs, it logs actions and progress in tryhackmebot.log:
- To uninstall you can use the uninstall.bat script for Windows systems and the uninstall.sh as sudo for Linux systems.<br />Those scripts will revert the project back to the point of cloning, then you can delete the project's folder.
- the scheduled task or Cron Job will not work if you move the project's folder after setup.<br />
If you need to change the location, please use uninstall.bat/sh and run setup.bat/sh after the location has changed.
- Currently you have to join this [room](https://tryhackme.com/room/polkit) to be able to run THMBot (will be fixed soon).
- Currently you have to join this [room](https://tryhackme.com/room/polkit) to be able to run THMBot (will be fixed soon).
49 changes: 49 additions & 0 deletions browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import subprocess, platform, time, os
from selenium import webdriver
from selenium.webdriver.firefox.service import Service as ffService
from selenium.webdriver.chrome.service import Service as chService
from webdriver_manager.firefox import GeckoDriverManager as gdm
from webdriver_manager.chrome import ChromeDriverManager as cdm

os_family = platform.uname().system
if os_family == "Linux":
s = ["sh",f"{os.getcwd()}/id_browser.sh"]
elif os_family == "Windows":
s = ["powershell", f"{os.getcwd()}\id_browser.ps1"]

def get_browser_bin(command):
with open("f", 'w') as file:
p = subprocess.Popen(command, stdout=file)
time.sleep(2)
file.close()
with open("f", 'r') as file:
e = file.readline().strip()
file.close()
try:
os.remove("f")
return e
except FileNotFoundError:
pass
except PermissionError:
pass


browser_exec = get_browser_bin(s)
print(browser_exec)

def get_driver():
if "chrome" in browser_exec:
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--mute-audio")
driver = webdriver.Chrome(browser_exec, service=chService(cdm(path=os.getcwd()).install()), options=chrome_options)
elif "firefox" in browser_exec:
firefox_options = webdriver.FirefoxOptions()
firefox_options.add_argument('--headless')
firefox_options.set_preference("media.volume_scale", "0.0")
firefox_options.set_preference("dom.push.enabled", False)
driver = webdriver.Firefox(firefox_binary=browser_exec, service=ffService(gdm(path=os.getcwd()).install()), options=firefox_options)
else:
print("Could not create webdriver for browser.")
return driver

15 changes: 15 additions & 0 deletions browser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
## detects installed browser and returns location - only chrome & firefox supported
got_chrome=$(which chrome 2>/dev/null)
if [[ $? -ne 0 ]]; then
got_ffx=$(which firefox 2>/dev/null)
if [[ $? -ne 0 ]]; then
exit 1
else
echo $got_ffx
fi
else
echo $got_chrome
fi
exit 1

5 changes: 5 additions & 0 deletions id_browser.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# detects default nstalled browser and returns location - only chrome & firefox supported

$browser = (Get-ItemProperty HKCU:\Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http\UserChoice -Name ProgId).ProgId
$b_location = (Get-ItemProperty HKLM:\SOFTWARE\Classes\$browser\DefaultIcon).'(default)'.Split(",")[0]
Write-Output $b_location
14 changes: 14 additions & 0 deletions id_browser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# detects installed browser and returns location - only chrome & firefox supported
got_chrome=$(which chrome)
if [[ $? -ne 0 ]]; then
got_ffx=$(which firefox)
if [[ $? -ne 0 ]]; then
exit 1
else
echo $got_ffx
fi
else
echo $got_chrome
fi
exit 1

11 changes: 8 additions & 3 deletions login.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ def recapcha(driver):
pass
driver.switch_to.default_content()


def login_form(driver):
config = configparser.ConfigParser()
config.read("account.conf")
if os.path.exists(r'account.conf'):
config = configparser.ConfigParser()
config.read("account.conf")
else:
with open("tryhackmebot.log", 'a') as f:
print("[+] Cannot load config. Please run setup.")
f.write("[+] Cannot load config. Please run setup\n")
exit(1)
try:
driver.get("https://tryhackme.com/login")
time.sleep(random.uniform(3,6))
Expand Down
12 changes: 3 additions & 9 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@
import datetime
from login import *
from keepstreak import *
from selenium import webdriver
from selenium.webdriver.firefox.service import Service
from webdriver_manager.firefox import GeckoDriverManager
from browser import *


def main():
firefox_options = webdriver.FirefoxOptions()
firefox_options.headless = True
firefox_options.set_preference("media.volume_scale", "0.0")
firefox_options.set_preference("dom.push.enabled", False)
driver = webdriver.Firefox(service=Service(GeckoDriverManager(path=os.getcwd()).install(), log_path=os.devnull), options=firefox_options)
driver = get_driver()
driver.implicitly_wait(10)
os.system("cls" if sys.platform == "win32" else "clear")

with open("tryhackmebot.log", 'a') as f:
with open("tryhackmebot.log", 'w') as f:
print("[+] Starting...")
date = datetime.datetime.now().strftime("%d-%m-%Y, %H:%M:%S")
f.write(f"{date}\n")
Expand Down