Finding the hardcoded credentials in a legacy app before the audit does

A security or audit finding is one of the common reasons a legacy system finally gets attention. The finding is often the same: credentials in plain text. Connection strings sit in web.config, in global.asa, in an .ini file next to a VB6 EXE, or in a string constant nobody has opened since 2009. This note shows how to find them all, rank them, and move them out before the first .NET 8 slice ships. You do not need to migrate anything first.

Step 1: Find every place a secret can live

Search the whole repository and the deployed folders, not just the repository. Deployed copies often differ from source.

StackWhere to look
WebFormsweb.config, appSettings, connectionStrings, *.config includes, machine.config on the server
Classic ASPglobal.asa, include/*.inc, conn.asp-style files, hard-coded Server.CreateObject("ADODB.Connection") strings
VB6.ini files, registry keys under HKLM\SOFTWARE, Const declarations in .bas modules, compiled resources
Accesslinked-table connection strings (read them from MSysObjects), saved pass-through queries, CurrentDb code
FoxProconfig.fpw, .h header files, SQLSTRINGCONNECT() calls

A first pass with plain grep is enough:

grep -rniE "(password|pwd|uid|user id|apikey|api_key|secret|token)\s*[=:]" \
  --include=*.config --include=*.asp --include=*.inc --include=*.asa \
  --include=*.bas --include=*.frm --include=*.cls --include=*.ini --include=*.prg .

Expect noise. Expect to miss things too. Strings built in pieces, such as "Pwd=" & prefix & "x9", will not match. Read the hits by hand.

Step 2: Check the history, not only the current files

Run a secret scanner over the git or SourceSafe/TFVC history. gitleaks and trufflehog both work on git history. If the history is in an older system, export it to git first. Anything that was ever committed should be treated as known to everyone who had repository access, including past contractors.

Step 3: Make a ledger

Write one row per secret. Keep the ledger outside the repository.

  • What it is: SQL login, SMTP account, payment gateway key, FTP password, third-party API token.
  • Where it is used and by which component.
  • Who owns the other end and can rotate it.
  • Whether it is shared across environments. Production and test using the same SQL login is common.
  • Rotation risk: what breaks when it changes.

The last column decides your order. A SQL login used by one web app is easy to rotate. A password baked into a compiled VB6 EXE that runs on six branch-office PCs is not.

Step 4: Fix the cheap ones in the legacy app

Some fixes do not need a rewrite:

  1. Create a separate least-privilege SQL login per application. Many legacy apps connect as sa or as a db_owner. Stop that first.
  2. In WebForms, encrypt the connectionStrings section with aspnet_regiis -pef. Know that this ties the file to the machine key, so keep the machine key in your deployment notes.
  3. Where the database is SQL Server and the app is on a domain-joined server, move to a Windows account for the app pool and drop the password altogether.
  4. Remove secrets from source control going forward, and rotate every one that appeared in history.

Step 5: Give the new .NET 8 slice a different path

The new slice should never read a secret from a file in the repository. Pick one source per environment:

  • Development: dotnet user-secrets.
  • Production: environment variables set by the host, or a vault such as Azure Key Vault, loaded with the standard configuration providers.
builder.Configuration.AddEnvironmentVariables();
var cs = builder.Configuration.GetConnectionString("Orders")
    ?? throw new InvalidOperationException("Orders connection string is not configured.");

Fail at startup if a value is missing. A silent fallback to a default is how test credentials reach production.

When the slice sits behind a routing shim, the shim and the legacy app should not share a secret store they cannot both rotate. Keep the two configurations separate.

Step 6: Prove nothing changed

Rotating credentials is a behavior change you can break. Run your characterization tests before and after each rotation. A failing test after a rotation usually means a forgotten second consumer, such as a scheduled job. Check the overnight batch jobs by name, because they are the most likely forgotten consumer.

If you hand source to an AI tool

If you use a coding agent on the codebase, scrub the ledger's secrets from the working copy first. See setting up a coding agent on a legacy codebase. The agent should never see production credentials, and it should not need them.

What we would not do

We would not rotate every secret in one weekend. Rotate in the order of the ledger, one consumer at a time, with the tests running. And we would not call the system clean after a grep. The grep gives you a starting list, not a guarantee.

If you cannot say how many secrets your system holds or where, that count is a good early deliverable of a codebase assessment.