Dev.to Security 🔐 Cybersecurity 👁 0 📖 12 min read

When a Scanner Finds the Zero-Day Before You Can Fix It, Block What Your Tests Never Saw

In 2018 and 2019, attackers needed 63 days on average to exploit a vulnerability after its patch came out. Google's Mandiant team measures this every year. In 2021 and 2022 it was 32 days. In 2023 it was 5 days. For 2025

In 2018 and 2019, attackers needed 63 days on average to exploit a vulnerability after its patch came out. Google's Mandiant team measures this every year. In 2021 and 2022 it was 32 days. In 2023 it was 5 days. For 2025, the estimate is minus 7 days.

A negative number means that the average attack starts before the patch exists.

Most of that drop happened before AI did much in security. Now add AI.

  • August 2025. In the final of DARPA's AI Cyber Challenge, fully automatic systems found 54 of 63 planted vulnerabilities and patched 43. They also found 18 real ones that nobody had planted. The average cost was about $152 per task.
  • April 2026. Anthropic said that its Mythos Preview model had found thousands of zero-day vulnerabilities, many of them critical, "in every major operating system and every major web browser". One month later, Anthropic and about 50 partners reported more than 10,000 high or critical vulnerabilities that they had found with the model.

Both of these are the defending side, with a disclosure process and time for the vendors. But a skill like this does not stay on one side. I think we should plan for a world where an automatic scanner can find an unknown hole in almost any system, on any day, for little money.

Now look at how we defend. Someone reports the hole. Someone writes the fix. Someone reviews it, tests it and deploys it. There is a person in every step, and the whole loop takes days when it goes well. When the attack takes hours, the engineer has no time to react. It does not matter how good the engineer is.

So I asked another question. What kind of defense needs no time to react?

It must work without knowing the attack, because the attack is new. It must work without a person, because no person is fast enough. I see one design that fits both: do not look for bad things at all. Know what the system does when it works correctly, and stop everything else at the moment it happens.

I cannot build that for an operating system or a browser. I can try it for the layer that I write myself: the API. The result is BehaviorLock, an open source library for ASP.NET Core.

Block what is not proven

An API endpoint is a boring thing, and that is its strength. An endpoint that returns one order runs one SELECT on one table. It did that yesterday and it will do that tomorrow. It has no reason to ever read the Users table, and no reason to call a host it has never called.

So BehaviorLock writes down what each endpoint does, and blocks the rest. "Proven" has a simple meaning here: a test sent a legal request, and this is what the endpoint did.

  1. Record. Your integration tests send one legal request per legal path. The guard is in Learn mode and records what each endpoint touches.
  2. Commit. The result is a file, behavior.lock.json. It works like a package lock file, but it locks behavior, not versions.
  3. Enforce. In production the guard checks each access before it runs. An access that is not in the file never runs. The request gets a 403, and an alert is raised.

A part of the file from the sample app:

{
  "GET /admin/orders": [
    "caller: signed in, role admin",
    "response: up to 100 bytes",
    "rows: up to 10 from SELECT \"o\".\"Id\", \"o\".\"Total\", \"o\".\"UserId\" FROM \"Orders\" AS \"o\"",
    "sql: SELECT \"o\".\"Id\", \"o\".\"Total\", \"o\".\"UserId\" FROM \"Orders\" AS \"o\""
  ],
  "GET /products/search": [
    "caller: anonymous",
    "response: up to 100 bytes",
    "rows: up to 10 from SELECT * FROM Products WHERE Name LIKE ?",
    "sql: SELECT * FROM Products WHERE Name LIKE ?"
  ],
  "POST /orders": [
    "caller: anonymous",
    "http: POST https://payments.example.com",
    "response: up to 100 bytes",
    "rows: up to 1 from INSERT INTO \"Orders\" (\"Total\", \"UserId\") VALUES (?) RETURNING \"Id\"",
    "sql: INSERT INTO \"Orders\" (\"Total\", \"UserId\") VALUES (?) RETURNING \"Id\""
  ]
}

Each line is one thing that the endpoint may do:

Entry What it holds
sql The shape of a statement: the SQL text with every value replaced by ?
http The method, scheme, host and port of an outbound call
caller The kind of caller: anonymous, or signed in with a list of roles
rows The largest number of rows that one command returns
response The largest size of the response body
redis The Redis command and the shape of each key, for example StringGet user:?:cart

The guard has no list of attacks. It cannot be out of date, because it has nothing to update. A new attack technique is still blocked on its first day, as long as it makes an endpoint touch something new, take a new kind of caller, or return more than it ever did.

What I have today

Version 0.3.0 is on NuGet, for .NET 10 and .NET 11.

dotnet add package BehaviorLock

The setup is three calls:

builder.Services.AddBehaviorLock(builder.Configuration.GetSection("BehaviorLock"));

builder.Services.AddDbContext<ShopDb>((services, options) => options
    .UseSqlite(connectionString)
    .UseBehaviorLock(services));

app.UseBehaviorLock();

That covers EF Core and every HttpClient of the process, also a client that a package creates by itself, and each redirect hop. An app with Dapper or plain ADO.NET wraps its connection, and the code that uses the connection does not change:

builder.Services.AddScoped<DbConnection>(services =>
    new NpgsqlConnection(connectionString).WithBehaviorLock(services));

A second package, BehaviorLock.StackExchangeRedis, does the same for Redis commands.

The repo has a small shop API with weaknesses built in on purpose. The tests run each attack twice: with the guard off, to prove the harm is real, and in Enforce mode, to prove it is stopped.

Weakness Request What the guard sees
SQL injection in a string UNION SELECT ... FROM Users New SQL shape
SQL injection in a string '; DELETE FROM Users; -- A second statement
SQL injection in login [email protected]' -- New SQL shape
Blind SQL injection in ORDER BY CASE WHEN (SELECT ...) THEN Name ELSE Price END New SQL shape
Mass assignment PATCH /users/1 with "IsAdmin": true UPDATE sets a new column
Plain bug DELETE /orders/0 DELETE without WHERE
Forgotten debug switch GET /orders/1?debug=true The endpoint reads a new table
SSRF url=http://169.254.169.254/... New outbound host
SSRF Another port on a legal host New outbound port
Compromised dependency An order with a high total New outbound host
Injection that keeps the shape % in a LIKE value More rows than a legal search
Missing role check A normal user calls GET /admin/orders New kind of caller

Twelve rows, and the guard has no rule for any of them. Three rows show the point best.

The debug switch. Someone left a ?debug=true in the order endpoint, and it returns every user with their password. There is nothing bad in that request. No quote, no payload, nothing a signature could match. The guard does not look at the request. It sees that an endpoint which reads Orders now wants to read Users. The request gets a 403, and the log has this line:

BehaviorLock violation (blocked): endpoint 'GET /orders/{id:int}' attempted
'sql: SELECT "u"."Id", "u"."DisplayName", "u"."Email", "u"."IsAdmin", "u"."Password" FROM "Users" AS "u"'
in request 0HNQEDMR934CV:00000001

The dependency. The payment client in the sample stands for a package that was taken over. It works as expected, but it copies every order above 1000 to another host. The tests place an order of 120, so no test ever sees that code run. This is the closest thing to a zero-day at this layer: bad code inside something you trust, on a path that nobody has looked at. In production the first large order tries to call a host that is not in the profile. The call never leaves the server.

The missing role check. GET /admin/orders only asks for a signed-in user. Somebody forgot the role. The tests call it as an admin, because that is the legal use. So the profile says caller: signed in, role admin, and a normal user gets a 403 from the guard. Nobody wrote that rule. The tests wrote it.

The cost is small. In my load test on a local SQLite file, which is the worst case for the guard, the throughput in Enforce mode was at most about 4% lower than without the package. The latency percentiles differed by 0.3 ms or less. The check of a SQL text that the guard has seen before takes about 0.4 microseconds.

Where the person went

The blocking needs no person. That was the goal.

Learning new legal behavior does need a person, and that is on purpose. Tests never cover every legal path, so the default mode is Audit: report, but allow. The guard writes each unknown access to a pending file, and all instances of the app can share that one file. A person reads it and decides: this is a legal path that the tests missed, or this is an attack. Legal paths get a test and move into the profile. When the pending file stays empty, the endpoint goes to Enforce. Each endpoint can have its own mode, so this can happen one endpoint at a time.

If the guard could approve new behavior by itself, an attacker would teach it. So the human work did not go away. It moved. It now happens before the attack, in a pull request where the profile diff shows that an endpoint touches something new. And it happens after the attack, in a review of what was blocked. It does not happen during the attack, when there is no time.

One thing can still go wrong at night: the guard blocks legal users. For that case the mode is a setting that the running app reads again. A change from Enforce to Audit needs no deploy.

The guard is code too

A world where scanners find holes everywhere includes the hole in my guard. I had one in the first version.

To turn WHERE Name = 'alice' into WHERE Name = ?, the guard must find the end of the string at the same place as the database. The first version used the rule of standard SQL. MySQL, by default, reads a backslash as an escape. So take a query that is built from two form fields, send one backslash as the name, and send OR 1=1 -- as the city:

SELECT Name FROM Users WHERE Name = '\' AND City = ' OR 1=1 -- '

The guard saw two strings and the legal shape, WHERE Name = ? AND City = ?. MySQL saw one string, '\' AND City = ', then OR 1=1, and returned every row. The attacker did not need a single quote character.

A backslash rule does not fix it, because MySQL and PostgreSQL can turn that escape on and off with a server setting, and the guard cannot see the setting. So the guard now reads each string both ways. When the two readings end at different places, it does not normalize the command. It keeps the whole text, which can then only match the exact same text in the profile.

I did not want to trust my own reading of the manuals again, so the repo has a test where the database is the judge. It builds commands from random pieces, runs each one on a real server, and counts the rows. A command that returns every row ran injected code, so its shape must differ from the legal shape.

First version Current version
Random commands on MySQL 8.4 and PostgreSQL 17 80,000 80,000
Commands that returned every row 8,334 8,334
Of those, with the legal shape 58 0

58 of 8,334 is 0.7%. It looks small. A random test tries things blind. An attacker with a good model needs one of the 58, and will find it.

What the guard cannot see, the network can

The guard runs inside the app process. Two things are out of its reach. A raw socket or native code does not pass through HttpClient. And an attacker who runs code in the process can turn the guard off.

The network sees both. And the profile already names every host that the app calls. So a small tool turns the profile into an allowlist for the network:

dotnet tool install --global BehaviorLock.Tool
behaviorlock egress behavior.lock.json --allow db.internal:5432
db.internal:5432
hooks.example.com:443
payments.example.com:443

It can also write a CiliumNetworkPolicy for Kubernetes. The network then drops every connection to another address, whatever code opens it. This list is for the whole app and not for one endpoint, so it does not replace the guard. The two work together: the guard is exact, and the network cannot be turned off from inside the process.

Pros and cons

What speaks for it

  • It needs no knowledge of the attack. The first use of a new technique is blocked like the thousandth.
  • It needs no person and no patch at the moment of the attack.
  • It also stops things that are not attacks: a bug that deletes a table, a debug switch, a package that changed.
  • The profile is in git. A change in what an endpoint touches is a line in a diff that a reviewer can question.
  • It is cheap at run time.

What speaks against it

  • An attack that stays inside the profile passes. Reading the order of another user has the same SQL shape and the same kind of caller as reading your own. Business logic errors, XSS and most DoS attacks are invisible to it. The repo keeps two such cases in a test class named KnownLimitTests, so nobody forgets them.
  • It guards my endpoints, not the operating system, the runtime or the web server below them. Most of the vulnerabilities from the first section are down there.
  • It sees EF Core, guarded ADO.NET connections, HttpClient and StackExchange.Redis. It does not see file access, a process start, or a database connection that the code opens by itself. A stored procedure is only a name for it.
  • It is as good as your tests. A legal path that no test covers gets a 403 in Enforce mode. Audit mode and the pending file exist for that reason, and they cost review time.
  • The callers and the limits come from your test users and your test data. A small test database gives low limits, and they need a review before they block.
  • The profile records what the code does, not what it should do. If a harmful path runs during recording, it becomes legal.
  • The SQL part is a small scanner, not a parser. The random test finds many errors. It does not prove that there are none.

So this does not replace parameters, authorization checks or patching. It is a layer for the hours when all of those are late.

What is still open

  • File access and process start are not checked.
  • The SQL Server rules come from its documentation. They were not compared with a real server, as the rules for MySQL and PostgreSQL were.
  • Moving a reviewed entry from the pending file to the approved file is done by hand. There is no tool for it yet.
  • Instances that share no volume need their own store for the pending accesses. The package has the interface for it, but no ready store for a database.
  • The version is 0.3.0, and the library is young. I will call it 1.0 when a real app has run in Enforce mode for some weeks, and when the format of the profile has stopped changing.

The question

I do not know if the world of the first section arrives in one year or in five. I do think that "find, report, patch, deploy" will lose to a scanner that needs an hour. Something has to stand in the request path that needs no person and no update.

BehaviorLock is my attempt at one piece of that. The code is at github.com/valbarov/BehaviorLock, under Apache-2.0. If you find a request that makes an endpoint do something new and still passes, please report it. SECURITY.md says how. That is the bug I most want to hear about.

If a scanner found a hole in your API tonight, what would stop the request before anyone on your team is awake?

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.