Sunday, November 06, 2022

Using IP geolocation to detect suspicious logins to GSuite

Today I will describe how to detect suspicious login activities to Google Workspace.

I am using AdminReports.Activities.list API ( from my Apps Script script ( To limit results I will filter only login applications ( According to documentation the Login application’s activity reports return information about different types of Login activity events. The most important events are: login_failure and login_success.

Below function returns 4 fields (email of user, event name, event time and source IP address). we have to provide two parameters to our function - start date and end date. It is important to store end date in order to provide this value during next call.

function Logins(starts, ends) {

    var pageToken, responseX;

    var userKey = "all";

    var applicationName = "login";

    var table = [];

    do {

        var optionalArgs = {

            maxResults: 20,

            startTime: starts,

            endTime: ends,

            pageToken: pageToken,


        responseX = AdminReports.Activities.list(





        var activities = responseX.items;

        if (activities && activities.length > 0) {

            for (i = 0; i < activities.length; i++) {

                var activity = activities[i];

                if (

          [0].name == "login_failure" ||

          [0].name == "login_success"









        } else {

            Logger.log("No logins found.");


        pageToken = responseX.nextPageToken;

    } while (pageToken);

    return table;


I also had to create function responsible for retrieving GEO IP values (City, Country and continent). I decided to us database via API but you can choose any other geo ip database.

function example_function(){

var result_from_logins = Logins(“2022-10-06T19:23:13.280Z”, “2022-10-07T19:23:13.280Z”);

var geo_ip_data = getIpGeolocationData(result_from_logins[counter][3],'CITY, COUNTRY');



A part of function getIpGeolocationData is presented below:

ipData = isBlank(ip)? getResponseJsonData('') : getResponseJsonData('' + ip);

Result is returned as JSON. In Apps Script fetch method is used from UrlFetchApp class to call ipbase API.

function getResponseJsonData(url) {

    if (isBlank(url)) return;

    let response = UrlFetchApp.fetch(url, {

        headers: {

            apikey: APIKEY,



    let json = response.getContentText();

    return JSON.parse(json);


Results can be written to spreadsheet or send directly to the SOC team. I am writing data to spreadsheet where each tab contain suspicious activities from one day. It is also possible to filter countries and email address to limit false alarms then SOC team can focus only on admin accounts and logon events from some countries.




Cloud Security

I come back after a quite long break in publishing posts. During last 4 years I was engaged in building a bank in the cloud. SSDLC, security of CI/CD, workspace security including mobile devices, cloud security, k8s security, security monitoring, access control or identity protection are in area on my responsibility. I am going to publish at this site posts related to listed topics. Source code used by me is stored at this repo:

As you can see I’ve also changed domain name to the .cloud as I will mostly focus on cloud security topics.