Jump to content

KSC 11 OpenAPI login problem


Recommended Posts

When POSTing to https://servername:13299/api/v1.0/login with the following headers: Authorization: KSCBasic user="x", pass="x", internal="1" Content-Type: application/json X-KSC-VServer: x Content-Length: 2 I get the following message back: “Authentication failure”. The user has full admin access, and can login to the KSC MMC console. According to the documentation the user, pass and X-KSC-VServer should all be endcoded with Base64.
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...
We are having the same problem using cURL on windows for testing and cannot login. We have tried many variants and also receive an authentication failure message. The current command we are using is curl -X POST https://hostname:13299/api/v1.0/login -H "Authorization: KSCBasic user="username", pass="password", internal="1"" -H "Content-Type: application/json" -H "Content-Length: 2" -d "{}" -k -v The user account is a full local admin on the server and can login to the MMC console. Username and password have been Base64 encoded. We have also tried using Postman and receive the same error message.
Link to comment
Share on other sites

  • 7 months later...

I too when I try to authenticate I get this error:

The authentication header is not valid


This is my script:
<?php


$ksc_server = "https://servert:13299";
$url = $ksc_server."/api/v1.0/login";

$user='user';
$password='password';
$user=base64_encode($user);
$password=base64_encode($password);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //timeout after 30 seconds
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$header=array("Authorization: KSCBasic user='$user', pass='$password', internal='1'",
               "Content-Type: application/json","Content-Length: 2");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

echo $result=curl_exec($ch);
echo "Status Code: ". curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result === false) {
    die('Error fetching data: ' . curl_error($ch));
}
$array=json_decode($result);
print_r($array);

curl_close($ch);
?>

Did I do something wrong in the header?

$header=array("Authorization: KSCBasic user='$user', pass='$password', internal='1'",
               "Content-Type: application/json","Content-Length: 2");

 
Link to comment
Share on other sites

  • 1 month later...

I spent an hour today trying to figure out why I can’t login. There is incorrect code:

    'Authorization': 'KSCBasic user="' + user + '", pass="' + passw + '", internal = "1"',

And there is correct:

'Authorization': 'KSCBasic user="' + user + '", pass="' + passw + '", internal="1"',

I had spaces around ‘=’ and it was the problem.

Dirkpitt, may be your code is not working because of using ‘ instead of ” ?

Link to comment
Share on other sites

Hello DmitriyL

 

thanks, but I tried with and without spaces but nothing

 

 

I tried it like this

  1. "Authorization: KSCBasic user='$user',pass='$password',internal='1'","Content-Type: application/json"
  2. "Authorization: KSCBasic user='$user',pass='$password',internal='1'"
  3. "Authorization: KSCBasic user='$user', pass='$password', internal='1'"
  4. "Authorization: KSCBasic user='$user', pass='$password'"

 

BUT I ALWAYS RECEIVE THIS ERROR

 

 

Authentication header is invalid
status Code: 401

Link to comment
Share on other sites

<?php

// get cURL resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, 'https://you_ip:13299/api/v1.0/login');

// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
  'Authorization: KSCBasic user="login", pass="pass"',
  ‘X-KSC-VServer: x',
]);

$response = curl_exec($ch);

// stop if fails
if (!$response) {
  die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;

curl_close($ch);

 

worked…

 

also I`ll now writing client library for accessing the KSC, you can see how certain elements work.

Here

Link to comment
Share on other sites

  • 2 weeks later...
Hello @0xcffaedfe  thank you for your answer, I understand where I went wrong.

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: KSCBasic user="dfahdushasda=", pass="afdsaedafafa=", internal="1"']);

 

I saw that no token is issued to me, so for further requests I used the session method with cookies

 

adding these lines

curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);  
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);

 

I finally got the 200 code

 

 

so I tried to make a new request, to get to know the groups

-------------------------------------------------------------------------------------------------------------

$url = $ksc_server."/api/v1.0/HostGroup.FindGroups";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);


$data_string = '{"wstrFilter": "", "vecFieldsToReturn": ["id", "name"], "lMaxLifeTime": 1000}';


curl_setopt($ch, CURLOPT_HTTPHEADER, ['"Content-Type": "application/json"']);
curl_setopt($ch, CURLOPT_POSTFIELDS, array());
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_VERBOSE, true);
echo $result=curl_exec($ch);
echo "<br>status Code: ". curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result === false) {
    die('Error fetching data: ' . curl_error($ch));
}
$array=json_decode($result);
print_r($array);

curl_close($ch);

-------------------------------------------------------------------------------

 

 

this is the result:

status Code: 0

Error fetching data: Empty reply from server

 

 

what am i doing wrong now?

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

  Hello @0xcffaedfe  thank you for your answer, I understand where I went wrong.

curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: KSCBasic user="dfahdushasda=", pass="afdsaedafafa=", internal="1"']);

 

I saw that no token is issued to me, so for further requests I used the session method with cookies

 

adding these lines

curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);  
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);

 

I finally got the 200 code

 

 

so I tried to make a new request, to get to know the groups

-------------------------------------------------------------------------------------------------------------

$url = $ksc_server."/api/v1.0/HostGroup.FindGroups";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);


$data_string = '{"wstrFilter": "", "vecFieldsToReturn": ["id", "name"], "lMaxLifeTime": 1000}';


curl_setopt($ch, CURLOPT_HTTPHEADER, ['"Content-Type": "application/json"']);
curl_setopt($ch, CURLOPT_POSTFIELDS, array());
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_VERBOSE, true);
echo $result=curl_exec($ch);
echo "<br>status Code: ". curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($result === false) {
    die('Error fetching data: ' . curl_error($ch));
}
$array=json_decode($result);
print_r($array);

curl_close($ch);

-------------------------------------------------------------------------------

 

 

this is the result:

status Code: 0

Error fetching data: Empty reply from server

 

 

what am i doing wrong now?

 

 

 

 

 

 

 

 

 

 

I think this is superfluous:


curl_setopt($ch, CURLOPT_HTTPHEADER, ['"Content-Type": "application/json"']);
curl_setopt($ch, CURLOPT_POSTFIELDS, array());

working example below

 

<?php

// get cURL resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, 'https://ksc_ip:13299/api/v1.0/HostGroup.FindGroups');

// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: KSCBasic user="login_hash", pass="pass_hash"',
'X-KSC-VServer: x',
'Content-Type: application/json; charset=utf-8',
]);

// json body
$json_array = [
'vecFieldsToOrder' => null,
'pParams' => [
'KLGRP_FIND_FROM_CUR_VS_ONLY' => true,
'KLSRVH_SLAVE_REC_DEPTH' => 0
],
'vecFieldsToReturn' => [
'id',
'name',
'grp_full_name',
'creationDate',
'KLGRP_CHLDHST_CNT',
'KLSRV_HSTSTAT_CRITICAL',
'KLSRV_HSTSTAT_WARNING'
],
'lMaxLifeTime' => 100,
'wstrFilter' => '
(&
(!"KLGRP_CHLDHST_CNT" = 0)
(!"grp_from_unassigned" = true)
)'
];
$body = json_encode($json_array);

// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);


// send the request and save response to $response
$response = curl_exec($ch);

// stop if fails
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;

// close curl resource to free up system resources
curl_close($ch);

 

 

Link to comment
Share on other sites

 

Hello @0xcffaedfe 

i am continuing to test my php script, if i try to make this call,

 

this is the previous answer

HTTP Status Code: 200 Response Body: {"strAccessor":"iGkjeDpOwG4gyhmLBPJi41","PxgRetVal":42}

 

new call

 

curl_setopt($ch, CURLOPT_URL, $ksc_server.'/api/v1.0/ChunkAccessor.GetItemsCount');

.

.

$json_array = [
    'strAccessor' => $strAccessor
 ];
$body = json_encode($json_array);
print_r($body);

// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

.

.

 

it gives me this error:

 

HTTP Status Code: 200 Response Body: {"PxgError":{"code":1186,"file":"c:\\a\\b\\a_32ba8ed2\\s\\csadminkit\\development2\\std\\tmstg\\timeoutstore.cpp","line":211,"message":"Object \"iGkjeDpOwG4gyhmLBPJi41\" is absent","module":"KLSTD"}}

 

thank you very much for the help you are giving me

 

Link to comment
Share on other sites

 Hello @ 0xcffaedfe   
to do a quick test, I put the two curl calls one below the other, but the result hasn't changed.
as a first answer I got:

HTTP Status Code: 200 Response Body: {"strAccessor":"Lxt1En2LCqqa45K4X0WeD2","PxgRetVal":20}

 

 

as a second answer I get:

 

HTTP Status Code: 200 Response Body: {"PxgError":{"code":1186,"file":"c:\\a\\b\\a_32ba8ed2\\s\\csadminkit\\development2\\std\\tmstg\\timeoutstore.cpp","line":211,"message":"Object \"Lxt1En2LCqqa45K4X0WeD2\" is absent","module":"KLSTD"}}

 

 

 how can i keep the session open? i tried to increase LMaxLifeTime too, but the result hasn't changed

:(

 

 

Link to comment
Share on other sites

    Hello @ 0xcffaedfe   
   to do a quick test, I put the two curl calls one below the other, but the result hasn't changed.
   as a first answer I got:

HTTP Status Code: 200 Response Body: {"strAccessor":"Lxt1En2LCqqa45K4X0WeD2","PxgRetVal":20}

 

 

as a second answer I get:

 

HTTP Status Code: 200 Response Body: {"PxgError":{"code":1186,"file":"c:\\a\\b\\a_32ba8ed2\\s\\csadminkit\\development2\\std\\tmstg\\timeoutstore.cpp","line":211,"message":"Object \"Lxt1En2LCqqa45K4X0WeD2\" is absent","module":"KLSTD"}}

 

 

    how can i keep the session open? i tried to increase LMaxLifeTime too, but the result hasn't changed

:(

 

 

think u should use another auth method.

<?php

// get cURL resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, 'https://ip:13299/api/v1.0/Session.StartSession'); // <-!

// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: KSCBasic user="login", pass="pass"',
'X-KSC-VServer: x',
'Content-Type: application/json; charset=utf-8',
]);

// json body
$json_array = [

];
$body = json_encode($json_array);

// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// send the request and save response to $response
$response = curl_exec($ch);

// stop if fails
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;

// close curl resource to free up system resources
curl_close($ch);

 

server response will be as example: {"PxgRetVal":"nngQKNEDkx9E6Pe63Be8eOQ=="}

u should user this token in header with key:  X-KSC-Session

<?php

// get cURL resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, 'https://ip:13299/api/v1.0/HostGroup.FindGroups');

// set method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');

// return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'X-KSC-VServer: x',
'X-KSC-Session: n9M8NjkzZUADIgn5wnKCkZA==', // !< - Auth token received from Session.StartSession
'Content-Type: application/json; charset=utf-8',
]);

// json body
$json_array = [
'vecFieldsToOrder' => null,
'pParams' => [
'KLGRP_FIND_FROM_CUR_VS_ONLY' => true,
'KLSRVH_SLAVE_REC_DEPTH' => 0
],
'vecFieldsToReturn' => [
'id',
'name',
'grp_full_name',
'creationDate',
'KLGRP_CHLDHST_CNT',
'KLSRV_HSTSTAT_CRITICAL',
'KLSRV_HSTSTAT_WARNING'
],
'lMaxLifeTime' => 100,
'wstrFilter' => '
(&
(!"KLGRP_CHLDHST_CNT" = 0)
(!"grp_from_unassigned" = true)
)'
];
$body = json_encode($json_array);

// set body
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

// send the request and save response to $response
$response = curl_exec($ch);

// stop if fails
if (!$response) {
die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo 'HTTP Status Code: ' . curl_getinfo($ch, CURLINFO_HTTP_CODE) . PHP_EOL;
echo 'Response Body: ' . $response . PHP_EOL;

// close curl resource to free up system resources
curl_close($ch);

 

 

but this token expires in 3 minutes =)

Link to comment
Share on other sites

  • 9 months later...

Hello everyone!!

 

I come back on this topic because I was trying to use OpenApi to get some info on the devices managed by my KSC but I have problem calling login API.

The error I get is 401 Unauthorized, that (reading the documentation available on the KSC) seems to be casued by a wrong formatted header.

 

Here below I post my code (vb.net):

WReq = CType(WebRequest.Create("https://" + ksc + ":13299/api/v1.0/login"), HttpWebRequest)

        ksc_u_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_u)).ToString()
        ksc_p_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_p)).ToString()
        ksc_h_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_h)).ToString()

        WReq.Headers.Add("Authorization", "KSCBasic user=" + ksc_u_b64 + ", pass=" + ksc_p_b64)
        'WReq.Headers.Add("X-KSC-VServer", ksc_h_b64)

        WReq.Method = "POST"
        WReq.ProtocolVersion = HttpVersion.Version11
        WReq.ContentType = "application/json"
        'WReq.ContentLength = 2
        System.Net.ServicePointManager.Expect100Continue = False
        WReq.Timeout = 12000000

        Dim jsonBytes = Encoding.UTF8.GetBytes("{}")
        WReq.ContentLength = jsonBytes.Length
        Using reqStream = WReq.GetRequestStream()
            reqStream.Write(jsonBytes, 0, jsonBytes.Length)
        End Using

        WResp = CType(WReq.GetResponse(), HttpWebResponse)

 

ksc = ip_address of my KSC

ksc_u_b64 = user base64 encoded (I am using a domain admin user, in order to be sure to have the permission

ksc_p_b64 = password base64 encoded

 

I have also tried passiong user and password with “”, as well I have also tried passing the parameter internal="1"  (I have made a try with =”0” too) and tried also passing the parameter X-KSC-VServer (the variable ksc_h_b64 in my code) but with no luck :(

 

Could you please help me out solving the problem? I have no idea where to turn my head.

 

Thank you in advance

 

Cheers

Matteo

 

 

 

Link to comment
Share on other sites

  • 3 weeks later...

Hello everyone!!

 

I come back on this topic because I was trying to use OpenApi to get some info on the devices managed by my KSC but I have problem calling login API.

The error I get is 401 Unauthorized, that (reading the documentation available on the KSC) seems to be casued by a wrong formatted header.

 

Here below I post my code (vb.net):

WReq = CType(WebRequest.Create("https://" + ksc + ":13299/api/v1.0/login"), HttpWebRequest)

        ksc_u_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_u)).ToString()
        ksc_p_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_p)).ToString()
        ksc_h_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_h)).ToString()

        WReq.Headers.Add("Authorization", "KSCBasic user=" + ksc_u_b64 + ", pass=" + ksc_p_b64)
        'WReq.Headers.Add("X-KSC-VServer", ksc_h_b64)

        WReq.Method = "POST"
        WReq.ProtocolVersion = HttpVersion.Version11
        WReq.ContentType = "application/json"
        'WReq.ContentLength = 2
        System.Net.ServicePointManager.Expect100Continue = False
        WReq.Timeout = 12000000

        Dim jsonBytes = Encoding.UTF8.GetBytes("{}")
        WReq.ContentLength = jsonBytes.Length
        Using reqStream = WReq.GetRequestStream()
            reqStream.Write(jsonBytes, 0, jsonBytes.Length)
        End Using

        WResp = CType(WReq.GetResponse(), HttpWebResponse)

 

ksc = ip_address of my KSC

ksc_u_b64 = user base64 encoded (I am using a domain admin user, in order to be sure to have the permission

ksc_p_b64 = password base64 encoded

 

I have also tried passiong user and password with “”, as well I have also tried passing the parameter internal="1"  (I have made a try with =”0” too) and tried also passing the parameter X-KSC-VServer (the variable ksc_h_b64 in my code) but with no luck :(

 

Could you please help me out solving the problem? I have no idea where to turn my head.

 

Thank you in advance

 

Cheers

Matteo

 

 

 

 

  1. Skip (CertificateValidation)!

     ServicePointManager.ServerCertificateValidationCallback = Function(se As Object, cert As X509Certificate,
        chain As X509Chain, sslerror As SslPolicyErrors) True

 


   2. Escape Double! (Name\Password hash must be quoted!)

request.Headers.Add("Authorization", "KSCBasic user=""" + ksc_u_b64 + """, pass=""" + ksc_p_b64 + """")

Link to comment
Share on other sites

Hi,

first of all thank you for your reply.

I have tried your suggestion, but still get the same 401 Unauthorized.

 

Here below the code:

        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12
        System.Net.ServicePointManager.ServerCertificateValidationCallback = Function(se As Object, cert As System.Security.Cryptography.X509Certificates.X509Certificate, chain As System.Security.Cryptography.X509Certificates.X509Chain, sslerror As System.Net.Security.SslPolicyErrors) True

        WReq = CType(WebRequest.Create("https://" + ksc_h + ":13299/api/v1.0/Session.StartSession"), HttpWebRequest)

        ksc_u_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_u)).ToString()
        ksc_p_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_p)).ToString()
        ksc_h_b64 = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(ksc_h)).ToString()

WReq.Method = "POST"
        WReq.ProtocolVersion = HttpVersion.Version11
        WReq.ContentType = "application/json"
        'WReq.ContentLength = 2
        System.Net.ServicePointManager.Expect100Continue = False
        WReq.Timeout = 12000000

        WReq.Headers.Add("Authorization", "KSCBasic user=""" + ksc_u_b64 + """, pass=""" + ksc_p_b64 + """")

May the user passed be a domain user or shall it be a kaspesky user o local user of the server?

 

Do you know if I shall enable anything on the security center side?

 

 

Thanks in advance

Mateo

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...