Created
September 22, 2014 15:33
-
-
Save hasherezade/b7fe598b33b9a94b0c2d to your computer and use it in GitHub Desktop.
Script for communication with SolarBot server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/python | |
| # Script for communication with SolarBot server | |
| # only for research purpose! | |
| # CC-BY: hasherezade (http://hasherezade.net) | |
| # uses function 'crypt' from: http://www.joonis.de/de/code/rc4-algorithm | |
| import sys | |
| import urllib2 | |
| import time | |
| verbose = 0 | |
| g_Host = 'yghqlyz.com' | |
| CMD_SLEEP = 0xC | |
| def crypt(data, key): | |
| """RC4 algorithm""" | |
| x = 0 | |
| box = range(256) | |
| for i in range(256): | |
| x = (x + box[i] + ord(key[i % len(key)])) % 256 | |
| box[i], box[x] = box[x], box[i] | |
| x = y = 0 | |
| out = [] | |
| for char in data: | |
| x = (x + 1) % 256 | |
| y = (y + box[x]) % 256 | |
| box[x], box[y] = box[y], box[x] | |
| out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256])) | |
| return out | |
| def sendData(data): | |
| url = 'http://'+ g_Host | |
| clen = len(data) | |
| request = urllib2.Request(url, data, {'Content-Type': 'application/x-www-form-urlencoded', | |
| 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; InfoPath.2)', | |
| 'Host': g_Host, | |
| 'Content-Length': clen, | |
| 'Cache-Control': 'no-cache'} | |
| ) | |
| request.get_method = lambda: 'POST' | |
| resp = urllib2.urlopen(request) | |
| rcode = resp.getcode() | |
| resContent = "ERR" | |
| if (rcode == 200): | |
| respContent = resp.read() | |
| resp.close() | |
| l = list(respContent) | |
| return l | |
| def prepareData(key): | |
| data = 'v=1.1&u=User&c=HOME&s=' + key +'&w=2.5.1&b=32' | |
| return data | |
| def executeCommand(decoded): | |
| decLen = len(decoded) | |
| if (decLen == 0): | |
| return False | |
| cmd = ord(decoded[0]) | |
| param = decoded[1:(decLen)] | |
| print "CMD = " + str(cmd) | |
| print "PARAM = " + ''.join(param) | |
| if (ord(decoded[0]) == CMD_SLEEP): | |
| paramVal = int(''.join(param)) | |
| print 'Sleeping '+ str(paramVal) + ' seconds...' | |
| time.sleep(paramVal) | |
| return True | |
| def splitCommands(decoded): | |
| DELIM = 0x00 | |
| prev = 0 | |
| position = 0 | |
| result = [] | |
| for position, char in enumerate(decoded): | |
| if ord(char) == DELIM : | |
| cmd = decoded[prev:position] | |
| result.append(cmd) | |
| prev = position + 1 | |
| return result | |
| def main(): | |
| ARG_NUM = 2 | |
| argc = sys.argv.__len__() | |
| rcode = 200 | |
| if (argc < ARG_NUM): | |
| print "Supply the key (s), i.e: {12345678-1234-1234-1234-123456789abcde}" | |
| exit (2) | |
| key = sys.argv[1] | |
| if (verbose): | |
| print "KEY=" + key | |
| data = prepareData(key) | |
| if (verbose): | |
| print "Data : " + data | |
| WAIT_TIME = 60 | |
| while True: | |
| try : | |
| respContent = sendData(data) | |
| if (verbose): | |
| print "DONE: " | |
| print respContent | |
| if (len(respContent) == 0): | |
| break | |
| decoded = crypt(respContent, key) | |
| print decoded | |
| commands = splitCommands(decoded) | |
| for cmd in commands: | |
| if (executeCommand(cmd) == False): | |
| break | |
| except (urllib2.HTTPError, urllib2.URLError) as e: | |
| print str(e) | |
| print "Trying again after " + str(WAIT_TIME) | |
| time.sleep(WAIT_TIME) | |
| return(0) | |
| if __name__ == "__main__": | |
| sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment