حماية البيانات الحساسة من المتسللين أو العمليات تصحيح من خلال حماية مناطق الذاكرة من الوصول وضمان مسح البيانات عند عدم الحاجة إليها.

من الأهمية بمكان حماية المعلومات الحساسة ، مثل مفاتيح التشفير وكلمات المرور والبيانات السرية الأخرى ، من الوصول غير المصرح به. بدون حماية مناسبة للذاكرة ، حتى البيانات الحساسة المخزنة مؤقتًا في الذاكرة يمكن أن تكون عرضة للهجمات مثل مقالب الذاكرة أو حقن العملية. تساعد هذه الوحدة على قفل الذاكرة وحمايتها ، مما يضمن حماية البيانات الحساسة ومحوها بشكل آمن عندما لم تعد هناك حاجة إليها.
ProtectedMemory إلى مشروع Delphi الخاص بك.ProtectMemory ، UnProtectMemory ، وإعادة ReleaseProtectedMemory لتأمين ذاكرتك.ReleaseAllProtectedMemory .TProtectedStreamTProtectedStream ، وراثيا من TMemoryStream .VirtualAlloc لتخصيص الذاكرة و VirtualProtect لحماية الذاكرة وعدم إزالتها.IsProtected بالتبديل بين حالات محمية (بدون وصول) وحالات غير محمية (قراءة/كتابة). 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) : يحمي منطقة الذاكرة المحددة عن طريق تعيينها على PAGE_NOACCESS وقفلها لمنع الترحيل. يتم نسخ البيانات إلى كتلة ذاكرة محمية جديدة ، ويتم تحديث المؤشر الأصلي للإشارة إلى هذه الكتلة المحمية.
UnProtectMemory(DataPtr: Pointer) : يعيد حماية الذاكرة إلى PAGE_READWRITE ويزيل المنطقة من قائمة كتل الذاكرة المحمية.
ReleaseProtectedMemory(DataPtr: Pointer) : يعيد الوصول إلى الذاكرة ، ويقوم بمسح المحتوى عن طريق الصدر الآمن للذاكرة ، ويزيلها من القائمة المحمية.
ReleaseAllProtectedMemory() : الإصدارات ويمسح جميع مناطق الذاكرة المحمية.
شادي أجام
نعم! كنا نحب دعمك! من فضلك أعطها؟ وشاركها مع الآخرين.
مشاركة على وسائل التواصل الاجتماعي :