From Newsgroup: alt.comp.os.windows-10
Maria Sophia wrote:
Thanks. I'll incorporate it into the script to scan for all
APKs on the system, list their contents, and look for Ad SDKs.
Note that can be used to look for anything but the original
goal is to find all the ad sdks on our system that "can" trigger.
This is getting closer... but it's not quite there yet... as I had
to make it a subroutine but I'm still getting errors...
It will eventually work.
Here's the current status... It doesn't throw syntax errors.
But it ends with
... billions of lines...
The system cannot find the drive specified.
The system cannot find the drive specified.
Scanning /system/priv-app/SecSoundPicker/SecSoundPicker.apk
The system cannot find the drive specified.
The system cannot find the drive specified.
Scanning /data/app/~~tzuj3WurSpKFVmnllqWMWw==/com.pixlr.removebg-e9DUGN4gtU8pMabH2172hQ==/base.apk
The system cannot find the drive specified.
The system cannot find the drive specified.
Scan complete.
Summary:
Total APKs scanned: 986
APKs containing ad-related files: 3
APKs with no ad-related files: 983
Detailed list saved to: scanads\scanads_5adsfound.txt
ECHO is off.
:: ------------------------------------------------------------------
:: scanads.bat
:: Scans non-rooted Android 10+ to look inside all apps for ad SDKs
:: 1. connects to Android via ADB on the Windows desktop
:: 2. lists installed apps on the Android device
:: 3. extracts APK paths for each Android app on the device
:: (this is the step I needed help on from the Usenet newsgroups)
:: 4. runs unzip -l on each APK to list all the files inside each app
:: 5. searches for known ad SDK classpaths
:: 6. counts apps containing the known specific ad SDKs
:: 7. prints a summary of APKs found that have specific ad-related SDKs
:: ------------------------------------------------------------------
:: Debug: scanads.bat > full_console_output.txt 2>&1
:: cmd /v /c "echo on & call scanads.bat > full_trace.txt 2>&1"
:: ------------------------------------------------------------------
:: v2p3 20260806 Had to move the trimming logic OUTSIDE the parentheses
:: because: for (...) do (...) blocks cannot contain :labels
:: becasue: goto cannot jump into or out of a parenthesized block
:: therefore: the trimming loop must be a subroutine
:: v2p2 20260806 Replaced extract APK powershell with Stefan's suggestion
:: v2p1 20260806 debugging galore (with help from Stefan Ram on Usenet)
:: v2p0 20260806 Created scanads_patterns.txt file containing SDKs
:: Because these strings only appear in apps that actually embed ad SDKs.
:: com/facebook/ads/AdManager
:: com/mopub/mobileads
:: com/applovin/mediation
:: com/unity3d/ads/android
:: com/ironsource/mediationsdk
:: com/bytedance/sdk/openads
:: com/chartboost/sdk
:: com/inmobi/ads
:: com/vungle/publisher
:: com/mintegral/msdk
:: com/tapjoy/TJAdUnit
:: com/adcolony/sdk
:: com/amazon/device/ads/AdRegistration
:: v1p9 20260806 added "setlocal enabledelayedexpansion"
:: v1p8 20260805 List APKs with ad SDKs in the summary and into a file
:: v1p7 20260805 Cleaned up minor errors and strengthened the summary
:: v1p6 20260805 Changed to match SDK classpaths, not metadata filenames
:: v1p5 20260805 Tightened grep from "ads" to a list of known ad packages
:: v1p4 20260805 Added result summary for packages found with ad SDKs
:: v1p3 20260805 Set temporary file names via easily-modified variables
:: v1p2 20260805 Added separate output folder and consistent file names
:: v1p1 20260805 Added checks to make sure adb is working first
:: v1p0 20260805 Scans all Android apps for ad-related packages
:: ------------------------------------------------------------------
:: This program connects to an Android device over ADB, collects the
:: full list of installed apps, extracts the file paths of their APKs,
:: and then scans each APK for known ad-network SDKs. For every APK,
:: it uses unzip -l on the device to list all files inside the package,
:: then it searches that file list for specific SDK classpaths. If any
:: of those classpaths appear, the app is counted as containing an ad SDK.
:: Otherwise, the app is counted as clean. At the end, the script prints
:: a summary showing how many APKs were scanned, how many contained ad SDKs,
:: and how many did not. Here is my first output on my daily drive:
:: Total APKs scanned: 986
:: APKs containing ad-related files: 3
:: APKs with no ad-related files: 983
:: ------------------------------------------------------------------
@echo off
setlocal enabledelayedexpansion
echo Android APK Ad Network Scanner
echo.
REM Working directory and filenames and counters
:: Define the directory name for temp working files
set WORKDIR=scanads
:: Define the file name for the list of package names and APK paths
set RAWPKG=%WORKDIR%\scanads_1packages_raw.txt
:: Define the file name for the list of APKs scanned
set APKPATHS=%WORKDIR%\scanads_2apkpaths.txt
:: Define the file name for the APK unzip showing every file in the APK
set APKCONTENTS=%WORKDIR%\scanads_3apkcontents.tmp
:: Define the ephemeral file name for ad-SDK classpath matches
set MATCHES=%WORKDIR%\scanads_4matches.tmp
:: Define the file where we save all APKs that contain ad SDKs
set ADSOUT=%WORKDIR%\scanads_5adsfound.txt
:: Define a pattern file that contains the desired search keywords
set PATTERNS=scanads_patterns.txt
:: Start counters at zero
set COUNT_TOTAL=0
set COUNT_ADS=0
set COUNT_NOADS=0
REM Create working directory if missing
if not exist %WORKDIR% mkdir %WORKDIR%
REM Check ADB connection
adb get-state > nul 2>&1
if errorlevel 1 (
echo No device detected. Connect Android and enable USB debugging.
exit /b
)
REM Check unzip availability
adb shell which unzip > nul 2>&1
if errorlevel 1 (
echo Device does not have unzip. Install BusyBox or enable unzip.
exit /b
)
REM Dump package list
echo Pulling package list...
adb shell pm list packages -f > %RAWPKG%
echo Package list saved.
echo.
REM Extract APK paths (v2p3 pure batch version)
echo Extracting APK paths...
> "%APKPATHS%" (
for /f "usebackq delims=" %%L in ("%RAWPKG%") do (
call :ExtractPath "%%L"
)
)
echo APK paths saved.
echo.
echo APK paths saved.
echo.
REM Scan APKs
echo Scanning for ad networks...
echo.
:: v1p8: clear output file for ad-SDK results
echo Ad SDKs found in these APKs: > "%ADSOUT%"
:: begin scan loop
for /f "usebackq delims=" %%A in ("%APKPATHS%") do (
set /a COUNT_TOTAL+=1
echo Scanning %%A
:: v1p8: clear APKCONTENTS before unzip to avoid stale data
echo. > "%APKCONTENTS%"
adb shell unzip -l "%%A" > "%APKCONTENTS%" 2>nul
:: v1p8: clear MATCHES to avoid stale data
echo. > "%MATCHES%"
:: Find ad SDKs inside of the list of files in every APK
:: findstr /i "com/facebook/ads/AdManager com/mopub/mobileads com/applovin/mediation com/unity3d/ads/android com/ironsource/mediationsdk com/bytedance/sdk/openads com/chartboost/sdk com/inmobi/ads com/vungle/publisher com/mintegral/msdk com/tapjoy/TJAdUnit com/adcolony/sdk com/amazon/device/ads/AdRegistration" %APKCONTENTS% > %MATCHES%
findstr /i /g:"%PATTERNS%" "%APKCONTENTS%" > "%MATCHES%"
:: errorlevel=0 means matches found, therefore it has ads
:: errorlevel=1 means no matches, therefore it does not have ads
if errorlevel 1 (
set /a COUNT_NOADS+=1
del %MATCHES% 2>nul
) else (
echo [ADS] %%A
for /f %%X in (%MATCHES%) do echo %%X
:: v1p8: append results to ADSOUT file
echo APK: %%A >> "%ADSOUT%"
for /f %%X in (%MATCHES%) do echo %%X >> "%ADSOUT%"
echo. >> "%ADSOUT%"
set /a COUNT_ADS+=1
:: Save it for debugging
copy "%MATCHES%" "scanads\matches_%%~nA.tmp" >nul
del %MATCHES% 2>nul
)
)
:: end scan loop
echo.
echo Scan complete.
echo.
echo Summary:
echo Total APKs scanned: %COUNT_TOTAL%
echo APKs containing ad-related files: %COUNT_ADS%
echo APKs with no ad-related files: %COUNT_NOADS%
echo.
:: v1p8 show the file containing detailed ad-SDK results
echo Detailed list saved to: %ADSOUT%
echo.
REM Subroutine: ExtractPath
REM Input: %1 = raw line from pm list packages -f
REM Output: trimmed APK path (echoed)
:ExtractPath
setlocal enabledelayedexpansion
set "line=%~1"
REM Strip leading "package:"
if /i "!line:~0,8!"=="package:" (
set "line=!line:~8!"
)
REM Trim everything after the last "="
set "tmp=!line!"
:trimloop
if "!tmp:~-1!"=="=" (
set "tmp=!tmp:~0,-1!"
goto :trimdone
)
set "tmp=!tmp:~0,-1!"
if not "!tmp!"=="" goto :trimloop
:trimdone
echo !tmp!
endlocal
goto :eof
gvim %ADSOUT%
pause
:: end of scanads.bat
--- Synchronet 3.22a-Linux NewsLink 1.2