Jump to content
kvk1989

python to delphi code convert

Recommended Posts

from src.common import to_bytes, from_bytes

import usb
import array
import struct

def bruteforce(device, config, dump_ptr, dump=False):

    addr = config.watchdog_address + 0x50

    # We don't need to wait long, if we succeeded
    # noinspection PyBroadException
    try:
        device.dev.timeout = 1
    except Exception:
        pass

    udev = device.udev

    try:
        # noinspection PyProtectedMember
        udev._ctx.managed_claim_interface = lambda *args, **kwargs: None
    except AttributeError as e:
        raise RuntimeError("libusb is not installed for port {}".format(device.dev.port)) from e

    linecode = udev.ctrl_transfer(0xA1, 0x21, 0, 0, 7) + array.array('B', [0])

    if dump:
        try:
            device.cmd_da(0, 0, 1)
            device.read32(addr)
        except:
            pass

        for i in range(4):
            udev.ctrl_transfer(0x21, 0x20, 0, 0, linecode + array.array('B', to_bytes(dump_ptr - 6 + (4 - i), 4, '<')))
            udev.ctrl_transfer(0x80, 0x6, 0x0200, 0, 9)

        brom = bytearray(device.cmd_da(0, 0, 0x20000))
        brom[dump_ptr - 1:] = b"\x00" + to_bytes(0x100030, 4, '<') + brom[dump_ptr + 4:]
        return brom

    else:
        try:
            device.cmd_da(0, 0, 1)
            device.read32(addr)
        except:
            pass

        for address in range(dump_ptr, 0xffff, 4):
            for i in range(3):
                udev.ctrl_transfer(0x21, 0x20, 0, 0, linecode + array.array('B', to_bytes(address - 5 + (3 - i), 4, '<')))
                udev.ctrl_transfer(0x80, 0x6, 0x0200, 0, 9)
            try:
                if(len(device.cmd_da(0, 0, 0x40))) == 0x40:
                    return (True, address)
            except RuntimeError:
                try:
                    device.read32(addr)
                except:
                    return (False, address + 4)
            except Exception:
                return (False, address + 4)

 

Share this post


Link to post
unit USBBruteforce;

interface

uses
  SysUtils, Classes, CPort, ConfigClass, Logger, StructConversion;
// Include your USB communication library here

function BruteForce(device: TComPort; const config: TObject; dump_ptr: Integer;
  dump: Boolean): TBytes;

implementation

uses Unit4;

function ToBytes(Value: Integer; Size: Integer; Endian: Char): TBytes;
begin
  SetLength(Result, Size);
  if Endian = '<' then
    for var i := 0 to Size - 1 do
      Result[i] := Byte((Value shr (i * 8)) and $FF)
  else
    for var i := Size - 1 downto 0 do
      Result[Size - 1 - i] := Byte((Value shr (i * 8)) and $FF);
end;

function FromBytes(Bytes: TBytes; Endian: Char): Integer;
begin
  var
  Size := Length(Bytes);
  Result := 0;
  if Endian = '<' then
    for var i := Size - 1 downto 0 do
      Result := (Result shl 8) or Bytes[i]
  else
    for var i := 0 to Size - 1 do
      Result := (Result shl 8) or Bytes[i];
end;

function BruteForce(device: TComPort; const config: TObject; dump_ptr: Integer;
  dump: Boolean): TBytes;
var
  addr: Integer;
  linecode: TBytes;
  brom: TBytes;
  i, address: Integer;
begin
  addr := TConfig(config).WatchdogAddress + $50;
  // Replace TYourConfigClass with your actual config class type

  try
    device.Timeouts.ReadInterval := 1;
  except
    // Handle timeout setting failure
  end;

  // Handle interface claiming based on your USB library
  // Placeholder code:
  // try
  // USBLibrary.ClaimInterface(device);
  // except
  // raise Exception.Create('Failed to claim interface');
  // end;

  SetLength(linecode, 8);
  // Placeholder code for obtaining linecode bytes
  // Replace the following line with actual code to get linecode bytes from the device
  // Example: linecode := USBLibrary.GetLinecodeBytes(device);

  if dump then
  begin
    try
      // Placeholder code for sending commands and reading data from the device
    except
      // Handle command sending or data reading failure
    end;

    for i := 0 to 3 do
    begin
      // Placeholder code for sending control transfers
    end;

    // Placeholder code for reading data from the device
    // Replace the following line with actual code to read data from the device
    // Example: brom := USBLibrary.ReadData(device, dump_ptr, 0x20000);

    Move(ToBytes($100030, 4, '<')[0], brom[dump_ptr - 1], 4);
    Result := brom;
  end
  else
  begin
    try
      // Placeholder code for sending commands and reading data from the device
    except
      // Handle command sending or data reading failure
    end;

    for address := dump_ptr to $FFFF do
    begin
      // Placeholder code for sending control transfers
      for i := 0 to 2 do
      begin
        // Placeholder code for sending control transfers
      end;

      try
        // Placeholder code for sending commands and reading data from the device
        // Replace the following line with actual code to read data from the device
        // Example: brom := USBLibrary.ReadData(device, address - 5, $40);

        if Length(brom) = $40 then
        begin
          Result := ToBytes(address, 2, '<');
          Exit;
        end;
      except
        on E: Exception do
        begin
          try
            // Placeholder code for sending commands and reading data from the device
          except
            Result := ToBytes(address + 4, 2, '<');
            Exit;
          end;
        end;
      end;
    end;
  end;
end;

end.

is this right code ? please tell me 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×