Create a Post
cancel
Showing results for 
Search instead for 
Did you mean: 
Ploni_Almoni
Participant
Jump to solution

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.

 

1 Solution

Accepted Solutions
Nehorai_Elbaz
Participant

Hi, I succeeded to upload to a local server using the RestSharp, (With HttpClient I succeeded to upload to the cloud but not to a local server, can't tell why)

Add reference to RestSharp  dll and try this code:

 

async Task UploadAsync()
{
    ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
    var client = new RestClient("https://xxx.xxx.xxx.xxx:1234/tecloud/api/v1/file/upload");
    var request = new RestRequest(Method.POST);
    string path = @"C:\Users\nehorai_elbaz\test.png";
    string json = "{\"request\":"
    + "{"
    + $"\"file_name\": \"{Path.GetFileName(path)}\","
    + $"\"file_type\": \"{Path.GetExtension(path)}\","
    + "\"features\": [\"te\"],"
    + "\"te\": {\"reports\": [\"tar\"]}"
    + "}"
    + "}";

    request.AddParameter("request", json);
    request.AddFileBytes("file",File.ReadAllBytes(path), "test.png");
    IRestResponse response = await client.ExecuteTaskAsync(request);
    Console.WriteLine(response.Content);
}

 

Please let me know if it works

View solution in original post

0 Kudos
15 Replies
G_W_Albrecht
Legend
Legend

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

CCSE CCTE CCSM SMB Specialist
Ploni_Almoni
Participant

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

G_W_Albrecht
Legend
Legend

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?

CCSE CCTE CCSM SMB Specialist
Ploni_Almoni
Participant

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

0 Kudos
Thomas_Werner
Employee Alumnus
Employee Alumnus

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

https://community.checkpoint.com/docs/DOC-3091-using-sandblast-api-from-commandline 

Can you also please post your full API request ?

Regards Thomas 

Ploni_Almoni
Participant

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
Thomas_Werner
Employee Alumnus
Employee Alumnus

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

Nehorai_Elbaz
Participant

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
Thomas_Werner
Employee Alumnus
Employee Alumnus

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

Nehorai_Elbaz
Participant

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

0 Kudos
Thomas_Werner
Employee Alumnus
Employee Alumnus

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

Kwangsoo__Soo__
Employee Alumnus
Employee Alumnus

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

Nehorai_Elbaz
Participant

Yes I succeded, I will add my github repo/ or a code snippet soon

0 Kudos
Nazarii_Makohin
Participant
Participant

Hello

Could You update us with repo link or explain how to resolve BAD REQUEST error to local Appliance?

0 Kudos
Nehorai_Elbaz
Participant

Hi, I succeeded to upload to a local server using the RestSharp, (With HttpClient I succeeded to upload to the cloud but not to a local server, can't tell why)

Add reference to RestSharp  dll and try this code:

 

async Task UploadAsync()
{
    ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
    var client = new RestClient("https://xxx.xxx.xxx.xxx:1234/tecloud/api/v1/file/upload");
    var request = new RestRequest(Method.POST);
    string path = @"C:\Users\nehorai_elbaz\test.png";
    string json = "{\"request\":"
    + "{"
    + $"\"file_name\": \"{Path.GetFileName(path)}\","
    + $"\"file_type\": \"{Path.GetExtension(path)}\","
    + "\"features\": [\"te\"],"
    + "\"te\": {\"reports\": [\"tar\"]}"
    + "}"
    + "}";

    request.AddParameter("request", json);
    request.AddFileBytes("file",File.ReadAllBytes(path), "test.png");
    IRestResponse response = await client.ExecuteTaskAsync(request);
    Console.WriteLine(response.Content);
}

 

Please let me know if it works

0 Kudos

Leaderboard

Epsum factorial non deposit quid pro quo hic escorol.

Upcoming Events

    CheckMates Events