1
0

pcdu.bat 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. @echo off
  2. setlocal EnableExtensions EnableDelayedExpansion
  3. REM =====================================================
  4. REM ____ _ ____ ____ _ _
  5. REM | _ \ __ _ _ ____ _( )___ / ___| _ \| | | |
  6. REM | |_) / _` | '__\ \ / /|// __|| | | | | | | | |
  7. REM | __/ (_| | | \ V / \__ \| |___| |_| | |_| |
  8. REM |_| \__,_|_| \_/ |___(_)____|____/ \___/
  9. REM
  10. REM Parv's Case Download Utility (PCDU) v1.2
  11. REM Downloads case files from analysis server
  12. REM =====================================================
  13. set "PCDU_VERSION=1.2"
  14. set "PCDU_NAME=pcdu"
  15. set "PCDU_INSTALL_DIR=%USERPROFILE%\PCDU"
  16. set "SCRIPT_PATH=%~f0"
  17. set "SCRIPT_NAME=%~nx0"
  18. REM =====================================================
  19. REM Command Line Argument Handling
  20. REM =====================================================
  21. if /i "%~1"=="/install" goto :INSTALL_TO_PATH
  22. if /i "%~1"=="/uninstall" goto :UNINSTALL_FROM_PATH
  23. if /i "%~1"=="/help" goto :SHOW_HELP
  24. if /i "%~1"=="-h" goto :SHOW_HELP
  25. if /i "%~1"=="--help" goto :SHOW_HELP
  26. if /i "%~1"=="/?" goto :SHOW_HELP
  27. REM =====================================================
  28. REM Check if first run (not installed to PATH)
  29. REM =====================================================
  30. call :CHECK_IF_INSTALLED
  31. if !PCDU_INSTALLED! EQU 0 (
  32. call :PROMPT_INSTALL
  33. )
  34. goto :MAIN_SCRIPT
  35. REM =====================================================
  36. REM INSTALL TO PATH FUNCTION
  37. REM =====================================================
  38. :INSTALL_TO_PATH
  39. echo.
  40. echo ============================================================
  41. echo ^|^> PCDU Installation
  42. echo ============================================================
  43. echo.
  44. REM Check if already installed
  45. call :CHECK_IF_INSTALLED
  46. if !PCDU_INSTALLED! EQU 1 (
  47. echo [OK] PCDU is already installed and accessible from PATH.
  48. echo Location: !PCDU_FOUND_PATH!
  49. echo.
  50. choice /C YN /M "Reinstall/Update PCDU?"
  51. if !ERRORLEVEL! EQU 2 (
  52. echo.
  53. echo Installation cancelled.
  54. goto :EOF
  55. )
  56. )
  57. REM Create install directory
  58. echo [1/4] Creating installation directory...
  59. if not exist "%PCDU_INSTALL_DIR%" (
  60. mkdir "%PCDU_INSTALL_DIR%" 2>nul
  61. if !ERRORLEVEL! NEQ 0 (
  62. echo [ERROR] Failed to create directory: %PCDU_INSTALL_DIR%
  63. goto :EOF
  64. )
  65. echo Created: %PCDU_INSTALL_DIR%
  66. ) else (
  67. echo Exists: %PCDU_INSTALL_DIR%
  68. )
  69. REM Copy script to install directory
  70. echo.
  71. echo [2/4] Copying PCDU script...
  72. copy /Y "%SCRIPT_PATH%" "%PCDU_INSTALL_DIR%\%PCDU_NAME%.bat" >nul 2>&1
  73. if !ERRORLEVEL! NEQ 0 (
  74. echo [ERROR] Failed to copy script to installation directory.
  75. goto :EOF
  76. )
  77. echo Copied to: %PCDU_INSTALL_DIR%\%PCDU_NAME%.bat
  78. REM Create a simple launcher without .bat extension (optional convenience)
  79. echo [2b/4] Creating launcher...
  80. echo @echo off > "%PCDU_INSTALL_DIR%\%PCDU_NAME%.cmd"
  81. echo call "%PCDU_INSTALL_DIR%\%PCDU_NAME%.bat" %%* >> "%PCDU_INSTALL_DIR%\%PCDU_NAME%.cmd"
  82. echo Created: %PCDU_INSTALL_DIR%\%PCDU_NAME%.cmd
  83. REM Check if directory is already in PATH
  84. echo.
  85. echo [3/4] Checking PATH environment variable...
  86. set "PATH_CONTAINS_PCDU=0"
  87. echo !PATH! | findstr /I /C:"%PCDU_INSTALL_DIR%" >nul 2>&1
  88. if !ERRORLEVEL! EQU 0 (
  89. set "PATH_CONTAINS_PCDU=1"
  90. echo PCDU directory already in PATH.
  91. )
  92. REM Add to PATH if not present
  93. if !PATH_CONTAINS_PCDU! EQU 0 (
  94. echo Adding PCDU to User PATH...
  95. REM Get current user PATH
  96. for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "USER_PATH=%%B"
  97. REM Check if USER_PATH is empty or not set
  98. if "!USER_PATH!"=="" (
  99. set "NEW_PATH=%PCDU_INSTALL_DIR%"
  100. ) else (
  101. set "NEW_PATH=!USER_PATH!;%PCDU_INSTALL_DIR%"
  102. )
  103. REM Use setx to permanently add to user PATH
  104. setx PATH "!NEW_PATH!" >nul 2>&1
  105. if !ERRORLEVEL! EQU 0 (
  106. echo [OK] Added to User PATH successfully.
  107. ) else (
  108. echo [WARNING] Could not add to PATH automatically.
  109. echo Please manually add this to your PATH:
  110. echo %PCDU_INSTALL_DIR%
  111. )
  112. )
  113. REM Verify installation
  114. echo.
  115. echo [4/4] Verifying installation...
  116. REM Update current session PATH
  117. set "PATH=%PATH%;%PCDU_INSTALL_DIR%"
  118. if exist "%PCDU_INSTALL_DIR%\%PCDU_NAME%.bat" (
  119. echo [OK] Script installed successfully.
  120. ) else (
  121. echo [ERROR] Script not found at expected location.
  122. goto :EOF
  123. )
  124. echo.
  125. echo ============================================================
  126. echo ^|^> Installation Complete!
  127. echo ============================================================
  128. echo.
  129. echo PCDU has been installed to: %PCDU_INSTALL_DIR%
  130. echo.
  131. echo You can now run PCDU from anywhere using:
  132. echo.
  133. echo pcdu - Run interactively
  134. echo pcdu [CaseNumber] - Download specific case
  135. echo pcdu [CaseNo] [User] - With username
  136. echo pcdu [CaseNo] [User] [Pw] - Fully automated
  137. echo.
  138. echo pcdu /help - Show help
  139. echo pcdu /install - Reinstall PCDU
  140. echo pcdu /uninstall - Remove PCDU from system
  141. echo.
  142. echo NOTE: You may need to restart your terminal/CMD for
  143. echo PATH changes to take effect.
  144. echo.
  145. echo ============================================================
  146. echo.
  147. REM Refresh environment for current process
  148. call :REFRESH_ENV
  149. pause
  150. goto :EOF
  151. REM =====================================================
  152. REM UNINSTALL FROM PATH FUNCTION
  153. REM =====================================================
  154. :UNINSTALL_FROM_PATH
  155. echo.
  156. echo ============================================================
  157. echo ^|^> PCDU Uninstallation
  158. echo ============================================================
  159. echo.
  160. choice /C YN /M "Are you sure you want to uninstall PCDU?"
  161. if !ERRORLEVEL! EQU 2 (
  162. echo.
  163. echo Uninstallation cancelled.
  164. goto :EOF
  165. )
  166. echo.
  167. echo [1/3] Removing PCDU from PATH...
  168. REM Get current user PATH
  169. for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "USER_PATH=%%B"
  170. REM Remove PCDU_INSTALL_DIR from PATH
  171. set "NEW_PATH=!USER_PATH:%PCDU_INSTALL_DIR%;=!"
  172. set "NEW_PATH=!NEW_PATH:;%PCDU_INSTALL_DIR%=!"
  173. set "NEW_PATH=!NEW_PATH:%PCDU_INSTALL_DIR%=!"
  174. REM Update PATH
  175. if not "!NEW_PATH!"=="!USER_PATH!" (
  176. setx PATH "!NEW_PATH!" >nul 2>&1
  177. echo [OK] Removed from PATH.
  178. ) else (
  179. echo [--] PCDU was not in PATH.
  180. )
  181. echo.
  182. echo [2/3] Removing installation directory...
  183. if exist "%PCDU_INSTALL_DIR%" (
  184. rmdir /S /Q "%PCDU_INSTALL_DIR%" 2>nul
  185. if !ERRORLEVEL! EQU 0 (
  186. echo [OK] Removed: %PCDU_INSTALL_DIR%
  187. ) else (
  188. echo [WARNING] Could not remove directory. Please delete manually:
  189. echo %PCDU_INSTALL_DIR%
  190. )
  191. ) else (
  192. echo [--] Installation directory not found.
  193. )
  194. echo.
  195. echo [3/3] Cleanup complete.
  196. echo.
  197. echo ============================================================
  198. echo ^|^> PCDU has been uninstalled.
  199. echo ============================================================
  200. echo.
  201. echo NOTE: Restart your terminal for PATH changes to take effect.
  202. echo.
  203. pause
  204. goto :EOF
  205. REM =====================================================
  206. REM SHOW HELP FUNCTION
  207. REM =====================================================
  208. :SHOW_HELP
  209. echo.
  210. echo ============================================================
  211. echo ^|^> Parv's Case Download Utility ^(PCDU^) v%PCDU_VERSION%
  212. echo ============================================================
  213. echo.
  214. echo DESCRIPTION:
  215. echo Downloads case files from the analysis server including
  216. echo configurations, packet captures, and media files.
  217. echo.
  218. echo USAGE:
  219. echo %PCDU_NAME% [options]
  220. echo %PCDU_NAME% [CaseNumber] [Username] [Password]
  221. echo.
  222. echo OPTIONS:
  223. echo /install Install PCDU to system PATH
  224. echo /uninstall Remove PCDU from system
  225. echo /help, -h Show this help message
  226. echo.
  227. echo EXAMPLES:
  228. echo %PCDU_NAME% Interactive mode
  229. echo %PCDU_NAME% 12345678 Download case 12345678
  230. echo %PCDU_NAME% 12345678 myuser With username
  231. echo %PCDU_NAME% /install Install to PATH
  232. echo.
  233. echo STORAGE LOCATIONS (in priority order):
  234. echo 1. D:\SJLNT\[CaseNumber]
  235. echo 2. [Temporary Storage Drive]:\SJLNT\[CaseNumber]
  236. echo 3. C:\Temp\SJLNT\[CaseNumber] (fallback)
  237. echo.
  238. echo DOWNLOADED FILES:
  239. echo - Saved configs (ns.conf) from collector bundles
  240. echo - Running configs (ns_running_config.conf)
  241. echo - Packet captures (.pcap, .cap, .har, etc.)
  242. echo - SSL keys (.sslkeys, .keys)
  243. echo - Media files (images)
  244. echo - Log and text files
  245. echo.
  246. echo REQUIREMENTS:
  247. echo - PuTTY (pscp.exe, plink.exe) OR OpenSSH (scp.exe)
  248. echo - Network access to sjanalysis.citrite.net
  249. echo.
  250. echo ============================================================
  251. echo.
  252. pause
  253. goto :EOF
  254. REM =====================================================
  255. REM CHECK IF INSTALLED FUNCTION
  256. REM =====================================================
  257. :CHECK_IF_INSTALLED
  258. set "PCDU_INSTALLED=0"
  259. set "PCDU_FOUND_PATH="
  260. REM Check if pcdu command is available
  261. where pcdu.bat >nul 2>&1
  262. if !ERRORLEVEL! EQU 0 (
  263. for /f "delims=" %%P in ('where pcdu.bat 2^>nul') do (
  264. set "PCDU_FOUND_PATH=%%P"
  265. )
  266. set "PCDU_INSTALLED=1"
  267. goto :EOF
  268. )
  269. where pcdu.cmd >nul 2>&1
  270. if !ERRORLEVEL! EQU 0 (
  271. for /f "delims=" %%P in ('where pcdu.cmd 2^>nul') do (
  272. set "PCDU_FOUND_PATH=%%P"
  273. )
  274. set "PCDU_INSTALLED=1"
  275. goto :EOF
  276. )
  277. REM Check if running from install directory
  278. if /i "%~dp0"=="%PCDU_INSTALL_DIR%\" (
  279. set "PCDU_INSTALLED=1"
  280. set "PCDU_FOUND_PATH=%PCDU_INSTALL_DIR%\%PCDU_NAME%.bat"
  281. )
  282. goto :EOF
  283. REM =====================================================
  284. REM PROMPT INSTALL FUNCTION
  285. REM =====================================================
  286. :PROMPT_INSTALL
  287. echo.
  288. echo ============================================================
  289. echo ^|^> PCDU First Run Setup
  290. echo ============================================================
  291. echo.
  292. echo PCDU is not installed to your system PATH.
  293. echo Installing allows you to run 'pcdu' from any directory.
  294. echo.
  295. echo Installation will:
  296. echo - Create folder: %PCDU_INSTALL_DIR%
  297. echo - Copy PCDU script to that folder
  298. echo - Add folder to your User PATH
  299. echo.
  300. choice /C YNC /M "Install PCDU to PATH? (Y=Yes, N=No, C=Cancel script)"
  301. if !ERRORLEVEL! EQU 3 (
  302. echo.
  303. echo Script cancelled.
  304. exit /b 0
  305. )
  306. if !ERRORLEVEL! EQU 1 (
  307. call :INSTALL_TO_PATH
  308. echo.
  309. echo Continuing with case download...
  310. echo.
  311. )
  312. goto :EOF
  313. REM =====================================================
  314. REM REFRESH ENVIRONMENT FUNCTION
  315. REM =====================================================
  316. :REFRESH_ENV
  317. REM This refreshes the PATH for the current cmd session
  318. for /f "tokens=2*" %%A in ('reg query "HKCU\Environment" /v PATH 2^>nul') do set "PATH=%%B;%PATH%"
  319. goto :EOF
  320. REM =====================================================
  321. REM MAIN SCRIPT
  322. REM =====================================================
  323. :MAIN_SCRIPT
  324. REM =====================================================
  325. REM Storage Drive Configuration
  326. REM Priority: D: -> "Temporary Storage" -> C:\Temp
  327. REM =====================================================
  328. set "STORAGE_DRIVE="
  329. set "STORAGE_BASE="
  330. set "DRIVE_STATUS="
  331. echo.
  332. echo [STORAGE CHECK] Checking available storage drives...
  333. echo.
  334. REM Check for D: drive first
  335. if exist "D:\" (
  336. set "STORAGE_DRIVE=D:"
  337. set "STORAGE_BASE=D:\SJLNT"
  338. set "DRIVE_STATUS=D: Drive found and accessible"
  339. echo [OK] D: Drive found and accessible
  340. goto :DRIVE_FOUND
  341. )
  342. echo [--] D: Drive not found, checking for 'Temporary Storage' drive...
  343. REM Check for drive with label "Temporary Storage"
  344. for /f "tokens=1,2*" %%A in ('wmic logicaldisk get caption^,volumename 2^>nul ^| findstr /I "Temporary Storage"') do (
  345. set "TEMP_DRIVE=%%A"
  346. if exist "!TEMP_DRIVE!\" (
  347. set "STORAGE_DRIVE=!TEMP_DRIVE!"
  348. set "STORAGE_BASE=!TEMP_DRIVE!\SJLNT"
  349. set "DRIVE_STATUS=Temporary Storage drive found at !TEMP_DRIVE!"
  350. echo [OK] 'Temporary Storage' drive found at !TEMP_DRIVE!
  351. goto :DRIVE_FOUND
  352. )
  353. )
  354. REM Alternative method: Check all drives for "Temporary Storage" label
  355. for %%D in (E F G H I J K L M N O P Q R S T U V W X Y Z) do (
  356. if exist "%%D:\" (
  357. for /f "tokens=*" %%L in ('vol %%D: 2^>nul ^| findstr /I "Temporary Storage"') do (
  358. set "STORAGE_DRIVE=%%D:"
  359. set "STORAGE_BASE=%%D:\SJLNT"
  360. set "DRIVE_STATUS=Temporary Storage drive found at %%D:"
  361. echo [OK] 'Temporary Storage' drive found at %%D:
  362. goto :DRIVE_FOUND
  363. )
  364. )
  365. )
  366. echo [--] 'Temporary Storage' drive not found
  367. echo.
  368. REM Fallback to C:\Temp if no preferred drive found
  369. echo [!!] WARNING: Neither D: nor 'Temporary Storage' drive found!
  370. echo.
  371. echo Available options:
  372. echo 1. Use C:\Temp\SJLNT (fallback location)
  373. echo 2. Cancel and check your drives
  374. echo.
  375. choice /C 12 /M "Select option"
  376. if !ERRORLEVEL! EQU 2 (
  377. echo.
  378. echo Operation cancelled by user.
  379. echo Please ensure D: drive or a drive labeled 'Temporary Storage' is available.
  380. pause
  381. exit /b 1
  382. )
  383. set "STORAGE_DRIVE=C:"
  384. set "STORAGE_BASE=C:\Temp\SJLNT"
  385. set "DRIVE_STATUS=Using fallback location C:\Temp\SJLNT"
  386. echo.
  387. echo [!!] Using fallback: C:\Temp\SJLNT
  388. :DRIVE_FOUND
  389. REM Check for pscp.exe (PuTTY SCP) or scp.exe
  390. set "SCP_TOOL="
  391. where pscp.exe >nul 2>&1
  392. if !ERRORLEVEL! EQU 0 (
  393. set "SCP_TOOL=pscp.exe"
  394. set "SCP_TYPE=PSCP"
  395. ) else (
  396. where scp.exe >nul 2>&1
  397. if !ERRORLEVEL! EQU 0 (
  398. set "SCP_TOOL=scp.exe"
  399. set "SCP_TYPE=SCP"
  400. )
  401. )
  402. if "%SCP_TOOL%"=="" (
  403. echo.
  404. echo ============================================================
  405. echo [ERROR] SCP Tool Not Found
  406. echo ============================================================
  407. echo.
  408. echo Neither pscp.exe nor scp.exe found in PATH.
  409. echo.
  410. echo Please install one of the following:
  411. echo - PuTTY ^(includes pscp.exe^) - https://www.putty.org/
  412. echo - OpenSSH Client ^(includes scp.exe^) - Built into Windows 10/11
  413. echo.
  414. echo To enable OpenSSH on Windows:
  415. echo Settings ^> Apps ^> Optional Features ^> Add OpenSSH Client
  416. echo.
  417. pause
  418. exit /b 1
  419. )
  420. REM =====================================================
  421. REM Configuration
  422. REM =====================================================
  423. set "REMOTE_SERVER=sjanalysis.citrite.net"
  424. set "REMOTE_BASE_PATH=/upload/ftp"
  425. REM Prompt for case number if not provided
  426. if "%~1"=="" (
  427. set /p CASENO=Enter Case Number:
  428. ) else (
  429. REM Skip if argument is an option
  430. echo %~1 | findstr /B /C:"/" /C:"-" >nul
  431. if !ERRORLEVEL! EQU 0 (
  432. set /p CASENO=Enter Case Number:
  433. ) else (
  434. set "CASENO=%~1"
  435. )
  436. )
  437. if "%CASENO%"=="" (
  438. echo ERROR: Case number cannot be empty.
  439. exit /b 1
  440. )
  441. REM Prompt for credentials if not provided
  442. if "%~2"=="" (
  443. set /p USERNAME=Enter SSH Username:
  444. ) else (
  445. set "USERNAME=%~2"
  446. )
  447. if "%USERNAME%"=="" (
  448. echo ERROR: Username cannot be empty.
  449. exit /b 1
  450. )
  451. if "%~3"=="" (
  452. echo Enter SSH Password for %USERNAME%@%REMOTE_SERVER%:
  453. REM Use PowerShell for hidden password input
  454. for /f "delims=" %%P in ('powershell -Command "$p = Read-Host -AsSecureString; [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($p))"') do set "PASSWORD=%%P"
  455. ) else (
  456. set "PASSWORD=%~3"
  457. )
  458. if "%PASSWORD%"=="" (
  459. echo ERROR: Password cannot be empty.
  460. exit /b 1
  461. )
  462. REM =====================================================
  463. REM Paths - Now using configured storage drive
  464. REM =====================================================
  465. set "REMOTE_CASE_PATH=%REMOTE_BASE_PATH%/%CASENO%"
  466. set "DEST=%STORAGE_BASE%\%CASENO%"
  467. set "LOGFILE=%DEST%\download_%CASENO%.log"
  468. set "TEMP_LIST=%TEMP%\caselist_%CASENO%.txt"
  469. set "TEMP_FILTERED=%TEMP%\filtered_%CASENO%.txt"
  470. set "COLLECTOR_LIST=%TEMP%\collectors_%CASENO%.txt"
  471. set "ALL_FILES_LIST=%TEMP%\allfiles_%CASENO%.txt"
  472. echo.
  473. echo ============================================================
  474. echo ^|^> Parv's Case Download Utility ^(PCDU^) v%PCDU_VERSION%
  475. echo ============================================================
  476. echo Case Number : %CASENO%
  477. echo Remote Server : %REMOTE_SERVER%
  478. echo Remote Path : %REMOTE_CASE_PATH%
  479. echo Storage Drive : %STORAGE_DRIVE%
  480. echo Local Dest : %DEST%
  481. echo SCP Tool : %SCP_TYPE%
  482. echo User : %USERNAME%
  483. echo Drive Status : %DRIVE_STATUS%
  484. echo ============================================================
  485. echo.
  486. REM =====================================================
  487. REM Create destination directory structure
  488. REM =====================================================
  489. echo [PATH CHECK] Verifying destination paths...
  490. echo.
  491. REM Check if base storage path exists
  492. if exist "%STORAGE_BASE%" (
  493. echo [OK] Base path exists: %STORAGE_BASE%
  494. ) else (
  495. echo [!!] Base path does not exist: %STORAGE_BASE%
  496. echo Creating base directory...
  497. mkdir "%STORAGE_BASE%" 2>nul
  498. if !ERRORLEVEL! EQU 0 (
  499. echo [OK] Created: %STORAGE_BASE%
  500. ) else (
  501. echo [ERROR] Failed to create base directory: %STORAGE_BASE%
  502. echo.
  503. echo Possible reasons:
  504. echo - Drive not writable
  505. echo - Insufficient permissions
  506. echo - Drive full
  507. echo.
  508. pause
  509. exit /b 1
  510. )
  511. )
  512. REM Check if case destination exists
  513. if exist "%DEST%" (
  514. echo [OK] Case path exists: %DEST%
  515. echo.
  516. echo [!!] WARNING: Case folder already exists!
  517. echo Files may be overwritten.
  518. echo.
  519. choice /C YN /M "Continue and potentially overwrite existing files?"
  520. if !ERRORLEVEL! EQU 2 (
  521. echo.
  522. echo Operation cancelled by user.
  523. pause
  524. exit /b 0
  525. )
  526. ) else (
  527. echo [!!] Case path does not exist: %DEST%
  528. echo Creating case directory...
  529. mkdir "%DEST%" 2>nul
  530. if !ERRORLEVEL! EQU 0 (
  531. echo [OK] Created: %DEST%
  532. ) else (
  533. echo [ERROR] Failed to create case directory: %DEST%
  534. pause
  535. exit /b 1
  536. )
  537. )
  538. REM Create subdirectories for organization
  539. echo.
  540. echo [PATH CHECK] Creating subdirectories...
  541. for %%S in (configs captures media) do (
  542. if exist "%DEST%\%%S" (
  543. echo [OK] Exists: %DEST%\%%S
  544. ) else (
  545. mkdir "%DEST%\%%S" 2>nul
  546. if !ERRORLEVEL! EQU 0 (
  547. echo [OK] Created: %DEST%\%%S
  548. ) else (
  549. echo [!!] Failed to create: %DEST%\%%S
  550. )
  551. )
  552. )
  553. echo.
  554. REM Initialize log file
  555. echo ============================================================ > "%LOGFILE%"
  556. echo Parv's Case Download Utility ^(PCDU^) v%PCDU_VERSION% - Download Log >> "%LOGFILE%"
  557. echo ============================================================ >> "%LOGFILE%"
  558. echo Case Number : %CASENO% >> "%LOGFILE%"
  559. echo Date/Time : %DATE% %TIME% >> "%LOGFILE%"
  560. echo Remote Server : %REMOTE_SERVER% >> "%LOGFILE%"
  561. echo User : %USERNAME% >> "%LOGFILE%"
  562. echo Storage Drive : %STORAGE_DRIVE% >> "%LOGFILE%"
  563. echo Destination : %DEST% >> "%LOGFILE%"
  564. echo Drive Status : %DRIVE_STATUS% >> "%LOGFILE%"
  565. echo ============================================================ >> "%LOGFILE%"
  566. echo. >> "%LOGFILE%"
  567. REM =====================================================
  568. REM Test SSH connection and verify case directory
  569. REM =====================================================
  570. echo [1/5] Testing SSH connection...
  571. echo [1/5] Testing SSH connection... >> "%LOGFILE%"
  572. if "%SCP_TYPE%"=="PSCP" (
  573. echo y | plink.exe -batch -pw "%PASSWORD%" %USERNAME%@%REMOTE_SERVER% "test -d %REMOTE_CASE_PATH% && echo OK || echo NOTFOUND" 2>nul | findstr /V /C:"Keyboard" /C:"prompts from server" > "%TEMP_LIST%"
  574. ) else (
  575. sshpass -p "%PASSWORD%" ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=NUL %USERNAME%@%REMOTE_SERVER% "test -d %REMOTE_CASE_PATH% && echo OK || echo NOTFOUND" > "%TEMP_LIST%" 2>&1
  576. )
  577. if !ERRORLEVEL! NEQ 0 (
  578. echo ERROR: SSH connection failed. >> "%LOGFILE%"
  579. type "%TEMP_LIST%" >> "%LOGFILE%"
  580. echo ERROR: SSH connection failed.
  581. echo Check credentials and network connectivity.
  582. echo See log: %LOGFILE%
  583. del "%TEMP_LIST%" 2>nul
  584. pause
  585. exit /b 1
  586. )
  587. findstr /C:"OK" "%TEMP_LIST%" >nul
  588. if !ERRORLEVEL! NEQ 0 (
  589. echo ERROR: Case directory not found on server: %REMOTE_CASE_PATH% >> "%LOGFILE%"
  590. echo ERROR: Case directory not found on server.
  591. echo Path: %REMOTE_CASE_PATH%
  592. echo Please verify the case number.
  593. del "%TEMP_LIST%" 2>nul
  594. pause
  595. exit /b 1
  596. )
  597. echo Connection successful.
  598. echo Connection successful. >> "%LOGFILE%"
  599. REM =====================================================
  600. REM Single SSH call to get all required info
  601. REM Collectors + All files in case root
  602. REM =====================================================
  603. echo.
  604. echo [2/5] Scanning case directory...
  605. echo [2/5] Scanning case directory... >> "%LOGFILE%"
  606. if "%SCP_TYPE%"=="PSCP" (
  607. plink.exe -batch -pw "%PASSWORD%" %USERNAME%@%REMOTE_SERVER% "echo '===COLLECTORS==='; find %REMOTE_CASE_PATH% -maxdepth 1 -type d -name 'collector_*' 2>/dev/null; echo '===FILES==='; find %REMOTE_CASE_PATH% -maxdepth 1 -type f \( -name '*.cap' -o -name '*.pcap' -o -name '*.pcapng' -o -name '*.sslkeys' -o -name '*.keys' -o -name '*.har' -o -name '*.xml' -o -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' -o -name '*.gif' -o -name '*.bmp' -o -name '*.webp' -o -name '*.tiff' -o -name '*.svg' -o -name '*.txt' -o -name '*.log' -o -name '*.csv' -o -name '*.json' \) 2>/dev/null" 2>nul | findstr /V /C:"Keyboard" /C:"prompts from server" > "%ALL_FILES_LIST%"
  608. ) else (
  609. sshpass -p "%PASSWORD%" ssh -o StrictHostKeyChecking=no %USERNAME%@%REMOTE_SERVER% "echo '===COLLECTORS==='; find %REMOTE_CASE_PATH% -maxdepth 1 -type d -name 'collector_*' 2>/dev/null; echo '===FILES==='; find %REMOTE_CASE_PATH% -maxdepth 1 -type f \( -name '*.cap' -o -name '*.pcap' -o -name '*.pcapng' -o -name '*.sslkeys' -o -name '*.keys' -o -name '*.har' -o -name '*.xml' -o -name '*.jpg' -o -name '*.jpeg' -o -name '*.png' -o -name '*.gif' -o -name '*.bmp' -o -name '*.webp' -o -name '*.tiff' -o -name '*.svg' -o -name '*.txt' -o -name '*.log' -o -name '*.csv' -o -name '*.json' \) 2>/dev/null" > "%ALL_FILES_LIST%"
  610. )
  611. REM Parse collectors from combined output
  612. set "IN_COLLECTORS=0"
  613. set "IN_FILES=0"
  614. echo. > "%COLLECTOR_LIST%"
  615. echo. > "%TEMP_LIST%"
  616. for /f "usebackq delims=" %%A in ("%ALL_FILES_LIST%") do (
  617. set "LINE=%%A"
  618. REM Check for section markers
  619. echo !LINE! | findstr /C:"===COLLECTORS===" >nul
  620. if !ERRORLEVEL! EQU 0 (
  621. set "IN_COLLECTORS=1"
  622. set "IN_FILES=0"
  623. ) else (
  624. echo !LINE! | findstr /C:"===FILES===" >nul
  625. if !ERRORLEVEL! EQU 0 (
  626. set "IN_COLLECTORS=0"
  627. set "IN_FILES=1"
  628. ) else (
  629. REM Skip empty lines and filter noise
  630. if not "!LINE!"=="" (
  631. echo !LINE! | findstr /C:"Keyboard" /C:"prompts" /C:"authentication" >nul
  632. if !ERRORLEVEL! NEQ 0 (
  633. if !IN_COLLECTORS! EQU 1 (
  634. echo !LINE!>> "%COLLECTOR_LIST%"
  635. )
  636. if !IN_FILES! EQU 1 (
  637. echo !LINE!>> "%TEMP_LIST%"
  638. )
  639. )
  640. )
  641. )
  642. )
  643. )
  644. REM Count collectors
  645. set "COLLECTOR_COUNT=0"
  646. for /f "usebackq delims=" %%A in ("%COLLECTOR_LIST%") do (
  647. set "LINE=%%A"
  648. if not "!LINE!"=="" (
  649. echo !LINE! | findstr /C:"collector_" >nul
  650. if !ERRORLEVEL! EQU 0 (
  651. set /a COLLECTOR_COUNT+=1
  652. for %%B in ("!LINE!") do echo Found: %%~nxB
  653. echo Found collector: !LINE! >> "%LOGFILE%"
  654. )
  655. )
  656. )
  657. if %COLLECTOR_COUNT% EQU 0 (
  658. echo No collector bundles found.
  659. echo No collector bundles found. >> "%LOGFILE%"
  660. ) else (
  661. echo Total collectors found: %COLLECTOR_COUNT%
  662. )
  663. REM =====================================================
  664. REM Download Saved Config (ns.conf from nsconfig folder)
  665. REM Inside collector bundles only
  666. REM =====================================================
  667. echo.
  668. echo [3/5] Downloading saved configurations ^(ns.conf^)...
  669. echo [3/5] Downloading saved configurations... >> "%LOGFILE%"
  670. set "CONFIG_COUNT=0"
  671. for /f "usebackq delims=" %%C in ("%COLLECTOR_LIST%") do (
  672. set "COLLECTOR_PATH=%%C"
  673. REM Skip empty lines and filter noise
  674. if not "!COLLECTOR_PATH!"=="" (
  675. echo !COLLECTOR_PATH! | findstr /C:"collector_" >nul
  676. if !ERRORLEVEL! EQU 0 (
  677. for %%D in ("!COLLECTOR_PATH!") do set "COLLECTOR_NAME=%%~nxD"
  678. echo Checking !COLLECTOR_NAME!...
  679. set "DEST_FILENAME=ns.conf_!COLLECTOR_NAME!"
  680. if "%SCP_TYPE%"=="PSCP" (
  681. pscp.exe -batch -pw "%PASSWORD%" -q "%USERNAME%@%REMOTE_SERVER%:!COLLECTOR_PATH!/nsconfig/ns.conf" "%DEST%\configs\!DEST_FILENAME!" >nul 2>&1
  682. ) else (
  683. scp -o StrictHostKeyChecking=no -q "%USERNAME%@%REMOTE_SERVER%:!COLLECTOR_PATH!/nsconfig/ns.conf" "%DEST%\configs\!DEST_FILENAME!" 2>nul
  684. )
  685. if exist "%DEST%\configs\!DEST_FILENAME!" (
  686. echo [OK] !DEST_FILENAME!
  687. echo [OK] Downloaded: !DEST_FILENAME! >> "%LOGFILE%"
  688. set /a CONFIG_COUNT+=1
  689. ) else (
  690. echo [--] ns.conf not found
  691. )
  692. )
  693. )
  694. )
  695. echo Saved configs downloaded: %CONFIG_COUNT%
  696. REM =====================================================
  697. REM Download Running Config (ns_running_config.conf)
  698. REM Inside collector bundles only
  699. REM =====================================================
  700. echo.
  701. echo [4/5] Downloading running configurations...
  702. echo [4/5] Downloading running configurations... >> "%LOGFILE%"
  703. set "RUNNING_COUNT=0"
  704. for /f "usebackq delims=" %%C in ("%COLLECTOR_LIST%") do (
  705. set "COLLECTOR_PATH=%%C"
  706. REM Skip empty lines and filter noise
  707. if not "!COLLECTOR_PATH!"=="" (
  708. echo !COLLECTOR_PATH! | findstr /C:"collector_" >nul
  709. if !ERRORLEVEL! EQU 0 (
  710. for %%D in ("!COLLECTOR_PATH!") do set "COLLECTOR_NAME=%%~nxD"
  711. echo Checking !COLLECTOR_NAME!...
  712. set "DEST_FILENAME=ns_running_config.conf_!COLLECTOR_NAME!"
  713. if "%SCP_TYPE%"=="PSCP" (
  714. pscp.exe -batch -pw "%PASSWORD%" -q "%USERNAME%@%REMOTE_SERVER%:!COLLECTOR_PATH!/shell/ns_running_config.conf" "%DEST%\configs\!DEST_FILENAME!" >nul 2>&1
  715. ) else (
  716. scp -o StrictHostKeyChecking=no -q "%USERNAME%@%REMOTE_SERVER%:!COLLECTOR_PATH!/shell/ns_running_config.conf" "%DEST%\configs\!DEST_FILENAME!" 2>nul
  717. )
  718. if exist "%DEST%\configs\!DEST_FILENAME!" (
  719. echo [OK] !DEST_FILENAME!
  720. echo [OK] Downloaded: !DEST_FILENAME! >> "%LOGFILE%"
  721. set /a RUNNING_COUNT+=1
  722. ) else (
  723. echo [--] Running config not found
  724. )
  725. )
  726. )
  727. )
  728. echo Running configs downloaded: %RUNNING_COUNT%
  729. REM =====================================================
  730. REM Download Capture and Media Files
  731. REM OUTSIDE collector bundles only (case root level)
  732. REM Using pre-fetched file list
  733. REM =====================================================
  734. echo.
  735. echo [5/5] Downloading files from case directory...
  736. echo [5/5] Downloading files from case root... >> "%LOGFILE%"
  737. set "CAPTURE_COUNT=0"
  738. set "MEDIA_COUNT=0"
  739. set "OTHER_COUNT=0"
  740. for /f "usebackq delims=" %%F in ("%TEMP_LIST%") do (
  741. set "REMOTE_FILE=%%F"
  742. REM Skip empty lines and filter noise
  743. if not "!REMOTE_FILE!"=="" (
  744. echo !REMOTE_FILE! | findstr /C:"Keyboard" /C:"prompts" /C:"authentication" >nul
  745. if !ERRORLEVEL! NEQ 0 (
  746. for %%G in ("!REMOTE_FILE!") do (
  747. set "FILENAME=%%~nxG"
  748. set "FILEEXT=%%~xG"
  749. )
  750. if not "!FILENAME!"=="" (
  751. REM Determine file type and destination
  752. set "FILE_DEST="
  753. set "FILE_TYPE="
  754. REM Check if capture file
  755. echo !FILEEXT! | findstr /I /C:".cap" /C:".pcap" /C:".pcapng" /C:".sslkeys" /C:".keys" /C:".har" >nul
  756. if !ERRORLEVEL! EQU 0 (
  757. set "FILE_DEST=%DEST%\captures\!FILENAME!"
  758. set "FILE_TYPE=capture"
  759. )
  760. REM Check if image file
  761. echo !FILEEXT! | findstr /I /C:".jpg" /C:".jpeg" /C:".png" /C:".gif" /C:".bmp" /C:".webp" /C:".tiff" /C:".svg" >nul
  762. if !ERRORLEVEL! EQU 0 (
  763. set "FILE_DEST=%DEST%\media\!FILENAME!"
  764. set "FILE_TYPE=media"
  765. )
  766. REM Check if other file (txt, log, xml, json, csv)
  767. echo !FILEEXT! | findstr /I /C:".txt" /C:".log" /C:".xml" /C:".json" /C:".csv" >nul
  768. if !ERRORLEVEL! EQU 0 (
  769. set "FILE_DEST=%DEST%\!FILENAME!"
  770. set "FILE_TYPE=other"
  771. )
  772. REM Download the file
  773. if defined FILE_DEST (
  774. echo Downloading: !FILENAME!
  775. if "%SCP_TYPE%"=="PSCP" (
  776. pscp.exe -batch -pw "%PASSWORD%" -q "%USERNAME%@%REMOTE_SERVER%:!REMOTE_FILE!" "!FILE_DEST!" >nul 2>&1
  777. ) else (
  778. scp -o StrictHostKeyChecking=no -q "%USERNAME%@%REMOTE_SERVER%:!REMOTE_FILE!" "!FILE_DEST!" 2>nul
  779. )
  780. if exist "!FILE_DEST!" (
  781. echo [OK] !FILENAME!
  782. echo [OK] Downloaded !FILE_TYPE!: !FILENAME! >> "%LOGFILE%"
  783. if "!FILE_TYPE!"=="capture" set /a CAPTURE_COUNT+=1
  784. if "!FILE_TYPE!"=="media" set /a MEDIA_COUNT+=1
  785. if "!FILE_TYPE!"=="other" set /a OTHER_COUNT+=1
  786. )
  787. )
  788. )
  789. )
  790. )
  791. )
  792. echo.
  793. echo Capture files downloaded: %CAPTURE_COUNT%
  794. echo Media files downloaded : %MEDIA_COUNT%
  795. echo Other files downloaded : %OTHER_COUNT%
  796. REM =====================================================
  797. REM Cleanup and Summary
  798. REM =====================================================
  799. del "%TEMP_LIST%" 2>nul
  800. del "%TEMP_FILTERED%" 2>nul
  801. del "%COLLECTOR_LIST%" 2>nul
  802. del "%ALL_FILES_LIST%" 2>nul
  803. REM Remove empty directories
  804. for %%D in (configs captures media) do (
  805. dir /b "%DEST%\%%D" 2>nul | findstr "." >nul || rmdir "%DEST%\%%D" 2>nul
  806. )
  807. echo.
  808. echo ============================================================
  809. echo ^|^> Download Complete - Summary
  810. echo ============================================================
  811. echo.
  812. echo STORAGE INFORMATION:
  813. echo Storage Drive : %STORAGE_DRIVE%
  814. echo Drive Status : %DRIVE_STATUS%
  815. echo Base Path : %STORAGE_BASE%
  816. echo.
  817. echo FROM COLLECTOR BUNDLES:
  818. echo Collector bundles found : %COLLECTOR_COUNT%
  819. echo Saved configs ^(ns.conf^) : %CONFIG_COUNT%
  820. echo Running configs : %RUNNING_COUNT%
  821. echo.
  822. echo FROM CASE ROOT DIRECTORY:
  823. echo Capture files ^(pcap,har^) : %CAPTURE_COUNT%
  824. echo Media files ^(images^) : %MEDIA_COUNT%
  825. echo Other files ^(xml,txt^) : %OTHER_COUNT%
  826. echo.
  827. echo Destination : %DEST%
  828. echo Log file : %LOGFILE%
  829. echo.
  830. REM Write summary to log
  831. echo. >> "%LOGFILE%"
  832. echo ============================================================ >> "%LOGFILE%"
  833. echo SUMMARY >> "%LOGFILE%"
  834. echo ============================================================ >> "%LOGFILE%"
  835. echo STORAGE INFORMATION: >> "%LOGFILE%"
  836. echo Storage Drive : %STORAGE_DRIVE% >> "%LOGFILE%"
  837. echo Drive Status : %DRIVE_STATUS% >> "%LOGFILE%"
  838. echo. >> "%LOGFILE%"
  839. echo FROM COLLECTOR BUNDLES: >> "%LOGFILE%"
  840. echo Collector bundles found : %COLLECTOR_COUNT% >> "%LOGFILE%"
  841. echo Saved configs : %CONFIG_COUNT% >> "%LOGFILE%"
  842. echo Running configs : %RUNNING_COUNT% >> "%LOGFILE%"
  843. echo. >> "%LOGFILE%"
  844. echo FROM CASE ROOT DIRECTORY: >> "%LOGFILE%"
  845. echo Capture files : %CAPTURE_COUNT% >> "%LOGFILE%"
  846. echo Media files : %MEDIA_COUNT% >> "%LOGFILE%"
  847. echo Other files : %OTHER_COUNT% >> "%LOGFILE%"
  848. echo ============================================================ >> "%LOGFILE%"
  849. echo ============================================================
  850. echo.
  851. REM Open destination folder
  852. choice /C YN /M "Open destination folder now?"
  853. if !ERRORLEVEL! EQU 1 explorer "%DEST%"
  854. endlocal
  855. exit /b 0