cancel
Showing results for 
Search instead for 
Did you mean: 
Post a Question

Uploading file works on cloud but not local gateway

I'm using sandBlast api, when I upload to the cloud all is fine, but when I try to upload to local gateway I receive

code:1008,label:BAD_REQUEST, message: Invalid Multipart/form request

Why?

I'm not sending cookie to local gateway, I'm using CSharp.

I found the same issue here, but there is no answer yes, please If you can share a working code example for c# in local gateway.

 

12 Replies

Re: Uploading file works on cloud but not local gateway

So when not using API calls, but the local CLI, upload to local sandblast appliance works ?

Re: Uploading file works on cloud but not local gateway

I am using This guide, there is no CLI here, can you share CLI guide? I will try

Re: Uploading file works on cloud but not local gateway

That is easy - most of it can be found here: sk114806 ATRG: Threat Emulation. How to manually emulate a file you can find in sk110375 How to emulate files manually on the SandBlast TE Appliance?

Re: Uploading file works on cloud but not local gateway

The links leads to something likeThat what I see when I click the links the you mentioned

0 Kudos
Employee++
Employee++

Re: Uploading file works on cloud but not local gateway

You may check this script to also get an idea how the API call looks for a local SB appliance:

 

Can you also please post your full API request ?

Regards Thomas 

Re: Uploading file works on cloud but not local gateway

I was succeeded to upload file to the gateway via SandBlast Chrome extension, but not via code.

I also was succeeded to perform a query from code to the local gateway.

But it fails when I try to upload a file to local gateway it always return "Bad request" "Invalid Multipart/form request", I have added my C# code:

static void Main(string[] args)
{
MainAsync(args).Wait();
}
static async Task MainAsync(string[] args)
{
string json = "{\r\n\t\"request\": {\r\n\t\t\"md5\": \"44D88612FEA8A8F36DE82E1278ABB02F\",\r\n\t\t\"file_name\": \"TestEicar.pdf\",\r\n\t\t\"file_type\": \"pdf\",\r\n\t\t\"features\": [\"te\"],\r\n\t\t\"te\": {\r\n\t\t\t\"reports\": [\"pdf\", \"xml\"],\r\n\t\t\t\"images\": [{\r\n\t\t\t\t\"id\": \"7e6fe36e-889e-4c25-8704-56378f0830df\",\r\n\t\t\t\t\"revision\": 1\r\n\t\t\t}, {\r\n\t\t\t\t\"id\": \"e50e99f3-5963-4573-af9e-e3f4750b55e2\",\r\n\t\t\t\t\"revision\": 1\r\n\t\t\t}]\r\n\t\t}\r\n\t}\r\n}";

Console.WriteLine(json);
ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

var path = @"C:\Users\066570516\Desktop\TempEicar.pdf";
var url = "https://172.45.1.122:18194/tecloud/api/v1/file/upload";
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "MyAPIKEY");

//add json byte array to mulipart
MultipartFormDataContent form = new MultipartFormDataContent("----WebKitFormBoundary7MA4YWxkTrZu0gW");
byte[] jsonByte = Encoding.ASCII.GetBytes(json);
var jsonContent = new ByteArrayContent(jsonByte);

jsonContent.Headers.Add("Content-Disposition", "form-data; name=\"request\"");
jsonContent.Headers.Add("Content-Type", "application/json");
form.Add(jsonContent);

//add file stream to multipart
FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
var streamContent = new StreamContent(fs);
streamContent.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + Path.GetFileName(path) + "\"");
streamContent.Headers.Add("Content-Type", "application/pdf");
form.Add(streamContent);

HttpResponseMessage response = await httpClient.PostAsync(url, form);
response.EnsureSuccessStatusCode();
httpClient.Dispose();
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}

0 Kudos
Employee++
Employee++

Re: Uploading file works on cloud but not local gateway

Hi Ploni,

is there a way that you can extract/show the pure JSON request that is generated by your code ?

I am not too good in reading C# code 🙂

Regards Thomas

Re: Uploading file works on cloud but not local gateway

And here is the text:

{
"request": {
"md5": "44D88612FEA8A8F36DE82E1278ABB02F",
"file_name": "TestEicar.pdf",
"file_type": "pdf",
"features": ["te"],
"te": {
"reports": ["pdf", "xml"],
"images": [{
"id": "7e6fe36e-889e-4c25-8704-56378f0830df",
"revision": 1
}, {
"id": "e50e99f3-5963-4573-af9e-e3f4750b55e2",
"revision": 1
}]
}
}
}

0 Kudos
Employee++
Employee++

Re: Uploading file works on cloud but not local gateway

For testing can you strip down your request to:

{
"request": {
"features": ["te"],
"te": {
"reports": ["pdf", "xml"],
"images": [{
"id": "7e6fe36e-889e-4c25-8704-56378f0830df",
"revision": 1
}, {
"id": "e50e99f3-5963-4573-af9e-e3f4750b55e2",
"revision": 1
}]
}
}
}

Regards Thomas

Re: Uploading file works on cloud but not local gateway

Hi, I am still getting "BAD REQUEST" as reponse

0 Kudos
Employee++
Employee++

Re: Uploading file works on cloud but not local gateway

Hi Nehorai,

to see if the appliance API itself has a problem could you please try to send the API request and file through Postman tool ? Postman | API Development Environment 

I assume your problem is how the file is uploaded by your code ... 

Regards Thomas

Re: Uploading file works on cloud but not local gateway

Hi Iron,

Have you found the solution ? I am facing the same issue like you. hope you share knowledge if you found it.

BR,

Soo