Hack Forums
[C#] Google Safe Browsing API v4 implementation with C# returns null response - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Google Safe Browsing API v4 implementation with C# returns null response (/showthread.php?tid=6037572)



Google Safe Browsing API v4 implementation with C# returns null response - ๖ۣۜchαmєllєσ - Jan 2, 2020

I am trying to lookup/scan the status of URLs with the Google Safe Browsing API v4, I tried the method I found in this link How to use Google Safe Browsing (v4) with .NET, but i get a null response returned.

Here's the code:

Code
private async void checkUrlBtn_Click(object sender, EventArgs e)
{
   var service = new SafebrowsingService(new BaseClientService.Initializer
   {
       ApplicationName = "dotnet-client",
       ApiKey = "[Fake API Key]AIzaSkuyfkuyI_kbyfuylijldcDfhG-g6t"
   });

   var request = service.ThreatMatches.Find(new FindThreatMatchesRequest()
   {
       Client = new ClientInfo
       {
           ClientId = "Dotnet-client",
           ClientVersion = "1.5.2"
       },
       ThreatInfo = new ThreatInfo()
       {
           ThreatTypes = new List<string> { "SOCIAL_ENGINEERING" },
           PlatformTypes = new List<string> { "PLATFORM_TYPE_UNSPECIFIED" },
           ThreatEntryTypes = new List<string> { "URL" },
           ThreatEntries = new List<ThreatEntry>
           {
               new ThreatEntry
               {
                   Url = "https://someurlIGotFromPhishtank.com"
               }
           }
       }
   });
   var response = await request.ExecuteAsync();
   string json = JsonConvert.SerializeObject(response);
   string jsonFormatted = JToken.Parse(json).ToString(Formatting.Indented);
   Console.WriteLine(jsonFormatted);
}
 
My Response:

Code
{
 "matches": null,
 "ETag": null
}
At this point I feel I am doing something very wrong and I need HELP!!!


RE: Google Safe Browsing API v4 implementation with C# returns null response - MavenGradle - Jan 2, 2020

According to the Google API documentation, a POST request should be sent like so:
Code
POST https://safebrowsing.googleapis.com/v4/threatMatches:find?key=API_KEY HTTP/1.1
Content-Type: application/json

Example request body:
Code
{
   "client": {
     "clientId":      "yourcompanyname",
     "clientVersion": "1.5.2"
   },
   "threatInfo": {
     "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
     "platformTypes":    ["WINDOWS"],
     "threatEntryTypes": ["URL"],
     "threatEntries": [
       {"url": "http://www.urltocheck1.org/"},
       {"url": "http://www.urltocheck2.org/"},
       {"url": "http://www.urltocheck3.com/"}
     ]
   }
 }

You should also note that the following text is available as well on the Google API docs:
Quote:Note: If there are no matches (that is, if none of the URLs specified in the request are found on any of the lists specified in a request), the HTTP POST response simply returns an empty object in the response body.

This applies to the response that you appear to have received.

You should also make sure that the type (threatType, platformType, and threatEntryType) combinations you specify are valid. There's a full list of them here:
https://developers.google.com/safe-browsing/v4/lists

You can find said API documentation here:
https://developers.google.com/safe-browsing/v4/lookup-api

In order to be able to fully help you, it would be easier if you could post a RAW example of the request headers and body that you're sending to the Google API. You can remove your API key regardless.


RE: Google Safe Browsing API v4 implementation with C# returns null response - ๖ۣۜchαmєllєσ - Jan 2, 2020

(Jan 2, 2020 11:18 AM)MavenGradle Wrote:  According to the Google API documentation, a POST request should be sent like so:
Code
POST https://safebrowsing.googleapis.com/v4/threatMatches:find?key=API_KEY HTTP/1.1
Content-Type: application/json

Example request body:
Code
{
   "client": {
     "clientId":      "yourcompanyname",
     "clientVersion": "1.5.2"
   },
   "threatInfo": {
     "threatTypes":      ["MALWARE", "SOCIAL_ENGINEERING"],
     "platformTypes":    ["WINDOWS"],
     "threatEntryTypes": ["URL"],
     "threatEntries": [
       {"url": "http://www.urltocheck1.org/"},
       {"url": "http://www.urltocheck2.org/"},
       {"url": "http://www.urltocheck3.com/"}
     ]
   }
 }

You should also note that the following text is available as well on the Google API docs:
This applies to the response that you appear to have received.

You should also make sure that the type (threatType, platformType, and threatEntryType) combinations you specify are valid. There's a full list of them here:
https://developers.google.com/safe-browsing/v4/lists

You can find said API documentation here:
https://developers.google.com/safe-browsing/v4/lookup-api

In order to be able to fully help you, it would be easier if you could post a RAW example of the request headers and body that you're sending to the Google API. You can remove your API key regardless.

the request body is there, from the method the guy i the link used is the same thing i used, the headers are automatically appended to the request headers with the gogle safebrowsing nuget package using the google.apis.safebrowsing namespace.


BUMP - Stanley - Jan 2, 2020

I'm Stanley HF's helper bot. This OP (original poster) of this thread wants everyone to see this page and has elected to bump it to the top of the showthread listing. Since HF doesn't want a ton of LQP (Low Quality Posts) this is the best method of bumping your content. There is a small βyte fee for each bump. Any OP can bump any of their threads on HF from the linked icon on the top of their first post.

If you haven't already read the first post of this thread we encourage you to do so.

Thanks,
Stanley