
Research
Using Trusted Protocols Against You: Gmail as a C2 Mechanism
Socket uncovers malicious packages on PyPI using Gmail's SMTP protocol for command and control (C2) to exfiltrate data and execute commands.
π€/π¨βπ¦° Recognise bots/crawlers/spiders using the user agent string.
The 'isbot' npm package is a lightweight utility for detecting bots, crawlers, and spiders based on the user agent string. It helps developers identify non-human traffic to their websites or applications.
Basic Bot Detection
This feature allows you to check if a given user agent string belongs to a bot. The function returns true if the user agent is identified as a bot, otherwise false.
const isBot = require('isbot');
const userAgent = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
console.log(isBot(userAgent)); // true
Custom Bot Patterns
You can extend the default bot detection patterns with custom patterns. This is useful if you have specific bots that are not covered by the default list.
const isBot = require('isbot');
isBot.extend(['my-custom-bot']);
const userAgent = 'my-custom-bot';
console.log(isBot(userAgent)); // true
Bot Detection in HTTP Requests
This feature demonstrates how to use 'isbot' to detect bots in incoming HTTP requests. Depending on whether the user agent is a bot, the server responds with a different message.
const isBot = require('isbot');
const http = require('http');
http.createServer((req, res) => {
if (isBot(req.headers['user-agent'])) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, bot!');
} else {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello, human!');
}
}).listen(3000);
The 'useragent' package provides detailed parsing of user agent strings, including bot detection. It offers more comprehensive information about the user agent, such as browser, version, and OS, but may be more complex to use compared to 'isbot'.
The 'express-useragent' package is an Express middleware for parsing user agent strings. It includes bot detection and provides additional details about the user agent. It is specifically designed for use with Express.js applications.
The 'ua-parser-js' package is a JavaScript library for parsing user agent strings. It includes bot detection and provides detailed information about the browser, engine, OS, and device. It is a more general-purpose library compared to 'isbot'.
Identify bots, crawlers, and spiders using the user agent string.
Install
npm i isbot
Straightforward usage
import { isbot } from "isbot";
// Request
isbot(request.headers.get("User-Agent"));
// Nodejs HTTP
isbot(request.getHeader("User-Agent"));
// ExpressJS
isbot(req.get("user-agent"));
// Browser
isbot(navigator.userAgent);
// User Agent string
isbot(
"Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
); // true
isbot(
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36",
); // false
Use JSDeliver CDN you can import to the browser directly
See specific versions and instructions https://www.jsdelivr.com/package/npm/isbot
ESM
<script type="module">
import { isbot } from "https://cdn.jsdelivr.net/npm/isbot@5/+esm";
isbot(navigator.userAgent);
</script>
UMD
<script src="https://cdn.jsdelivr.net/npm/isbot@5"></script>
<script>
// isbot is now global
isbot(navigator.userAgent);
</script>
import | Type | Description |
---|---|---|
isbot | (string?): boolean | Check if the user agent is a bot |
isbotNaive | (string?): boolean | Check if the user agent is a bot using a naive pattern (less accurate) |
getPattern | (): RegExp | The regular expression used to identify bots |
list | string[] | List of all individual pattern parts |
isbotMatch | (string?): string | null | The substring matched by the regular expression |
isbotMatches | (string?): string[] | All substrings matched by the regular expression |
isbotPattern | (string?): string | null | The regular expression used to identify bot substring in the user agent |
isbotPatterns | (string?): string[] | All regular expressions used to identify bot substrings in the user agent |
createIsbot | (RegExp): (string?): boolean | Create a custom isbot function |
createIsbotFromList | (string[]): (string?): boolean | Create a custom isbot function from a list of string representation patterns |
Create a custom isbot
that does not consider Chrome Lighthouse user agent as bots.
import { createIsbotFromList, isbotMatches, list } from "isbot";
const ChromeLighthouseUserAgentStrings: string[] = [
"mozilla/5.0 (macintosh; intel mac os x 10_15_7) applewebkit/537.36 (khtml, like gecko) chrome/94.0.4590.2 safari/537.36 chrome-lighthouse",
"mozilla/5.0 (linux; android 7.0; moto g (4)) applewebkit/537.36 (khtml, like gecko) chrome/94.0.4590.2 mobile safari/537.36 chrome-lighthouse",
];
const patternsToRemove = new Set<string>(
ChromeLighthouseUserAgentStrings.map(isbotMatches).flat(),
);
const isbot: (ua: string) => boolean = createIsbotFromList(
list.filter(
(record: string): boolean => patternsToRemove.has(record) === false,
),
);
Create a custom isbot that considers another pattern as a bot, which is not included in the package originally.
import { createIsbotFromList, list } from "isbot";
const isbot = createIsbotFromList(list.concat("shmulik"));
This package aims to identify "Good bots". Those who voluntarily identify themselves by setting a unique, preferably descriptive, user agent, usually by setting a dedicated request header.
It does not try to recognise malicious bots or programs disguising themselves as real users.
Recognising good bots such as web crawlers is useful for multiple purposes. Although it is not recommended to serve different content to web crawlers like Googlebot, you can still elect to
It is not recommended to whitelist requests for any reason based on user agent header only. Instead, other methods of identification can be added such as reverse dns lookup.
isbot
maintains accuracyisbot
is an asset when it can most accurately identify bots by the user agent string. It uses expansive and regularly updated lists of user agent strings to create a regular expression that matches bots and only bots.
And above everything else, it is maintained by a community of contributers who help keep the list up to date.
The pattern uses lookbehind methods which are not supported in all environments. A fallback is provided for environments that do not support lookbehind. The fallback is less accurate. The test suite includes a percentage of false positives and false negatives which is deemed acceptable for the fallback: 1% false positive and 75% bot coverage.
We use external data sources on top of our own lists to keep up to date
Missing something? Please open an issue
Remove named export "pattern" from the interface, instead use "getPattern" method
Remove isbot
function default export in favour of a named export.
import { isbot } from "isbot";
Remove testing for node 6 and 8
Change return value for isbot: true
instead of matched string
No functional change
FAQs
π€/π¨βπ¦° Recognise bots/crawlers/spiders using the user agent string.
The npm package isbot receives a total of 912,539 weekly downloads. As such, isbot popularity was classified as popular.
We found that isbot demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Socket uncovers malicious packages on PyPI using Gmail's SMTP protocol for command and control (C2) to exfiltrate data and execute commands.
Product
We redesigned Socket's first logged-in page to display rich and insightful visualizations about your repositories protected against supply chain threats.
Product
Automatically fix and test dependency updates with socket fixβa new CLI tool that turns CVE alerts into safe, automated upgrades.