TL;DR
You can serve different content to users based on their browser and operating system using JavaScript’s navigator.userAgent property. This allows you to test attacks across multiple platforms from a single link, or deliver tailored payloads.
How it Works
The navigator.userAgent string contains information about the user’s browser and OS. We can parse this string in JavaScript to identify the platform and then load different code or redirect the user accordingly.
Steps
- Create an HTML file: Start with a basic HTML structure.
- Add JavaScript Code: Include a
<script>tag to contain your JavaScript code. This is where the magic happens. - Parse User Agent: Use
navigator.userAgentto get the user agent string. - Identify Browser and OS: Write conditional statements (
if/else if/else) to check for specific browser or OS keywords in the user agent string. - Load Different Content: Based on the identified platform, load different JavaScript code, CSS styles, or redirect the user to a different URL.
Example Code
<!DOCTYPE html>
<html>
<head>
<title>Browser/OS Targeting</title>
</head>
<body>
<script>
const userAgent = navigator.userAgent;
if (userAgent.indexOf("Firefox") > -1) {
// Firefox specific code
document.write("You are using Firefox!");
} else if (userAgent.indexOf("Chrome") > -1) {
// Chrome specific code
document.write("You are using Chrome!");
} else if (userAgent.indexOf("Windows NT") > -1) {
// Windows specific code
document.write("You are on Windows!");
} else if (userAgent.indexOf("Mac OS X") > -1) {
// macOS specific code
document.write("You are on macOS!");
} else {
// Default content for other browsers/OSs
document.write("Unknown browser/OS.");
}
</script>
</body>
</html>
More Robust Detection
The above example is basic. For more accurate detection, consider these points:
- Case Sensitivity: User agent strings can be case-sensitive. Use
toLowerCase()for consistent comparisons (e.g.,userAgent.toLowerCase().indexOf("firefox") > -1). - Version Numbers: Check specific browser versions if needed.
- Mobile Detection: Include checks for mobile devices (e.g.,
userAgent.indexOf("Android") > -1oruserAgent.indexOf("iPhone") > -1). - Libraries: Consider using JavaScript libraries like UserAgentString for more advanced parsing and detection capabilities.
Redirecting Users
Instead of displaying different content, you can redirect users to different URLs:
if (userAgent.indexOf("Chrome") > -1) {
window.location.href = "https://example.com/chrome";
} else if (userAgent.indexOf("Firefox") > -1) {
window.location.href = "https://example.com/firefox";
}
Security Considerations
User-Agent Spoofing: The navigator.userAgent string can be easily spoofed by users or browser extensions. Do not rely solely on this information for critical security decisions. This technique is best used for testing and targeted content delivery, not for robust authentication or authorization.
cyber security implications are significant if relying on user agent alone to determine trust levels.

