Jump to content











Photo
- - - - -

[RELEASE] catedit.g4b - script to write hex values


  • Please log in to reply
11 replies to this topic

#1 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 August 2013 - 01:41 PM

Here is small .script (just below 1024 bytes) that allows to "hexedit" files or device sectors from command line.

Not particularly (please read as completely unlike ) foolproof :w00t:, but less half-@§§ed than usual, actually working and potentially very dangerous :ph34r:.

Kids, play with this on something of which you have a copy or backup (bytes, once written, tend to stay written ;)).

!BAT
setlocal
if %1.==. goto :help
set ddwrite=%~dpnx0
set target=%1
shift
set offset=%1
if not exist offset goto :help
set /a offset=%offset% > nul
set /a counter=0 > nul

:p_loop
shift
if %1.==. goto :show
set /a skip=%offset%+%counter% > nul
cat --hex --skip=%skip% --length=1 %target% | set tbyte=
cat --hex --skip=%skip% --locate=\x%tbyte:~10,2% --number=1 --replace=\x%1 %target%
set /a counter=%counter%+1 > nul
goto :p_loop

:show
cat --hex --skip=%offset% --length=%counter% %target%
goto :EOF

:help
echo catedit.g4b - small script to write hex values (bytes) to file or device
echo - by jaclaz - completely FREE under Careware License
echo Usage:
echo catedit.g4b <target> <offset> [space separated hex values]
echo
echo Examples:
echo catedit.g4b (fd0)0+1 510 55 AA
echo will write the "Magic Bytes" 55AA on the first sector of first floppy device
echo
echo catedit.g4b (hd0,0)/boot.ini 54 3D 31 30
echo will write "=10" at offset 54 in the file (hd0,0)/BOOT.INI
goto :EOF

:cheers:

Wonko

Attached Files


  • Brito likes this

#2 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 13 August 2013 - 02:41 PM

looks handy. So offset is in decimal but byte values are in hex?  If for offset we use 0xnnn (e.g.  0x1fe)  will that be used as a hex offset?



#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 August 2013 - 04:25 PM

looks handy. So offset is in decimal but byte values are in hex?  If for offset we use 0xnnn (e.g.  0x1fe)  will that be used as a hex offset?

 

Sure :),
as long as you prepend to the offset the "0x" prefix, the "set /a" will understand that it is a hex value and everything will be cool.
The actual "textual" byte values must NOT have the "0x" prefix.

The idea is that it is easy (Tiny Hexer and most other hex/disk editors) to get "textual hex" which is space separated.
Since I use the batch parameters as a parsing method, this implies that also the output of cat --hex (which has a double space every 4 bytes) can be used *almost* directly.
I.e. something like:



cat --hex --skip=0 --length=16 (hd0,0)/boot.ini | set myline=
set my_addr=0x%myline:~0,8%
set my_bytes=%myline:~10,50%

:cheers:
Wonko



#4 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 13 August 2013 - 05:22 PM

maybe add an example to the usage: text for using hexadecimal offset value then?



#5 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 August 2013 - 05:38 PM

maybe add an example to the usage: text for using hexadecimal offset value then?

You mean like the one that was there initially and that I removed to have the thingy fit in 1024 bytes? :dubbio:

:cheers:
Wonko

#6 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 13 August 2013 - 05:46 PM

sorry, my crystal ball is at the cleaners!

You could remove a few blank lines?

Or why not remove the word 'small' and change the example line to

echo catedit.g4b (fd0)0+1 0x1FE 55 AA

thus saving 4 bytes!

P.S. Why is 1024 significant?  Will anyone (apart from Wonko) cry if it was 1026?



#7 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 August 2013 - 05:58 PM

sorry, my crystal ball is at the cleaners!
You could remove a few blank lines?
Or why not remove the word 'small' and change the example line to







echo catedit.g4b (fd0)0+1 0x1FE 55 AA
thus saving 4 bytes!
P.S. Why is 1024 significant?  Will anyone (apart from Wonko) cry if it was 1026?

 

Cannot say about other people. :dubbio:

On a floppy image full up to the brim of grub4dos files + grub4dos utilities + .g4b files, one sector is one sector :smiling9:

Why don't you make as usual a fork of the script, add/change whatever you see fit, and then host it on your site? :whistling:

:cheers:
Wonko



#8 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 13 August 2013 - 06:11 PM

Why don't you make as usual a fork of the script, add/change whatever you see fit, and then host it on your site? 

Because I have already suggested a good alternative which saves a few bytes and I am sure the author is not the sort of guy to let petty pride get in the way of accepting good suggestions in order to improve his code... :devil:  :whistling:



#9 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 13 August 2013 - 07:03 PM

Because I have already suggested a good alternative which saves a few bytes and I am sure the author is not the sort of guy to let petty pride get in the way of accepting good suggestions in order to improve his code... :devil:  :whistling:

 

Well, no.

You have provided NOT any suggestions to the code, you suggested to add something to the "help" part, WITHOUT providing adequate (sufficient) means to remain within the 1024 bytes (as a matter of fact hinting how the Author's choice to fit the script within 1024 bytes was nonsensical :w00t:).

Just as an example you could have noticed how the "set ddwrite=%~dpnx0" line was not needed ;).

 

However :), see if you like this better:





!BAT
setlocal
if %1.==. call :h echo && goto :EOF
set t=%1
shift
set o=%1
if not exist o goto :h
set /a o=%o% > nul
set /a c=0 > nul

:l
shift
if %1.==. goto :s
set /a sk=%o%+%c% > nul
cat --hex --skip=%sk% --length=1 %t% | set tb=
cat --hex --skip=%sk% --locate=\x%tb:~10,2% --number=1 --replace=\x%1 %t%
set /a c=%c%+1 > nul
goto :l

:s
cat --hex --skip=%o% --length=%c% %t%
goto :EOF

:h
%1 catedit.g4b - small script to write hex values (bytes) to file or device
%1 - by jaclaz - completely FREE under Careware License
%1 Usage:
%1 catedit.g4b <target> <offset> [space separated hex values]
%1
%1 Examples:
%1 catedit.g4b (fd0)0+1 510 55 AA
%1 will write the "Magic Bytes" 55AA on the first sector of first floppy device
%1
%1 catedit.g4b (fd0)0+1 0x1FE 55 AA will do the same, the offset can be a hex
%1 number (with prefix "0x")
%1
%1 catedit.g4b (hd0,0)/boot.ini 54 3D 31 30
%1 will write "=10" at offset 54 in the file (hd0,0)/BOOT.INI
%1
%1 This batch is 1024 bytes long. :-)

:cheers:

Wonko

Attached Files



#10 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 13 August 2013 - 07:25 PM

:thumbsup:



#11 halikus

halikus

    Frequent Member

  • Advanced user
  • 110 posts
  •  
    Canada

Posted 15 August 2013 - 01:37 AM

This looks like it could be very useful.  I also know this will easily bugger a HDD or file if im not careful.  I'm looking forward to how more advanced users incorporate it into their commands to find some practical uses for it to fix bootloaders and such.



#12 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 15 August 2013 - 02:55 PM

This looks like it could be very useful.  I also know this will easily bugger a HDD or file if im not careful.  I'm looking forward to how more advanced users incorporate it into their commands to find some practical uses for it to fix bootloaders and such.

Yes, "as is" it is simply a tool comparable to hexalter:

http://kuwanger.net/.../hexalter.shtml

here is an example of the practical use of such a tool:

http://reboot.pro/to...e-event-viewer/

or - to some extents - comparable to echoo.com or bpatcher.

 

In due time it might be possible to "convert" to "pure" grub4dos small, simple batch tools like:

http://reboot.pro/to...atch-001-alpha/

http://reboot.pro/to...-alpha-release/

 

The first has been (in read only mode) already "converted":

http://reboot.pro/to...l-for-grub4dos/

but there was the need for a "practical" way of modifying bytes to proceed with editing features.

 

grub4dos has built-in dd that is already an almost complete replacement for the tools in the DSFOK toolkit, it was a lot of time I had - in the back of my mind - the idea to script a "command line hex editor", but initially I thought about using as base for it the same dd, when I realized that cat --hex features could be used instead.

 

 

:cheers:

Wonko






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users