Advertisement
Techmo

PythonOS_5_interp.py

Feb 26th, 2014
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.98 KB | None | 0 0
  1. __author__ = 'Brad'
  2. #File must be named 'interp.py'
  3. #Must be in the same directory as 'kernel.py' and 'eret.py'
  4. #You must have pycrypto installed on your computer
  5. #Python version must be python 2.7
  6. #DO NOT RUN THIS FILE run the file 'kernel.py'
  7. #This script is made to run on a unix terminal. the clear screen commands may not match.
  8. import os
  9. import random
  10. import time
  11. import platform
  12. import subprocess
  13. import smtplib
  14. import base64
  15. import hashlib
  16. from Crypto.Cipher import AES
  17. from eret import errorcodes
  18. def escape():
  19.     print
  20. def cls():
  21.     print("\x1B[2J")
  22. def cpuname():
  23.     if platform.system() == "Windows":
  24.         return platform.processor()
  25.     elif platform.system() == "Darwin":
  26.         return subprocess.check_output(['/usr/sbin/sysctl', "-n", "machdep.cpu.brand_string"]).strip()
  27.     elif platform.system() == "Linux":
  28.         command = "cat /proc/cpuinfo"
  29.         return subprocess.check_output(command, shell=True).strip()
  30.     return ""
  31. def interpret(com):
  32. #the say command
  33.     if com == "say":
  34.         print("syntax: say phrase")
  35.     elif com[:3] == "say":
  36.         word = com[4:]
  37.         print(word)
  38. #the resetpw command
  39.     elif com[:7] == "resetpw":
  40.         f1 = open("pass.enc", "r+")
  41.         password = f1.read()
  42.         PADDING = '{'
  43.         DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
  44.         key = 'hj%4jfd 7&gh17g*'
  45.         cipher = AES.new(key)
  46.         decoded = DecodeAES(cipher, password)
  47.         if ("resetpw " + decoded + " ") in com:
  48.             x = len(decoded) + 9
  49.             npas = com[x:]
  50.             BLOCK_SIZE = 16
  51.             PADDING ='{'
  52.             pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
  53.             EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
  54.             key = 'hj%4jfd 7&gh17g*'
  55.             cipher = AES.new(key)
  56.             encodedpass = EncodeAES(cipher, npas)
  57.             open("pass.txt", 'w').close()
  58.             f2 = open("pass.enc", "r+")
  59.             f2.write(encodedpass)
  60.             f2.flush()
  61.             open("state.txt", "w").close()
  62.             oss = open("state.txt", "r+")
  63.             oss.write("osState: 0")
  64.             print("Password Reset")
  65.         else:
  66.             print("syntax: resetpw oldpassword newpassword")
  67. #the count command
  68.     elif com == "count":
  69.         print("syntax: count startingnumber endingnumber interval")
  70.     elif com[:5] == "count":
  71.         coms = com[6:].split(" ")
  72.         snum = float(coms[0])
  73.         enum = float(coms[1])
  74.         interval = float(coms[2])
  75.         while snum <= enum:
  76.             print str(snum)
  77.             snum += interval
  78. #the sysinf command
  79.     elif com == "sysinf":
  80.         print("OS name: " + platform.release())
  81.         print("OS type: " + platform.system())
  82.         print("OS version: " + platform.version())
  83.         print("CPU: " + cpuname())
  84.         print("CPU type: " + platform.machine())
  85.         print("Python version: " + platform.python_version())
  86.         print("Network name: " + platform.node())
  87. #the coms command
  88.     elif com == "coms":
  89.         print("commands: coms, resetpw, count, calc, say, sysinf, rstusr, exit, new, view, cls, dir, encrypt, decrypt")
  90. #the rstusr command
  91.     elif com == "rstusr":
  92.         print"syntax: rstusr oldusername newusername"
  93.     elif com[:6] == "rstusr":
  94.         coms = com[7:].split(" ")
  95.         newusr = coms[1]
  96.         oldusr = coms[0]
  97.         f1 = open("usr.txt", "r+")
  98.         username = f1.read()
  99.         if oldusr == username:
  100.             open("usr.txt", 'w').close()
  101.             f3 = open("usr.txt", "r+")
  102.             f3.write(newusr)
  103.             f3.flush()
  104.             print("Username Reset")
  105.         else:
  106.             print("Username does not exist")
  107. #the exit command
  108.     elif com == "exit":
  109.         exit()
  110. #the new command
  111.     elif com == "new":
  112.         print("syntax: new filename")
  113.     elif com[:3] == "new":
  114.         coms = com[4:]
  115.         fname = coms
  116.         x = 1
  117.         y = 1
  118.         open(fname, "w").close()
  119.         file = open(fname, "r+")
  120.         cls()
  121.         print("PCOS Editor v1.2 | write [end] to end file")
  122.         print("------------------------------------------")
  123.         while y == 1:
  124.             line = raw_input(str(x) + ") ")
  125.             if line == "[end]":
  126.                 escape()
  127.                 y = 0
  128.             else:
  129.                 file.write(line + "\n")
  130.                 file.flush()
  131.                 x += 1
  132. #the view command
  133.     elif com == "view":
  134.         print("syntax: view filename")
  135.     elif com[:4] == "view":
  136.         coms = com[5:].split(" ")
  137.         name = coms[0]
  138.         if name == "pass.txt" or name == "state.txt":
  139.             print(errorcodes(1))
  140.         else:
  141.             try:
  142.                 fdata = open(name, "r+").read()
  143.                 print("The file contains:")
  144.                 print(fdata)
  145.             except IOError:
  146.                 print("Invalid file name/characters in file")
  147.  
  148. #the cls command
  149.     elif com == "cls":
  150.         cls()
  151. #the mail command
  152.     elif com == "mail":
  153.         print("syntax: mail youremail password recipientemail subject message")
  154.     elif com[:4] == "mail":
  155.         coms = com[4:].split(" ")
  156.         userEmail = coms[0]
  157.         userPass = coms[1]
  158.         TO = coms[2]
  159.         SUBJECT = coms[3]
  160.         TEXT = coms[4]
  161.         FROM = userEmail
  162.  
  163.      # Prepare actual message
  164.         message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
  165.        """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
  166.         try:
  167.             #server = smtplib.SMTP(SERVER)
  168.             server = smtplib.SMTP("smtp.gmail.com", 587) #or port 465 doesn't seem to work!
  169.             server.ehlo()
  170.             server.starttls()
  171.             server.login(userEmail, userPass)
  172.             server.sendmail(FROM, TO, message)
  173.             #server.quit()
  174.             server.close()
  175.             print 'successfully sent the mail'
  176.         except:
  177.             print (errorcodes(2))
  178.     elif com == "dir":
  179.         os.system("dir")
  180.     elif com == "encrypt":
  181.         print("syntax: encrypt file password")
  182.     elif com[:7] == "encrypt":
  183.         try:
  184.             coms = com[8:].split(" ")
  185.             fileName = coms[0]
  186.             passCode = coms[1]
  187.             file = open(fileName, "r+")
  188.             etext = file.read()
  189.             BLOCK_SIZE = 32
  190.             PADDING ='{'
  191.             pad = lambda s: s + (BLOCK_SIZE - len(s) % BLOCK_SIZE) * PADDING
  192.             EncodeAES = lambda c, s: base64.b64encode(c.encrypt(pad(s)))
  193.             key = hashlib.sha256(passCode).digest()
  194.             cipher = AES.new(key)
  195.             encoded = EncodeAES(cipher, etext)
  196.             open(fileName + ".enc", "w").close
  197.             file = open(fileName + ".enc", "r+")
  198.             file.write(encoded)
  199.             file.flush()
  200.             file.close()
  201.             print("Encrypted " + fileName + " to " + fileName + ".enc")
  202.         except:
  203.             print("File does not exist")
  204.     elif com == "decrypt":
  205.         print("syntax: decrypt filename password")
  206.     elif com[:7] == "decrypt":
  207.         try:
  208.             coms = com[8:].split(" ")
  209.             fileName = coms[0]
  210.             passWord = coms[1]
  211.             file = open(fileName, "r+")
  212.             encrypted = file.read()
  213.             PADDING = '{'
  214.             DecodeAES = lambda c, e: c.decrypt(base64.b64decode(e)).rstrip(PADDING)
  215.             key = hashlib.sha256(passWord).digest()
  216.             cipher = AES.new(key)
  217.             decoded = DecodeAES(cipher, encrypted)
  218.             newFileName = "decrypted-" + fileName[:(len(fileName) - 4)]
  219.             open(newFileName, "w").close()
  220.             newFile = open(newFileName, "r+")
  221.             newFile.write(decoded)
  222.             newFile.flush()
  223.             newFile.close()
  224.             print("Decrypted " + fileName + " to " + newFileName)
  225.             print("If the password was incorrect, the file content will be incorrect as well")
  226.         except:
  227.             print("File does not exist. Check the file name and be sure to include the '.enc' extension")
  228.     elif com == "calc":
  229.         print("syntax: calc function first_integer second_integer")
  230.         print("example: calc add 5 5")
  231.     elif com[:4] == "calc":
  232.         coms = com[5:].split(" ")
  233.         x = float(coms[1])
  234.         y = float(coms[2])
  235.         function = coms[0]
  236.         if function == "add":
  237.             z = x + y
  238.             print(str(x) + " + " + str(y) + " = " + str(z))
  239.         elif function == "subtract":
  240.             z = x - y
  241.             print(str(x) + " - " + str(y) + " = " + str(z))
  242.         elif function == "multiply":
  243.             z = x * y
  244.             print(str(x) + " x " + str(y) + " = " + str(z))
  245.         elif function == "divide":
  246.             z = x / y
  247.             print(str(x) + " / " + str(y) + " = " + str(z))
  248.         else:
  249.              print("invalid function")
  250.     elif com == "run":
  251.         print("syntax: run file")
  252.         print("file can have .py or .pyc extensions")
  253.     elif com[:3] == "run":
  254.         file = com[4:]
  255.         os.system("python " + file)
  256.     else:
  257.         print("Unknown command")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement