Understanding PAC Files

PAC files, short for Proxy Auto-Configuration files, are critical in dictating how web traffic is directed through or around proxy servers. In this blog post, we will delve into the technical aspects of how PAC files operate and affect packet flow in a network.

What Are PAC Files?

Before diving into packet flows, let’s establish what PAC files are. PAC files are JavaScript scripts used by web browsers to determine whether web requests should be forwarded through a proxy server or directly to the destination. These files are instrumental in corporate environments where network traffic needs to be managed efficiently.

A typical PAC file defines a JavaScript function `FindProxyForURL(url, host)`, which returns a string with instructions on how the browser should route its request. For example:

function FindProxyForURL(url, host) {
    if (shExpMatch(host, "codero.me")) {
        return "PROXY proxyserver:port";
    }
    if (shExpMatch(host, "cordero.ai")) {
        return "DIRECT";
    }
    return "DIRECT";
}

This PAC file directs traffic to codero.me through a proxy server (you need to replace proxyserver:port with the actual proxy server address and port) and bypasses the proxy for cordero.ai by connecting directly. Any other traffic also connects directly.

How Do PAC Files Affect Packet Flow?

Let’s break down the packet flow involving a PAC file step by step:

Step 1: Browser Startup or Network Connection Establishment

When a browser starts up, or a device establishes a network connection, it checks if a PAC file is configured. The browser can be manually configured to use a specific PAC file or the Web Proxy Auto-Discovery Protocol (WPAD) to automatically discover the PAC file on the network.

Step 2: PAC File Retrieval

The browser sends a request to the server hosted by the PAC file. This could be a local server within the organization or an external server. The browser then downloads the PAC file.

Step 3: Processing the PAC File

Whenever the browser requests a URL, it executes the `FindProxyForURL` function in the PAC file. The process evaluates the URL and host against the rules defined in the PAC file.

Step 4: Routing the Request

Based on the output of the `FindProxyForURL` function, the browser decides how to route the request.

1. Through a Proxy Server: If the function returns a proxy server (e.g., “PROXY proxy.example.com:8080“), the browser sends the request to the specified proxy server. The proxy server then makes the request on behalf of the browser. Once the proxy receives the response, it returns it to the browser.

2. Direct Connection: If the function returns “DIRECT”, the browser directly connects to the destination without going through a proxy.

Step 5: Receiving the Response

Finally, the browser receives the response directly from the destination server or the proxy server and renders the content for the user.

Distribution Using Microsoft AD Group Policy Objects (GPO)

In corporate environments, especially those using Microsoft Active Directory, PAC files are often distributed using Group Policy Objects (GPO). Administrators can create a GPO that specifies the URL of the PAC file and assign it to Active Directory groups. This approach allows centralized control over network traffic without configuring each user’s browser settings individually.

Once the GPO is applied, the Internet browser’s configuration settings on the user’s machine are automatically updated to use the PAC file. For example, the proxy settings will point to the PAC file specified in the GPO in Internet Explorer or Microsoft Edge.

The Benefits of PAC Files

  • Granular Control: PAC files allow network administrators to have granular control over network traffic, routing specific requests through proxy servers and allowing others to bypass them.
  • Centralized Management: By managing a single PAC file, administrators can control the proxy settings of multiple devices across the organization.
  • Efficient Bandwidth Usage: PAC files can be used to route traffic through caching proxies, reducing bandwidth usage.
  • Security: By directing traffic through proxy servers, PAC files can add an additional layer of security.

Customizability

PAC files can be completely customized to suit specific requirements. They offer a wide range of options and flexibility for defining proxy configuration rules based on various criteria. Some of the options and functions available in PAC files include:

  • `isPlainHostName(host)`: Returns true if the `host` parameter does not contain any dots, indicating a plain hostname without a domain.
  • `dnsDomainIs(host, domain)`: Returns true if the rightmost component of `host` matches the specified `domain` completely.
  • `shExpMatch(str, pattern)`: Performs pattern matching for `str` against a shell wildcard pattern specified in `pattern`.
  • `isInNet(host, pattern, mask)`: Checks if the IP address of `host` falls within the specified IP `pattern` and `mask` range.
  • `dnsResolve(host)`: Resolves the hostname `host` to its corresponding IP address.
  • Conditional statements (`if`, `else`, `else if`): Allows defining different proxy configurations based on conditions and rules.
  • Proxy server configurations: You can specify the proxy server address and port to be used, such as `PROXY proxy.example.com:8080`.

With these options and functions, you can create custom logic and rules within the `FindProxyForURL` function of a PAC file to determine how requests should be routed through proxies or bypassed.

It’s important to note that PAC files have their own syntax and functions specific to proxy auto-configuration. The available options and functions may vary slightly across different implementations and versions of web browsers. It is recommended to refer to the documentation of the web browser or proxy server software you are using for complete details on available options and functions.

Example 1: (FQDN Based)

Configure the PAC file to route traffic for cordero.me and cordero.ai through the Bluecoat Proxy while bypassing the proxy for other destinations, you can modify the PAC file’s FindProxyForURL function as follows:

function FindProxyForURL(url, host) {
    // Proxy configuration for specific domains
    if (dnsDomainIs(host, "cordero.me") ||
        dnsDomainIs(host, "cordero.ai")) {
        return "PROXY proxy.example.com:8080";
    }

    // Direct connection for all other destinations
    return "DIRECT";
}

In this configuration:

Requests made to cordero.me and cordero.ai will be routed through the Bluecoat Proxy (specified as bluecoat-proxy.example.com:8080 in this example).

All other destinations will bypass the proxy and use a direct connection (DIRECT).

Please make sure to replace bluecoat-proxy.example.com:8080 with the actual hostname and port of your Bluecoat Proxy server.

Example 2: (Destination Subnets)

A PAC file can examine the destination IP subnet and make decisions based on it. Here’s an example PAC file configuration where the destination IP ranges of `198.51.100.0/24` and `203.0.113.0/24` will use the proxy:

function FindProxyForURL(url, host) {
    // Proxy configuration for specific IP ranges
    if (isInNet(host, "198.51.100.0", "255.255.255.0") ||
        isInNet(host, "203.0.113.0", "255.255.255.0")) {
        return "PROXY proxy.example.com:8080";
    }

    // Direct connection for all other destinations
    return "DIRECT";
}

In this example:

1. Traffic directed towards the IP range `198.51.100.0/24` or `203.0.113.0/24` will use the proxy configuration specified as `PROXY proxy.example.com:8080`.

2. All other destinations will bypass the proxy and use a direct connection (`DIRECT`).

Please replace `proxy.example.com:8080` with the actual hostname and port of your proxy server.

Conclusion

PAC files efficiently and effectively manage web traffic in network environments. Using JavaScript functions, network administrators can create complex rules to ensure network traffic is routed optimally. The integration with Microsoft Active Directory and GPOs further enhances the utility of PAC files, making them a robust tool for network traffic management and optimization strategies.