Batch Hex to Asciic makes the string useless? - windows

I belive i found a nice way to send out a command line to multiple batch windows at once, here is how i see it going:
Using Set with /p allows me to user input a string that i save as message
Exporting it to its own bat file with "Echo set command=%message% >command.bat
Using echo to type out ftp information and then run it with "Ftp -s:upload.txt ftp.example.com
This uploads it to my webhotel where im gonna use a bitadmin.exe to download the file
Then running command.bat and ensuring that if the string %command% isnt the same before it runs the command
It's all working fine and what not, but i feel like my ftp name/password is too exposed to anyone that has the IQ to "Right-Click, Edit" and inspect the code. I've come to the point where i'm trying to make it harder to really get it just by looking at the batch file.
So far i converted my password into HEX and using a fancy batch file convert it back into a string from a batch file i call, and then export it with the ftp connection file. It works all up untill the batch reads the ftp connection file and gets stuck on "requiring password" even though when i check my ftp connection file then the password is correctly typed in the right place, no additional spaces or odd stuff. But it wont work untill i deleted the line and typed it in my self. (The other way that i was considering using a Batch to exe program but that just runs the batch file in the temp folder, everyone knows that) The hex password i use here is just "password"
Code so far:
#Echo Off
:Start
Rem (1)Set/(2)Export Message
Rem (1)
Set /p message=
Rem (2)
Echo set run=%message% >command.bat
Rem (1)Decode Hex/(2)Running hex output/(3)Acces FTP service
Rem (1)
Start HEX.bat 70617373776f7264
Rem (2)
Call CODE.bat
Del /Q CODE.bat
Rem (3)
Echo darkrock> upload.txt
Echo %code%>> upload.txt
Echo asc>>upload.txt
Echo put command.bat>> upload.txt
Echo quit >> upload.txt
Ftp -s:upload.txt ftp.example.com
Goto :Start
The hex converter comes here:
#echo off
setlocal DisableDelayedExpansion
set Caret=^^
set ControlChar={SOH} {STX} {ETX} {EOT} {ENQ} {ACK} {BEL} {BS} {HT} {LF} {VT} {FF} {CR} {SO} {SI} {DLE} {XON}
set ControlChar=%ControlChar% {DC2} {XOFF} {DC4} {NAK} {SYN} {ETB} {CAN} {EM} {SUB} {ESC} {FS} {GS} {RS} {US}
set AsciiChar= !"#$%%&'()*+,-./0123456789:;<=>?
setlocal EnableDelayedExpansion
set AsciiChar=!AsciiChar!#ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]!Caret!_`abcdefghijklmnopqrstuvwxyz{^|}~
set AsciiChar=!AsciiChar!€‚ƒ„…†‡ˆ‰Š‹ŒŽ‘’“”•–—˜™š›œžŸ ¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿
set AsciiChar=!AsciiChar!ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ
set HexCode=%1
call :Conversion
echo Hex: %1
echo Dec: %DecimalCode%
echo Ascii: !AsciiCode!
echo Bin: %BinaryCode%
cls
goto :EOF
:Conversion
set DecimalCode=
set AsciiCode=
set BinaryCode=
:ConvertHex
set /A Decimal=0x%HexCode:~0,2%, DecChar=Decimal-32
set DecimalCode=%DecimalCode%%Decimal%,
if %Decimal% lss 32 (
if %Decimal% equ 0 (
set AsciiCode=!AsciiCode!{NUL}
) else (
for /F "tokens=%Decimal%" %%c in ("%ControlChar%") do set AsciiCode=!AsciiCode!%%c
)
) else (
set AsciiCode=!AsciiCode!!AsciiChar:~%DecChar%,1!
)
call :DecToBin
set BinaryCode=%BinaryCode%%Binary%,
set HexCode=%HexCode:~2%
if defined HexCode goto ConvertHex
exit /B
:DecToBin
set Binary=
for /L %%i in (1,1,8) do (
set /A "Bit=Decimal&1, Decimal>>=1"
set Binary=!Bit!!Binary!
)
echo set code=%AsciiCode% >Code.bat
exit /B
Thx in advance! :) It might be me that missed something in the code but i belive i tried by best the last two days compared to being busy with collage.

Related

download file from FTP and store those files in daily dated folder [duplicate]

I am writing a script using ftp.exe to download files from an FTP server, it works at first. But the version I wrote was suited for only one file and the current date. My script is below:
echo user>>ftp.txt
echo password>>ftp.txt
set prefix=%date:~0,10%
set "name=%prefix%.txt"
echo get %name% >> ftp.txt
echo bye >> ftp.txt
ftp -s:ftp.txt ftpserver.com
del ftp.txt
But now there are more than one file named like aa-bb-2011-09-13.0.log,
aa-bb-2011-09-13.1.log,
aa-bb-2011-09-13.10.log. The last number is a serial number, it could be 0, 1, 2, 3...
How could download these files by batch script? How to modify my script to download more than one file (the number is unknown) which file name pattern is yesterday?
In terms of downloading multiple files, use mget instead of get. The former allows you to specify wildcards for getting rather than specific files.
You'll just have to construct the "name" with a wildcard pattern, and make sure you have a prompt in your script before mget otherwise it will ask for confirmation on every file.
This is untested, but it's probably as simple as changing:
echo get %name% >> ftp.txt
to something like:
echo prompt>>ftp.txt
echo mget *%prefix%*>>ftp.txt
In terms of getting yesterdays date, you can use the following script. It's pretty complex compared to what you would do in, for example bash, but it works.
#setlocal enableextensions enabledelayedexpansion
#echo off
rem Get the date from WMI (on one line).
for /f "skip=2 tokens=2-7 delims=," %%A in ('wmic
path win32_localtime get day^,month^,year^ /format:csv') do (
set /a "yest_yyyy = %%C"
set /a "yest_mm = %%B"
set /a "yest_dd = %%A"
)
rem Not the first of the month, just decrement day.
if not %yest_dd%==1 (
set /a yest_dd = yest_dd - 1
goto done
)
rem Jan 1, set to Dec 31 previous year.
if %yest_mm%==1 (
set /a "yest_dd = 31"
set /a "yest_mm = 12"
set /a "yest_yyyy = yest_yyyy - 1"
goto :done
)
rem Any other day, decrement month.
set /a "yest_mm = yest_mm - 1"
rem Need to find last day, default to 31.
set dim=31
rem Apr/Jun/Sep/Nov all have 30 days. Feb gets special handling.
if %yest_mm%==4 set dim=30
if %yest_mm%==6 set dim=30
if %yest_mm%==9 set dim=30
if %yest_mm%==11 set dim=30
if not %yest_mm%==2 goto :got_dim
rem Default Feb to 28 then use rules to override.
set dim=28
set /a "divid=yest_yyyy%%400"
if "%divid%"=="0" goto daysinmonth_29days
set /a "divid=yest_yyyy%%100"
if "%divid%"=="0" goto :done
set /a "divid=yest_yyyy%%4"
if not "%divid%"=="0" goto :done
rem Adjust to 29 days.
:daysinmonth_29days
set dim=29
:done
rem Pad out and return value.
if %yest_mm% lss 10 set yest_mm=0%yest_mm%
if %yest_dd% lss 10 set yest_dd=0%yest_dd%
set yesterday=%yest_yyyy%-%yest_mm%-%yest_dd%
endlocal && set yesterday=%yesterday%
It will set the yesterday environment variable to the format YYYY-MM-DD so that you can use it in your current script. Simply invoke call yesterday.cmd and then use the environment variable.
It's a pretty complex task to implement with Windows batch-file and the built-in FTP client (ftp.exe).
It would be more easier with PowerShell.
And even easier using a more capable FTP client, like the latest version of WinSCP FTP client.
If you want to download files based on a pattern in a file name, this will do:
winscp.com /ini=nul /log=yesterday.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"get /remote/path/*%%TIMESTAMP-1D#yyyy-mm-dd%%* C:\local\path\" ^
"exit"
This uses the %TIMESTAMP% syntax
If you want to download based on a file modification time, use a file mask with a time-constraint:
winscp.com /ini=nul /log=yesterday.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"get /remote/path/*>=yesterday<today C:\local\path\" ^
"exit"
The >=yesterday<today syntax is supported by WinSCP 5.15 and newer.
In older versions of WinSCP, you can again use %TIMESTAMP% syntax, particularly >=%%TIMESTAMP-1D#yyyy-mm-dd%%<%%TIMESTAMP#yyyy-mm-dd%%, instead of >=yesterday<today.
(I'm the author of WinSCP)
This is a sample FTP script that does almost exactly what you need but it uses a 3rd party client instead of the one that comes free with Windows: http://kb.robo-ftp.com/script_library/show/45
Maybe you can convert it.

How do you make a batch file check for duplicate batch files?

How do you check for already existing batch files in batch script?
I'm working on a batch RPG game where you can log in or create an account. I have successfully been able to set up a code that notifies the user when the username has a space in it, and how it can't be used, but I'm stumped at how it could be done to check for a duplicate batch file. For example, I already have a username called "Test", and I would like to make another username called "Test"...
Here is a copy of my code for the no spaces script:
:createuser
echo.
echo What would you like your Username to be?
set /p username1=
set v1f=0
:checkforspaces
set x=!v1f!
set Letter%v1f%=!username1:~%x%,1!
if "!Letter%v1f%!" EQU " " (
echo.
echo.
echo Sorry you cant use spaces in your Username.
pause>nul
goto entergame
)
if NOT "!Letter%v1f%!" EQU "" (
set /a v1f=%v1f%+1
goto checkforspaces
)
echo.
echo What would you like your Password to be?
set /p password1=
goto DATA_VALUES
To check for spaces in var:
echo %var%|find " " >nul
if errorlevel 1 (echo no spaces) else (echo spaces found)
to check whether the filename var exists:
if exist %var%.ext (echo file %var%.ext exists) else (echo file %var%.ext not found)
btw - an easy-peasy game-save routine is
set>%var%.ext
and reload is
for /f "delims=" %%a in (%var%.ext) do set %%a
note that var above can be any variable-name and .ext your chosen file extension.

How to get attributes of a file using batch file

I am trying to make a batch file to delete malicious files from pendrive. I know that these malicious files uses hidden,read only and system attributes mainly to hide itself from users. Currently i am deleting these files using cmd by removing malicious files attributes then deleting it. Now I am thinking to make a small batch file which can be used to remove these files just by entering the drive letter.
I have found this code in a website to find attributes of a file. But after entering the name of the file the batch file just exits without showing any results.
#echo off
setlocal enabledelayedexpansion
color 0a
title Find Attributes in Files
:start
set /p atname=Name of the file:
if not exist %atname% (
cls
echo No file of that name exists!
echo.
echo Press any key to go back
pause>nul
goto start
)
for /f %%i in (%atname%) do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
echo Press any key to go back
pause>nul
goto start
can you tell me why this batch file is exiting without showing any results. Or can you give any better batch script for getting attributes of a file.
EDIT
I was able to work the above code only for a single file. As my purpose of my batch file is to remove malicious files by entering the drive letter. How can i use it to find what kind of attributes files are using in a particular drive.
For example:
In cmd we can use this command to find the file attributes of a given drive
attrib *.*
Advance thanks for your help
I tried the bat file (without inspecting the details) and it seems to work fine for me. What I noticed is that it closes instantly if you don't enclose file path with quotation marks - e.g. "file". Example:
Name of the file: path\file.txt // this will close immediately
Name of the file: "path\file.txt" // now it will stay open and display the result
This hopefully solves your problem.
As far as your question in EDIT is concerned, a simple option is to iterate a list of files and execute the batch on each one.
batch1.bat: (%1 refers to the first command-line parameter)
#echo off
setlocal enabledelayedexpansion
echo %1
set atname=%1
for %%i in ("%atname%") do set attribs=%%~ai
set attrib1=!attribs:~0,1!
set attrib2=!attribs:~1,1!
set attrib3=!attribs:~2,1!
set attrib4=!attribs:~3,1!
set attrib5=!attribs:~4,1!
set attrib6=!attribs:~5,1!
set attrib7=!attribs:~6,1!
set attrib8=!attribs:~7,1!
set attrib9=!attribs:~8,1!
cls
if %attrib1% equ d echo Directory
if %attrib2% equ r echo Read Only
if %attrib3% equ a echo Archived
if %attrib4% equ h echo Hidden
if %attrib5% equ s echo System File
if %attrib6% equ c echo Compressed File
if %attrib7% equ o echo Offline File
if %attrib8% equ t echo Temporary File
if %attrib9% equ l echo Reparse point
echo.
echo.
Next, generate a list of all files within a given path (say 'folder' including all subfolders):
dir /s /b folder > ListOfFiles.txt
main.bat (read ListOfFiles.txt line-by-line and pass each line to batch1.bat as a command line parameter):
#echo off
for /f "tokens=*" %%l in (ListOfFiles.txt) do (batch1.bat %%l)
Then, from cmd:
main.bat >> output.txt
The last step generates an output file with complete results. Granted, this can be done in a more polished (and probably shorter) way, but that's one obvious direction you could take.
You're using a for /f loop here, which isn't necessary (and may yield undesired results if the filename contains spaces). Change this:
for /f %%i in (%atname%) do set attribs=%%~ai
into this:
for %%i in ("%atname%") do set attribs=%%~ai
This is dangerous code - but it'll delete read only, hidden and system files.
It should fail to run on c: drive but I haven't tested it. Note that some Windows installs are on drives other than c:
#echo off
echo "%cd%"|find /i "c:\" >nul || (
del *.??? /ar /s /f
del *.??? /ah /s
del *.??? /as /s
)

VBScript and Batch interaction

I am running a batch script and somewhere the user has to access a database.
At this moment, a window made in vbscript would prompt asking the user to type in the login and password. (OK, Cancel buttons)
If the credentials are correct after the OK, the batch would continue according to planA, otherwise the batch would do something else going to planB. If (Cancel), it would return to the batch and the main menu.
THIS IS WHAT I HAVE BEEN STRUGGLING WITH:
#echo off
:Ini
echo [1] Access database
echo [2] Main menu
echo:
set /p Quest= What do you prefer (1 / 2)?
if not '%Quest%'=='' set Quest=%Quest:~0,1%
if '%Quest%'=='1' goto VBS
if '%Quest%'=='2' goto BATCH
echo Invalid option, please try again
cls
goto Ini
:BATCH
echo Heading for main menu ...
goto Main
:VBS
:wscript.echo InputBox("Enter your password","VBScript-Batch")
findstr "^:" "%~sf0" | findstr /i /v ":Label" >temp.vbs
for /f "delims=" %%N in ('cscript //nologo temp.vbs') do set pass=%%N
del temp.vbs
:Label1
If %pass%=="okay" echo Valid Password ! & goto PLAN-A
If not %pass%=="okay" echo Invalid Password !! & goto PLAN-B
:PLAN-A
echo continue from here
:PLAN-B
echo do something else
(...)
-- How to capture the user information, validate it and go back to the batch for the planA or planB ??
As you see, if we eliminate the "& goto PLAN" stuff the script works. It sends the VBS input "pass" to the batch and the batch echoes "continue from here" or "do something else", from where the rest of your code should continue in the same batch.
However, it is not working ... Any help to make this really work ?
Your primary issue was you didn't set up the file properly to facilitate extracting the VBS from the batch file. Your VBS looks no different than a batch label. You filter out "Label" labels, but you still include lines like :ini, :BATCH, etc. Obviously those will trip up VBS. I solved the problem by prefixing your VBS with ::: and adapting your filter. There is no need to explicitly filter out any labels. I chose 3 colons because a single colon is used for a label, and 2 colons is frequently used for comments. You could have multiple independent VBS scripts embedded within your batch simply by varying the number of preceding colons.
I also restructured the code a small amount, and sprinkled in some EXIT /B statements so that code does not fall through. Also your :MAIN is not defined so I commented out the GOTO and replaced it with EXIT /B.
#echo off
:Ini
echo [1] Access database
echo [2] Main menu
echo:
set /p Quest= What do you prefer (1 / 2)?
if not '%Quest%'=='' set Quest=%Quest:~0,1%
if '%Quest%'=='1' goto VBS
if '%Quest%'=='2' goto BATCH
echo Invalid option, please try again
cls
goto Ini
:BATCH
echo Heading for main menu ...
::goto Main
exit /b
:VBS
:::wscript.echo InputBox("Enter your password","VBScript-Batch")
findstr "^:::" "%~sf0" >temp.vbs
for /f "delims=" %%N in ('cscript //nologo temp.vbs') do set pass=%%N
del temp.vbs
If "%pass%"=="okay" (
echo Valid Password !
goto PLAN-A
) else (
echo Invalid Password !!
goto PLAN-B
)
:PLAN-A
echo continue from here
exit /b
:PLAN-B
echo do something else
exit /b

Windows batch script to download yesterday files

I am writing a script using ftp.exe to download files from an FTP server, it works at first. But the version I wrote was suited for only one file and the current date. My script is below:
echo user>>ftp.txt
echo password>>ftp.txt
set prefix=%date:~0,10%
set "name=%prefix%.txt"
echo get %name% >> ftp.txt
echo bye >> ftp.txt
ftp -s:ftp.txt ftpserver.com
del ftp.txt
But now there are more than one file named like aa-bb-2011-09-13.0.log,
aa-bb-2011-09-13.1.log,
aa-bb-2011-09-13.10.log. The last number is a serial number, it could be 0, 1, 2, 3...
How could download these files by batch script? How to modify my script to download more than one file (the number is unknown) which file name pattern is yesterday?
In terms of downloading multiple files, use mget instead of get. The former allows you to specify wildcards for getting rather than specific files.
You'll just have to construct the "name" with a wildcard pattern, and make sure you have a prompt in your script before mget otherwise it will ask for confirmation on every file.
This is untested, but it's probably as simple as changing:
echo get %name% >> ftp.txt
to something like:
echo prompt>>ftp.txt
echo mget *%prefix%*>>ftp.txt
In terms of getting yesterdays date, you can use the following script. It's pretty complex compared to what you would do in, for example bash, but it works.
#setlocal enableextensions enabledelayedexpansion
#echo off
rem Get the date from WMI (on one line).
for /f "skip=2 tokens=2-7 delims=," %%A in ('wmic
path win32_localtime get day^,month^,year^ /format:csv') do (
set /a "yest_yyyy = %%C"
set /a "yest_mm = %%B"
set /a "yest_dd = %%A"
)
rem Not the first of the month, just decrement day.
if not %yest_dd%==1 (
set /a yest_dd = yest_dd - 1
goto done
)
rem Jan 1, set to Dec 31 previous year.
if %yest_mm%==1 (
set /a "yest_dd = 31"
set /a "yest_mm = 12"
set /a "yest_yyyy = yest_yyyy - 1"
goto :done
)
rem Any other day, decrement month.
set /a "yest_mm = yest_mm - 1"
rem Need to find last day, default to 31.
set dim=31
rem Apr/Jun/Sep/Nov all have 30 days. Feb gets special handling.
if %yest_mm%==4 set dim=30
if %yest_mm%==6 set dim=30
if %yest_mm%==9 set dim=30
if %yest_mm%==11 set dim=30
if not %yest_mm%==2 goto :got_dim
rem Default Feb to 28 then use rules to override.
set dim=28
set /a "divid=yest_yyyy%%400"
if "%divid%"=="0" goto daysinmonth_29days
set /a "divid=yest_yyyy%%100"
if "%divid%"=="0" goto :done
set /a "divid=yest_yyyy%%4"
if not "%divid%"=="0" goto :done
rem Adjust to 29 days.
:daysinmonth_29days
set dim=29
:done
rem Pad out and return value.
if %yest_mm% lss 10 set yest_mm=0%yest_mm%
if %yest_dd% lss 10 set yest_dd=0%yest_dd%
set yesterday=%yest_yyyy%-%yest_mm%-%yest_dd%
endlocal && set yesterday=%yesterday%
It will set the yesterday environment variable to the format YYYY-MM-DD so that you can use it in your current script. Simply invoke call yesterday.cmd and then use the environment variable.
It's a pretty complex task to implement with Windows batch-file and the built-in FTP client (ftp.exe).
It would be more easier with PowerShell.
And even easier using a more capable FTP client, like the latest version of WinSCP FTP client.
If you want to download files based on a pattern in a file name, this will do:
winscp.com /ini=nul /log=yesterday.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"get /remote/path/*%%TIMESTAMP-1D#yyyy-mm-dd%%* C:\local\path\" ^
"exit"
This uses the %TIMESTAMP% syntax
If you want to download based on a file modification time, use a file mask with a time-constraint:
winscp.com /ini=nul /log=yesterday.log /command ^
"open ftp://username:password#ftp.example.com/" ^
"get /remote/path/*>=yesterday<today C:\local\path\" ^
"exit"
The >=yesterday<today syntax is supported by WinSCP 5.15 and newer.
In older versions of WinSCP, you can again use %TIMESTAMP% syntax, particularly >=%%TIMESTAMP-1D#yyyy-mm-dd%%<%%TIMESTAMP#yyyy-mm-dd%%, instead of >=yesterday<today.
(I'm the author of WinSCP)
This is a sample FTP script that does almost exactly what you need but it uses a 3rd party client instead of the one that comes free with Windows: http://kb.robo-ftp.com/script_library/show/45
Maybe you can convert it.

Resources