[VB.Net] Offset Locator Function's by fudmario

Iniciado por fudmario, Octubre 23, 2016, 02:34:13 AM

Tema anterior - Siguiente tema

0 Miembros y 1 Visitante están viendo este tema.

Octubre 23, 2016, 02:34:13 AM Ultima modificación: Octubre 23, 2016, 02:41:04 AM por fudmario

Aquí les dejos del Baul de los recuerdos, lo he actualizado un poco de la version anterior, algunas funciones para armar vuestro Offset Locator, el code esta un poco sucio, pero se puede mejorar...xD
FUNCIONES:

  • AvFucker
  • Dsplit
  • 256 Combinaciones
  • Offset Replace

Ejemplos de Uso:
Código: vbnet

        ' Call Method AvFucker
        OffsetLocator.Methods.AvFucker(filePath := "D:\test.exe",dirpath := "D:\AvFucker", _
                                       offsetStart := 1000, offsetEnd := 63488, _
                                       blockbyte := 100, valueData := "4D")

        'Call Method Dsplit
        OffsetLocator.Methods.DSplit(filepath := "D:\test.exe", dirpath := "D:\dsplit", _
                                     offsetStart := 1000, offsetEnd := 63488, blockBytes := 1000)

        'Call Method 256 Combination
        OffsetLocator.Methods.C256Combination(filepath := "D:\test.exe",dirpath := "D:\combination",offset := 3400)

        'Call Method OffsetReplace
        OffsetLocator.Methods.OffsetReplace(filepath := "D:\test.exe", fileOutput := "D:\test_replace_90.exe", _
                                            offset := 4500, valueData := "90")



Class file = "Methods.vb"

Código: vbnet

' ***********************************************************************
' Assembly         : Demo Offset Locator
' Author           : fudmario
'
' Last Modified By : fudmario
' Last Modified On : 10-23-2016
' ***********************************************************************
' <copyright file="Methods.vb" company="DeveloperTeam">
'     Copyright ©  2016
' </copyright>
' <summary></summary>
' ***********************************************************************

Imports System.Threading.Tasks
Imports System.IO
Namespace OffsetLocator
    Public NotInheritable Class Methods
        Private Const FormatName As String = "{0}_{1}{2}"
        Public Shared Sub AvFucker(ByVal filePath As String, ByVal dirpath As String, _
                                   ByVal offsetStart As Integer, ByVal offsetEnd As Integer, _
                                   ByVal blockbyte As Integer, ByVal valueData As String)
            Dim fileOutput As String
            Dim ext as String = Path.GetExtension(filepath)
            Dim value As Integer = Convert.ToInt32(value:=valueData, fromBase:=16)
            Dim byteArray As Byte() = Internal_GenBlockBytes(CByte(value), blockbyte)
            Dim res As List(Of Tuple(Of Integer, Integer)) = Internal_StepGenList(offsetStart, offsetEnd, blockbyte)
            Parallel.ForEach(res, Sub(m)
                                      fileOutput = Path.Combine(dirpath, String.Format(FormatName, m.Item1, blockbyte, ext))
                                      If m.Item2 <> 0 Then byteArray = Internal_GenBlockBytes(CByte(value), m.Item2)
                                      Internal_OffsetReplaceB(filePath, fileOutput, m.Item1, byteArray)
                                  End Sub)
        End Sub
        Private Shared Sub Internal_OffsetReplaceB(ByVal filepath As String, ByVal fileOutput As String, Byval offset As Integer, ByVal blockByte As Byte())
            Using fs As New FileStream(path:=filepath, mode:=FileMode.Open, access:=FileAccess.Read)
                Using fs2 As New FileStream(path:=fileOutput, mode:=FileMode.Create, access:=FileAccess.Write)
                    fs.CopyTo(fs2)
                    fs2.Position = offset
                    fs2.Write(blockByte, 0, blockByte.Length)
                End Using
            End Using
        End Sub

        Public Shared Sub DSplit(ByVal filepath As String, ByVal dirpath As String, ByVal offsetStart As Integer, Byval offsetEnd As Integer,
                          ByVal blockBytes As Integer)
            Dim c As New FileInfo(filepath)
            Dim res As List(Of Tuple(Of Integer, Integer)) = Internal_StepGenList(offsetStart, offsetEnd, blockBytes)

            Parallel.ForEach(res, Sub(m)
                                      Dim output As String = Path.Combine(dirpath, String.Format(FormatName, m.item1, blockBytes, c.Extension))
                                      CreateFileWithBytes(filepath:=filepath, fileoutput:=output, length:=m.item1)
                                      If m.Item2 > 0 Then
                                          CreateFileWithBytes(filepath, Path.Combine(dirpath, String.Format(FormatName, offsetEnd, m.Item2, c.Extension)), offsetEnd)
                                      End If
                                  End Sub)

        End Sub

        Private Shared Sub CreateFileWithBytes(filepath As String, fileoutput As String, length As Integer)
            Dim buffer As Byte() = New Byte(length - 1) {}
            Using fs as New FileStream(path:=filepath, mode:=FileMode.Open, access:=FileAccess.Read)
                fs.Seek(0, SeekOrigin.Begin)
                fs.Read(buffer, 0, buffer.Length)
            End Using
            File.WriteAllBytes(fileoutput, buffer)
        End Sub
        Public Shared Sub OffsetReplace(ByVal filepath As String, ByVal fileOutput As String, ByVal offset As Integer, Byval valueData As String)
                Dim value As Integer = Convert.ToInt32(value:= valueData, fromBase:= 16)
            Internal_OffsetReplaceA(filepath, fileOutput, offset, value)
        End Sub
        Private Shared Sub Internal_OffsetReplaceA(ByVal filepath As String, ByVal fileOutput As String, _
                                                   ByVal offset As Integer, Byval value As Integer)
            Using fs As New FileStream(path:=filepath, mode:=FileMode.Open, access:=FileAccess.Read)
                Using fs2 As New FileStream(path:=fileOutput, mode:=FileMode.Create, access:=FileAccess.Write)
                    fs.CopyTo(fs2)
                    fs2.Position = offset
                    fs2.WriteByte(value:=CByte(value))
                End Using
            End Using
        End Sub
        Public Shared Sub C256Combination(ByVal filepath As String, ByVal dirpath As String, ByVal offset As Integer)
            Dim ext As String = Path.GetExtension(filepath)
            Dim value As String
            Dim output As String
            Parallel.For(0, 255, Sub(i)
                                     value = Convert.ToString(value:=i, toBase:=16).ToUpper
                                     If value.Length = 1 Then value = String.Format("0{0}", value)
                                     output = Path.Combine(dirpath, String.Format(FormatName, offset, value, ext))
                                     Internal_OffsetReplaceA(filepath, output, offset, i)
                                 End Sub)


        End Sub


        Private Shared Function Internal_GenBlockBytes(ByVal value As Byte, ByVal length As Integer) As Byte()
            Return ParallelEnumerable.Repeat(element:=value, count:=length).ToArray()
        End Function
        Private Shared Function Internal_StepGenList(ByVal startIndex As Integer, ByVal endIndex As Integer, ByVal stepSize As Integer) As List(Of Tuple(Of Integer, Integer))
            Dim c As New List(Of Tuple(Of Integer, Integer))
            For i = startIndex to endIndex Step stepSize
                Dim k As Integer = endIndex - i
                If k < stepSize Then
                   ' If k = 0 Then k = 1
                    c.Add(New Tuple(Of Integer, Integer)(item1 := i, item2 := k))
                Else
                    c.Add(New Tuple(Of Integer, Integer)(item1 := i, item2 := 0))
                End If
            Next
            return c
        End Function
    End Class
End NameSpace