Protegendo dados confidenciais de hackers ou processos, depra -se, protegendo as regiões de memória do acesso e garantindo que os dados sejam limpos quando não é mais necessário.

É crucial proteger informações confidenciais, como chaves de criptografia, senhas e outros dados confidenciais, do acesso não autorizado. Sem proteção adequada à memória, mesmo os dados sensíveis armazenados temporariamente na memória podem ser vulneráveis a ataques como despejos de memória ou injeção de processo. Esta unidade ajuda a bloquear e proteger a memória, garantindo que dados confidenciais sejam protegidos e apagados com segurança quando não são mais necessários.
ProtectedMemory para o seu projeto Delphi.ProtectMemory , UnProtectMemory e ReleaseProtectedMemory para proteger sua memória.ReleaseAllProtectedMemory .TProtectedStreamTProtectedStream , herdando da TMemoryStream .VirtualAlloc para alocação de memória e VirtualProtect para proteger e desprotectar a memória.IsProtected permite alternar entre estados protegidos (sem acesso) e desprotegidos (leia/gravação). uses
ProtectedMemory;
var
Data: array [ 0 .. 255 ] of Byte;
DataPtr: Pointer;
begin
Data[ 0 ] := 99 ;
Data[ 1 ] := 11 ;
Data[ 2 ] := 22 ;
Data[ 3 ] := 33 ;
Data[ 4 ] := 44 ;
Data[ 5 ] := 55 ;
DataPtr := @Data[ 0 ];
// Protect the memory (prevents access to the memory region)
ProtectMemory(DataPtr, SizeOf(Data));
// Accessing the protected memory here will return zeros.
// Unprotect the memory before accessing it
UnProtectMemory(DataPtr);
// Optionally release the memory and clear its content
ReleaseProtectedMemory(DataPtr);
end ; uses
ProtectedMemory;
var
SensitiveStr: string;
NonSensitiveStr: string;
DataPtr: Pointer;
begin
SensitiveStr := ' Sensitive Data ' ;
NonSensitiveStr := ' Not Sensitive Data ' ;
// Get a pointer to SensitiveStr's memory
DataPtr := Pointer(SensitiveStr);
// Protect the memory region containing SensitiveStr
Writeln( ' Protecting memory... ' );
ProtectMemory(DataPtr, Length(SensitiveStr) * SizeOf(Char));
// Accessing SensitiveStr here will return zeros or show undefined behavior
Writeln( ' SensitiveStr after protection: ' , SensitiveStr);
// You can still access NonSensitiveStr, which is unaffected
NonSensitiveStr := ' Updated Non-Sensitive Data ' ;
Writeln( ' NonSensitiveStr: ' , NonSensitiveStr);
// UnProtect Memory it's reutrn it's orginal data
Writeln( ' Releasing memory... ' );
UnProtectMemory(DataPtr);
// SensitiveStr is now restored
Writeln( ' Restored SensitiveStr: ' , SensitiveStr);
end ; uses
ProtectedStream;
var
Stream: TProtectedStream;
Data: AnsiString;
Buffer: array [ 0 .. 255 ] of Byte;
begin
Data := ' Sensitive Data ' ;
Stream := TProtectedStream.Create;
try
Stream.Write(PAnsiChar(Data)^, Length(Data));
Data := ' ' ;
Stream.IsProtected := True; // Protect the memory
// Unprotect to read
Stream.IsProtected := False;
Stream.Read(Buffer, 10 );
finally
Stream.Free;
end ;
end ; ProtectMemory(var DataPtr: Pointer; Size: NativeUInt) : protege a região de memória especificada, definindo -a como PAGE_NOACCESS e travando -a para evitar a paginação. Os dados são copiados para um novo bloco de memória protegido e o ponteiro original é atualizado para apontar para este bloco protegido.
UnProtectMemory(DataPtr: Pointer) : restaura a proteção da memória para PAGE_READWRITE e remove a região da lista de blocos de memória protegidos.
ReleaseProtectedMemory(DataPtr: Pointer) : restaura o acesso à memória, limpa o conteúdo zerando com segurança a memória e a remove da lista protegida.
ReleaseAllProtectedMemory() : libera e limpa todas as regiões de memória protegidas.
Shadi Ajam
SIM! Adoraríamos seu apoio! Por favor, dê um? e compartilhe com outras pessoas.
Compartilhe nas mídias sociais :