Sub topic 13
Hands-on diagnostic commands
This chapter covers the command-line checks you can run when a phone or PC may be compromised. It assumes you know the symptoms covered in earlier chapters (high data use, pop-ups, redirects, altered files), and that you can open an elevated Command Prompt on Windows and enable USB debugging on Android. The A+ exam rarely asks you to type these commands. It does expect you to know what each one shows and when to use it. The commands gather evidence. They do not remove malware.
Where commands fit in the process
Commands belong in the "establish a theory" and "test the theory" steps of the CompTIA troubleshooting method. A symptom gives you a theory, such as "an unknown app is running" or "the hosts file redirects my bank". A command confirms or rules out that theory without changing the system. You then plan the fix, apply it, verify it, and document it.
Android checks with adb
The Android Debug Bridge (adb) is a tool that runs on a PC and talks to an Android device over USB. It needs two things. The device must have developer options and USB debugging turned on, and you must approve the PC's key on the device when prompted. Because USB debugging is a risk when left on, turn it off after the check.
Confirm that the device is connected first:
adb devicesThe output lists each device and its state. A state of unauthorized means you have not approved the PC on the phone yet.
List third-party apps
adb shell pm list packages -3pm is the package manager. The -3 flag limits the list to third-party apps, which means apps the user installed, not those that shipped with the system. Each line looks like package:com.example.app. Look for names you do not recognize, apps with generic names such as "security update", or apps the user says they never installed. These can point to sideloading or a spoofed app. To see where a suspicious package came from, ask the package manager about it:
adb shell pm list packages -3 -iThe -i flag adds the installer. An app installed by the Play Store shows installer=com.android.vending. A value of null or a file manager points to sideloading.
Check battery health
adb shell dumpsys batterydumpsys prints the state of a system service. For the battery service you get the charge level, whether the device is charging, and the temperature. The temperature appears in tenths of a degree Celsius, so 420 means 42.0 °C. A phone that is hot while idle and loses charge quickly may have a runaway app, possibly malware such as a cryptominer. It may also have a failing battery. The command alone cannot tell these apart, so compare the result with the app list and the per-app battery usage in Settings.
Read the system log
adb logcat -dlogcat shows the Android system log. The -d flag dumps what is in the log buffer and then exits. Without it, the command keeps streaming until you stop it. Search the output for the app's name and look for the same error or crash repeating. An app that crashes over and over, or that restarts itself after being closed, is a fault worth investigating. It could be a bad update or something hostile.
| Command | What it shows | Typical symptom it supports |
|---|---|---|
| `adb shell pm list packages -3` | User-installed apps | Unknown or sideloaded app |
| `adb shell dumpsys battery` | Charge, status, temperature | Battery drain, overheating |
| `adb logcat -d` | Recent system log | App that crashes or will not close |
Windows checks
These commands run in Command Prompt. Open it as administrator for sfc. The other two run without elevation, though netstat -b (not used here) would need it.
Inspect the hosts file
type %SystemRoot%\System32\drivers\etc\hostsThe hosts file maps names to IP addresses and is checked before DNS. Malware edits it to send a real site name to an attacker's server. This gives the browser symptom of a known site that loads a fake page or fails to load.
A clean file is mostly comments. Lines starting with # are comments. Look for any active line like this:
203.0.113.50 www.examplebank.comAn active line that points a bank, update server, or antivirus vendor to an unfamiliar address is suspicious. Many malware families also block security vendor sites this way, so a user who "cannot reach the antivirus website" is a strong clue. Note that a default hosts file may contain no active lines at all, and that is normal.
Match connections to processes
netstat -anoThe flags mean: -a shows all connections and listening ports, -n shows numbers instead of resolving names (faster and clearer), and -o adds the process ID (PID) that owns each connection. Read the columns this way:
| Column | Meaning |
|---|---|
| Proto | TCP or UDP |
| Local Address | This PC's IP and port |
| Foreign Address | The remote IP and port |
| State | For example ESTABLISHED or LISTENING |
| PID | The owning process |
Find an ESTABLISHED connection to an address you do not recognize, note its PID, and open Task Manager. On the Details tab, sort by PID to find the process. If the process name is odd, or a familiar name runs from an unusual folder, you have a lead. A connection alone is not proof of malware. Browsers and update services make many legitimate connections.
Verify system files
sfc /scannowSystem File Checker (SFC) compares protected Windows files against cached good copies and replaces any that differ. Run it from an elevated prompt. It takes several minutes. The result is one of three messages:
- No integrity violations were found.
- Corrupt files were found and repaired.
- Corrupt files were found but some could not be repaired.
Use SFC after you remove malware, because malware sometimes damages system files. It is also useful when the symptom is an altered or missing operating system file. If SFC cannot repair files, the next tools are DISM /Online /Cleanup-Image /RestoreHealth and, as a last resort, a reset or reinstall.
Common errors
- Running the command without admin rights.
sfcreports that you must be an administrator. Reopen Command Prompt with "Run as administrator". - Forgetting to approve USB debugging.
adb devicesshowsunauthorized, and every other adb command fails. - Reading temperature wrongly. The battery value is in tenths of a degree, not degrees.
- Treating a hosts file as infected because it has comments. Only uncommented lines have an effect.
- Blaming a process by name alone. Check the file location and the signer as well, because malware often imitates real process names.
- Skipping quarantine. Running these checks on a PC that is still on the network lets malware keep communicating. Disconnect it first if you suspect active infection.
Choosing the right command on the exam
Match the symptom to the tool.
| Scenario | Best first check |
|---|---|
| Unknown app on an Android phone | `pm list packages -3` |
| Hot phone with fast battery loss | `dumpsys battery` |
| App keeps crashing | `logcat -d` |
| Trusted site opens a fake page | View the hosts file |
| PC talks to an unknown remote server | `netstat -ano`, then Task Manager |
| Windows files missing or altered | `sfc /scannow` |
Exercise
A user says her browser sends her to a fake login page whenever she types her bank's address, even though the address is correct. Other sites work normally. Which command would you run first to check for a local cause, and what would you look for?
Show answer
Display the hosts file with type %SystemRoot%\System32\drivers\etc\hosts. Look for an uncommented line (one that does not start with #) that maps the bank's domain name to an unfamiliar IP address.
Exercise
You run netstat -ano and see an ESTABLISHED connection to an unknown foreign address owned by PID 4812. What is your next step, and why should you not conclude the PC is infected yet?
Show answer
Find PID 4812 on the Details tab of Task Manager to identify the process, then check its name, file location and signer. The connection alone is not proof, because many legitimate programs open network connections.