Skip to content

Instantly share code, notes, and snippets.

@riptidewave93
Last active August 8, 2024 18:02
Show Gist options
  • Save riptidewave93/ad135229b0f0939da7bd223d7f723528 to your computer and use it in GitHub Desktop.
Save riptidewave93/ad135229b0f0939da7bd223d7f723528 to your computer and use it in GitHub Desktop.
ty-enable-ssh.py
#!/usr/bin/env python3
# Created by Chris Blake (chrisrblake93@gmail.com)
# Please do not redistribute!
import argparse
import hashlib
import os
import re
import shutil
import sys
import tarfile
import tempfile
cryptkey = 'frkenc##KEY@R717'
def main(file):
# Can we access it?
if not os.path.isfile(file):
print(f"Unable to process {file}, please verify the file path.")
sys.exit(1)
elif not os.access(file, os.R_OK):
print(f"Unable to read from {file}, please check file permissions.")
sys.exit(1)
# Make sure we know where to save our new generated config
cfgpath, _ = os.path.split(os.path.abspath(file))
# Create a temp directory to work in
tmpdir = tempfile.TemporaryDirectory()
# Copy the config file
shutil.copyfile(file, f"{tmpdir.name}/hotspot_cfg.bin")
# Decrypt
os.system(f"openssl enc -aes-128-cbc -d -md md5 -in {tmpdir.name}/hotspot_cfg.bin -out {tmpdir.name}/hotspot_cfg_extract.tar -k {cryptkey} 2> /dev/null")
os.remove(f"{tmpdir.name}/hotspot_cfg.bin")
# Extract Layer 1
os.mkdir(f"{tmpdir.name}/hotspot_cfg")
hscfg1 = tarfile.open(f"{tmpdir.name}/hotspot_cfg_extract.tar")
hscfg1.extractall(f"{tmpdir.name}/hotspot_cfg/")
hscfg1.close()
os.remove(f"{tmpdir.name}/hotspot_cfg_extract.tar")
# Extract Layer 2
os.mkdir(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg")
hscfg2 = tarfile.open(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg.tar")
hscfg2.extractall(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg/")
hscfg2.close()
os.remove(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg.tar")
# Enable SSH
with open(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg/data/configs/mobileap_cfg.xml", "r") as sources:
lines = sources.readlines()
with open(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg/data/configs/mobileap_cfg.xml", "w") as sources:
for line in lines:
sources.write(re.sub('<Ssh>0</Ssh>', '<Ssh>1</Ssh>', line))
# Package layer 2
hscfg2 = tarfile.open(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg.tar", "w:gz")
hscfg2.add(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg/data",arcname="data")
hscfg2.close()
shutil.rmtree(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg/")
# Update MD5sum
newhash = hashlib.md5(open(f"{tmpdir.name}/hotspot_cfg/hotspot_cfg.tar", "rb").read()).hexdigest()
with open(f"{tmpdir.name}/hotspot_cfg/hashfile", 'w') as f:
f.write(f"hotspot_cfg.tar={newhash}")
# Package layer 1
hscfg1 = tarfile.open(f"{tmpdir.name}/hotspot_cfg.tar", "w:gz")
for file in ["hashfile", "model", "hotspot_cfg.tar"]:
hscfg1.add(f"{tmpdir.name}/hotspot_cfg/{file}",arcname=f"{file}")
hscfg1.close()
shutil.rmtree(f"{tmpdir.name}/hotspot_cfg/")
# Encrypt
os.system(f"openssl enc -aes-128-cbc -md md5 -in {tmpdir.name}/hotspot_cfg.tar -out {tmpdir.name}/hotspot_cfg.bin -k {cryptkey} 2> /dev/null")
os.remove(f"{tmpdir.name}/hotspot_cfg.tar")
# Copy it back to our main dir
shutil.copyfile(f"{tmpdir.name}/hotspot_cfg.bin", f"{cfgpath}/hotspot_cfg_ssh_enabled.bin")
tmpdir.cleanup() # removes the temp dir
print("Complete, upload hotspot_cfg_ssh_enabled.bin to enable SSH access. Enjoy!")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Modify a T-Mobile T9 config backup to enable SSH.')
parser.add_argument('file', help='The T9 config file to enable SSH on')
args = parser.parse_args()
main(args.file)
@mustj
Copy link

mustj commented Aug 19, 2022

how do we use this on windows?

@RIdey1
Copy link

RIdey1 commented Sep 11, 2022

fg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment