How to find the sequence of numbers in a txt file and save it with CMD of Windows 10?

Viewed 227

I have a file authen.txt being in real a file with binary data because of containing also bytes with the decimal values 1, 3, 5, 6, 8, 11, 12, 15 and 22. The binary bytes in text block below are replaced by other characters for better display by the browsers using following table with hexadecimal values:

01 ... B1 ... ± ... two occurrences
03 ... B3 ... ³ ... two occurrences
05 ... B5 ... µ ... seven occurrences
06 ... B6 ... ¶ ... one occurrence
08 ... B8 ... ¸ ... two occurrences
0B ... BB ... » ... two occurrences
0C ... BC ... ¼ ... one occurrence
0F ... BF ... ¿ ... three occurrences
16 ... C6 ... Æ ... one occurrence

I want to separate the sequence of numbers like 1000xxxxxx .. and save them into a file uid.txt. I use CMD on Windows 10.

My file authen.txt with the binary bytes replaced as listed above:

±   ¸µ ¶secret  0a96f39c64da3faebb9a5b8f4c0df70dµ ¼access_token ÊEAAAAUaZA8jlABAFRhsSpAZCpSHZBiEx8zr1MNlyO6EDF84yEq3f4HYck0Pobx3gLfRVTztwsSuzQkHkRGZAdK4WOZCklyyG7kYTGqiyZB369bfCfDEFDFlzXOiqDkaME97X5ZAZCGYlM0vTiZAb5A3uJRdtIeeJZCkXK9xhmdFXJhyQgi0OgpFdFVZAOBZAzXznU40oZD  »is_imported±µ ¸username ¿100067536142472µ ³uid ¿100067536142472µ ¿analytics_claim 5hmac.AR3hvKMOVcvLUwDFoj3xtLU-D1nQnr2ROtWNIazCnr0bm7Raµ »session_key .5.cmnvGeQeV4V_sA.1620719120.29-100067536142472µ Æsession_cookies_string³$[{"name":"c_user","value":"100067536142472","expires":"Wed, 11 May 2022 07:45:20 GMT","expires_timestamp":1652255120,"domain":".facebook.com","path":"/","secure":true},{"name":"xs","value":"29:cmnvGeQeV4V_sA:2:1620719120:-1:-1","expires":"Wed, 11 May 2022 07:45:20 GMT","expires_timestamp":1652255120,"domain":".facebook.com","path":"/","secure":true,"httponly":true},{"name":"fr","value":"1A1PLzebTFjuxoVmE.AWWi1-XI-VNYw9B4ZPSpMkCXq5I.BgmjYQ.kW.AAA.0.0.BgmjYQ.AWULPGUfH58","expires":"Mon, 09 Aug 2021 07:45:19 GMT","expires_timestamp":1628495119,"domain":".facebook.com","path":"/","secure":true,"httponly":true},{"name":"datr","value":"BjaaYN3Xkt5USKMtDNwku5cs","expires":"Thu, 11 May 2023 07:45:20 GMT","expires_timestamp":1683791120,"domain":".facebook.com","path":"/","secure":true,"httponly":true}]"

There are no null bytes in the file.

Content of file authen.txt as displayed by Windows Notepad

2 Answers

This works when the file is exactly formatted as in your example (even when it's not a pure textfile):

@echo off & setlocal 
<authen.txt set /p var=
set "var=%var:*"value":=%"
REM set "var=%var:*uid =%"
>uid.txt echo/%var:~1,12%

Usually, we use a for loop with proper tokens and delims to extract a subtext from a file or string. Due to some special characters (lower than 20 hex), this fails.

Instead, this copies the (first ~1k chars of (limitation of set /p, but that's enough here)) file into a variable. It then deletes everything from the beginning until (including) the string "value": (or uid - pick your choice). Then it simply extracts the relevant substring (we know it's length) and writes it to the output file.

This looks surprisingly short, but there is no verification or check in place. Also any Null-Bytes may stop it from working properly (you claimed there are none).

It looks like the file with the authentication data is a binary file containing multiple strings with a byte for the length of a string left to each string and some other binary data.

So it is not really a text file with characters encoded using a code page like Windows-1252 or a Unicode encoding like UTF-16 or UTF-8. But it is possible to process the authentication data with Windows command processor as long as the file does not contain null bytes. It would be nevertheless better to use a more modern script interpreter than the Windows command processor cmd.exe for processing such files. cmd has no built-in support for string functions. The Windows command processor is designed for running commands and executables and definitely not for enhanced processing of data in files.

Here is a partly commented batch file code to get the universal identifier number 100067536142472 written into a file uid.txt.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "authen.txt" goto EndBatch
rem Read the data from a file and assign it to an environment variable.
set "DataFile="
for /F usebackq^ delims^=^ eol^= %%I in ("authen.txt") do set "DataFile=%%I"
if not defined DataFile goto EndBatch
rem Delete the output file on already existing.
del "uid.txt" 2>nul
rem Enable delayed expansion for safer processing the authentication data.
setlocal EnableDelayedExpansion

:LoopUID
set "DataTemp=!DataFile:*uid =!"
if "!DataFile!" == "!DataTemp!" endlocal & goto EndBatch
:LoopStart
for /F delims^=0123456789^ eol^= %%I in ("!DataTemp:~0,1!") do set "DataTemp=!DataTemp:~1!" & goto LoopStart
set "DataUID="
:LoopEnd
set "DataUID=%DataUID%%DataTemp:~0,1%"
set "DataTemp=!DataTemp:~1!"
for /F delims^=0123456789^ eol^= %%I in ("!DataTemp:~0,1!") do goto OutputUID
goto LoopEnd
:OutputUID
echo !DataUID!>>"uid.txt"
set "DataFile=!DataTemp!" & goto LoopUID

:EndBatch
endlocal

Note: The FOR loop at top is designed to get the last line assigned to the environment variable DataFile. That means if one of the binary bytes has by chance the decimal value 10 (hexadecimal 0A) which is in ASCII table a line-feed (LF), the environment variable DataFile is defined with the data after last byte with decimal value 0A. This could result in uid number not extracted from the file if the file contains one or more bytes with decimal value 10 after the block with uid.

The code is written to extract all uid values in last line in file authen.txt line by line to uid.txt.

The command line after :LoopUID defines the environment variable DataTemp with the string assigned to the environment variable DataFile with having removed everything from the beginning to the end of first occurrence of the string uid using a string substitution.

The loop named LoopUID is exited on string assigned to DataTemp is equal the string assigned to DataFile because of there is no more the string uid in the string assigned currently to the environment variable DataFile.

The loop named LoopStart is for finding the start of the universal identifier number. It analyzes if the first character of the string assigned to variable DataTemp is not a digit character in which case it removes this character from DataTemp and runs the FOR command once again. Otherwise the UID extraction starts on first character in range 0 to 9 found in the string after uid .

The loop named LoopEnd is for appending one digit character after the other to the string assigned to the environment variable DataUID from the string assigned to the variable DataTemp being also removed from this string until the first character of DataTemp is not anymore a digit character.

The determined identifier number is output with redirecting this output into the file uid.txt.

Please note that the code is definitely not fail safe. There are several possible use cases on which this code could result in wrong behavior depending on the authentication data in the file. It would be really better to use VBscript or PowerShell for this task and not a batch file interpreted by the Windows command processor.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • rem /?
  • set /?
  • setlocal /?

Please read also the Microsoft documentation about Using command redirection operators for an explanation of >> and 2>nul.

Related