08 مايو 2009
العمدي للتقنية

يعتبر أمن المعلومات وحمايتها من أكثر الأمور حساسية التي يجب علينا الاهتمام بها والتعامل معها بجدّية خصوصا عندما يتعلق الأمر بالانترنت. سأشرح في هذا المقال كيفية استخدام برنامج GNU Privacy Guard في صناعة زوج مفاتيح تشفير خاصة بك, طرق التعامل معها وكيفية تشفير/فك تشفير الملفات وتوقيعها باستخدام هذا البرنامج. لذا يعرف برنامج GNU Privacy Guard باسم GnuPG أو GPG اختصاراً وهو موجود بشكل أساسي في أغلب توزيعات نظام جنو/لينوكس ويوجد منه اصدارات خاصة بأنظمة Unix-like مثل FreeBSD, OpenBSD , Mac OS X, OpenSolaris... كما يوجد منه اصدار خاص بنظام Windows. يعتمد GnuPG على مبدأ التشفير باستخدام المفتاح الغير متناظر تطبيقاً لمعيار OpenPGP في تشفير وفك تشفير الملفات وهو بديل حر لبرنامج PGP التجاري. ماذا يعني الكلام السابق؟ كما نعلم
يوجد عدة أنواع من التشفير منها التشفير باستخدام المفتاح المتناظر أي أن المفتاح الذي يشفّر الملف هو نفسه الذي يفك تشفيره لكن بهذه الحالة لنفرض أننا نريد ارسال ملفات هامة لشخص ما عن طريق الانترنت كيف سنتمكن من اعطائه المفتاح الذي شفّرنا به هذه الملفات بطريقة آمنة؟ كيف سنتأكد أن هذا الشخص هو الوحيد القادر على فك تشفير الملفات والاطلاع عليها؟ بهذه الحالة سنختار التشفير باستخدام المفتاح الغير متناظر الذي يتألف من مفتاحين الأول المفتاح العلني Public Key يستخدم في تشفير الملفات التي نريد ارسالها أما المفتاح الثاني هو المفتاح الخاص (أو السري) Private Key ويستخدم في فك تشفير الملفات المشفّر بالمفتاح العلني. بهذه الحالة الملفات المشفّرة باستخدام المفتاح العلني لا يمكن فك تشفيرها الا باستخدام المفتاح الخاص.
ملاحظة: اعتمدت بالشرح على سطر الأوامر بشكل أساسي لأنها الطريقة الأفضل "برأيي" ولأهمية البرنامج بالنسبة لمدراء السيرفرات فغالبا التحكم بالسيرفر يكون عن طريق SSH وليس الواجهة الرسومية, تم التطبيق على نظام GNU/Linux توزيعة Fedora والاصدار 1.4.9 من برنامج GnuPG.
صناعة زوج مفاتيح تشفير:
مافهمناه حتى الآن كل شخص يجب أن يملك مفتاحين الأول علني يتم نشره وارساله للجميع يستخدمه الطرف الآخر في تشفير الملفات والمعلومات التي يريد ارسالها أما الثاني مفتاح خاص يتم الاحتفاظ به في مكان آمن ويستخدم في فك تشفير الملفات المشفرة. لصنع زوج مفاتيح تشفير جديدة نفتح سطر الأوامر وننفذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --gen-key
بعد تنفيذ الأمر السابق سيعمل البرنامج ويطلب منا تحديد نوع المفتاح الذي نريد صنعه, نحدد الخيار الأول (DSA and Elgamal) ثم نضغط Enter ليطلب منا البرنامج تحديد حجم المفتاح, شخصيا أرى الاعدادت الافتراضية مناسبة لذلك سأكتفي بالضغط على Enter مرة أخرى ليظهر لنا كما في الصورة التالية:
يطلب البرنامج تحديد فترة صلاحية المفتاح وهذا طبعا يعود لمدة حاجتنا له وبماذا سنستخدمه, من الممكن عدم تحديد فترة صلاحية وذلك بابقاء القيمة 0 والضغط على زر Enter أو جعل المفتاح صالح ليوم واحد مثلا! نكتب 1 ثم نضغط Enter أما اذا أردنا جعله صالح لأسبوع فقط نكتب 1w , شهر 1m , سنة 1y وهكذا... بالنسبة لي سأبقي الخيار الافتراضي 0 لكي لايتم تحديد فترة صلاحية للمفتاح, سيظهر بعد ذلك رسالة تأكيدية نضغط y لتظهر لنا رسالة تطلب منا ادخال الاسم, الايميل وتعليق عن المفتاح:
أدخل المعلومات بما يناسبك وعندما تنتهي ستظهر رسالة تأكيدية, نضغط حرف O للاكمال بعد ذلك سيطلب منا البرنامج ادخال كلمة سر للمفتاح (تستخدم عند فك التشفير). اكتب كلمة السر التي تريدها وتأكد من تذكرها جيدا فاذا فقدتها لن تتمكن من فك تشفير الملفات وسيصبح مفتاحك عديم الفائدة بعد ذلك سيبدأ البرنامج بصناعة زوج مفاتيح تشفير جديدة وعندما ينتهي سيظهر لك مثل الصورة التالية:
الـ KEY-ID الخاص بهذا المفتاح: 4D3F8269 (هذا المفتاح للتجربة واستخدم بغرض الشرح فقط!)
التعامل مع المفاتيح:
لاستعراض المفاتيح الموجودة في البرنامج نستخدام الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --list-keys
الأمر السابق سيعرض لنا المفاتيح العلنية فقط المضافة لبرنامج GPG لعرض المفاتيح الخاصة فقط ننفذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --list-secret-keys
الأن في حال أردنا تصدير المفتاح العلني الخاص بنا لارساله لشخص أو وضعه في الموقع/المدونة الخاصة بنا مثلا, ننفذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --export -a XXXXXXXX > Public.asc
مع استبدال XXXXXXXX بالـ KEY-ID الخاص بالمفتاح الذي نريد لينشأ ملف باسم Public.asc هو المفتاح العلني, أما اذا أردنا تصدير المفتاح الخاص Private Key لحفظه في مكان آمن كنسخة احتياطية أو نقله لجهاز آخر, ننفذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --export-secret-key -a XXXXXXXX > Private.asc
أيضا مع استبدال XXXXXXXX بالـ KEY-ID الخاص بالمفتاح الذي نريد. اذا أردنا اضافة مفتاح جديد لبرنامج GPG لنفرض المفتاح العلني الخاص بموقع iSecur1ty, في البداية نقوم بتحميل المفتاح:
[Br4v3-H34r7@iSecur1ty ~]$ wget http://www.isecur1ty.org/iS-Public.asc
ثم نقوم باضافة المفتاح الى برنامج GnuPG باستخدام الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --import iS-Public.asc
لحذف مفتاح علني من البرنامج ننفذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --delete-key XXXXXXXX
مع استبدال XXXXXXXX بالـ KEY-ID الخاص بالمفتاح الذي نريد. ولحذف مفتاح سري, نستخدم الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --delete-secret-key XXXXXXXX
من ميزات برنامج GnuPG هي امكانية ارسال المفاتيح العلنية لحفظها على سيرفر خاص بحفظ المفاتيح تمكن الجميع من الوصول اليها, نستطيع القيام بذلك بتنفيذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --send-keys --keyserver keys.gnupg.net XXXXXXXX
مع اسبدال XXXXXXXX بالـ KEY-ID الخاص بالمفتاح الذي نريد ارساله وبهذه الحالة يكفي الحصول على الـ KEY-ID الخاص بمفتاح الشخص الذي نريد ارسال المعلومات اليه بعد التأكد من أنه قام بارساله مسبقا للسيرفر ثم نقوم بتحميله واضافته بشكل تلقائي للبرنامج بتنفيذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --recv-key XXXXXXXX
كذلك نستطيع البحث عن المفتاح العلني الخاص بهذا الشخص في السيرفر باستخدام الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --search-keys "USER/EMAIL"
مع استبدال USER/EMAIL باسم أو ايميل هذا الشخص, على سبيل المثال:
[Br4v3-H34r7@iSecur1ty ~]$ gpg --search-keys Br4v3-H34r7
لاضافة المفتاح, نكتب الرقم الخاص به وبهذه الحالة 1 ثم Enter أو نضغط N للانتقال للصفحة التالية في حال ظهر عدد كبير من المفاتيح أو Q للخروج.
تشفير/فك تشفير الملفات:
بعد صناعة زوج مفاتيح تشفير جديدة ومعرفة كيفية التعامل معها علينا معرفة طريقة تشفير/فك تشفير الملفات باستخدام برنامج GnuPG. لنفرض مثلا أننا نريد تشفير ملف passwords.txt وارساله لشخص ما, كل ماعلينا هو اضافة مفتاحه العلني للبرنامج وتنفيذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg -r XXXXXXXX -e passwords.txt
مع استبدال XXXXXXXX بالـ KEY-ID الخاص بالمفتاح العلني للشخص الذي نريد ارسال الملف المشفّر اليه لينتج في النهاية ملف جديد مشفّر باسم passwords.txt.gpg لا يمكن فك تشفيره الا باستخدم المفتاح السري الخاص بالشخص الذي أرسلنا الملف اليه مع العلم أننا نستطيع تكرار -r عدة مرات اذا أردنا ارسال الملف لأكثر من شخص وجعل الجميع قادرين على فك تشفير الملف بنفس الوقت:
[Br4v3-H34r7@iSecur1ty ~]$ gpg -r XXXXXXXX -r XXXXXXXX -e passwords.txt
في حال لم نحدد KEY-ID سيظهر البرنامج رسالة تطلب منك ادخال الـ KEY-ID أو اسم/ايميل الشخص الذي نريد ارسال الملف اليه, كل ماعلينا هو ادخال الـ KEY-ID الذي نريد والضغط على Enter واذا أردنا ارسال الملف لأكثر من شخص ندخل الـ KEY-ID الخاص بجميع الأشخاص واحدا تلو الآخر وعندما تنتهي نضغط زر Enter مرتين ليتم تشفير الملف:
لو لاحظنا الناتج passwords.txt.gpg سيكون ملف مشفّر بصيغة binary غير مقروء ماذا لو احتجنا لأن يكون الملف بصيغة ASCII عبارة عن نص يمكن ارساله عن طريق الايميل مثلا؟ ببساطة نضيف الخيار -a للأمر السابق لينتج ملف باسم passwords.txt.asc:
[Br4v3-H34r7@iSecur1ty ~]$ gpg -a -r XXXXXXXX -e passwords.txt
الآن نستطيع قراءة الملف بشكل عادي جدا وارساله كنص عن طريق الايميل:
[Br4v3-H34r7@iSecur1ty ~]$ cat passwords.txt.asc
أما اذا أردنا فك تشفير ملف مشفّر تم ارساله الينا نستطيع ببساطة تنفيذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg -d passwords.txt.gpg
بعد التأكد من وجود المفتاح السري الخاص بنا في برنامج GnuPG, الأمر السابق سيظهر الناتج على شاشة الـ Terminal لذلك اذا أردنا فك تشفير الملف وارسال الناتج الى ملف جديد نستخدم الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg -d passwords.txt.gpg > passwords.txt
التواقيع الرقمية:
لنفرض مثلا أننا سنرسل ملف مشفّر لشخص ما.. في البداية سنقوم بالحصول على المفتاح العلني الخاص بهذا الشخص, اضافته للبرنامج, تشفير الملف الذي نريد ارساله ثم ارسال الملف اليه. السؤال الذي يطرح نفسه كيف سيتأكد هذا الشخص أن الملف الذي استلمه هو نفسه الملف الذي قمنا نحن بارسله؟ هنا يأتي دور التواقيع الرقمية حيث قبل أن نرسل الملف سنقوم بتوقيعه باستخدام المفتاح السري الخاص بنا. وبهذه الحالة عندما يقوم الشخص بفك تشفير الملف ستظهر رسالة تؤكد أننا نحن من أرسلنا الملف وليس شخص آخر, لنقوم بذلك كل ماعلينا هو تنفيذ الأمر:
[Br4v3-H34r7@iSecur1ty ~]$ gpg -s -u YYYYYYYY -r XXXXXXXX -e passwords.txt
مع استبدال YYYYYYYY بالـ KEY-ID الخاص بالمفتاح السري الذي سنستخدمه في توقيع الملف و XXXXXXXX بالـ KEY-ID الخاص بالمفتاح العلني للشخص الذي نريد ارسال الملف المشفّر اليه. الآن عندما يقوم هذا الشخص بفك تشفير الملف passwords.txt.gpg ستظهر له رسالة تخبره من الشخص الذي قام بتشفير هذا الملف.
[Br4v3-H34r7@iSecur1ty ~]$ gpg -d passwords.txt.pgp
الواجهات الرسومية:
اذا أردنا استخدام البرنامج لسطح مكتب قد يكون استخدام الواجهة الرسومية أسهل علينا, يوجد للبرنامج العديد من الواجهات الرسومية يمكن استخدامها مع البرنامج وهي متوفرة لأكثر من نظام تشغيل وأكثر من سطح مكتب مثل برنامج Gpg4win لنظام ويندوز, Kgpg برنامج خاص بسطح مكتب KDE و Seahorse الواجهة الرسومية الخاصة بسطح مكتب GNOME:
يمكنكم الاطلاع على
هذه الصفحة لمزيد من المعلومات عن الواجهات الرسومية المتوفرة كما يوجد اضافات للعديد من البرامج تسهّل علينا التعامل مع GunPG مثل اضافة
Enigmail لبرنامج Thunderbird الخاص بادرة الايميل.
رأينا أهمية هذا الموضوع فقما بنقله ، مع الشكر
Public Key Encryption:
Classic methods for encryption ('symmetrical cyphers') only use one key for both encryption and decryption. The sender encrypts the message with this key. To be able decrypt this the receiver needs to have this very same key. This key must have been given to the receiver in a way, that others won't have had the opportunity to obtain this key. If somebody else does have the key, this method of encryption is useless. The use of so-called Public Key Cryptography can solve this problem. Public Key is a concept where two keys are involved. One key is a Public Key (used for the encryption) that can be spread through all sorts of media and may be obtained by anyone. The other key is the decrypting Private Key. This key is secret and cannot be spread. This key is only available to the owner. When the system is well implemented the secret key cannot be derived from the public key. Now the sender will crypt the message with the public key belonging to the receiver. Then decryption will be done with the secret key of the receiver. Crucial in this concept is that the secret (Private) key remains a secret and should not be given away or become available to anyone else but the owner of this key. YOU SHOULD NOT SEND THIS KEY OVER THE INTERNET (at least not if you want maximum security...). Also it is very unwise to use GnuPG over telnet (one might consider never to use telnet based on the high security risks).
Basic procedures (Windows GUI interface WinPT) For Windows-addicts not working on the command line, I recommend using the mentioned WinPT graphical user interface. See below for compact instructions for the basic steps using WinPT.
Sending a file to an other party
For encryption to send to an other party, you will (once) need to have the public key of the receiving party, and to have this key entered into your key ring (see separate section further down this page).
Right-click the WinPT WinPT icon icon on the task bar, select File Manager. A popup window will appear in which you can drag your file(s) to encrypt, or use the File/Open menu to select files. Use File/Encrypt to encrypt. Select the receiver from the popup menu. The encrypted files will be placed in the same directory as the original files, having a .pgp extension.
Receiving files from other parties
To receive an encrypted file from an external party, this party first needs to have your public key (see separate section later on, on how to send this to him/her). The sending party uses this key to encrypt the data and will send the resulting file to you.
Procedure is basically the same as for encryption, however now select decrypt instead of encrypt. Enter your password into the popup box.
Importing public keys received from others
A public key is typically send to you via email in preparation to you having send a file. For example, have a look at my public key file (can also be used for testing).
From a mail: copy the key including the ----BEGIN and ----END lines (^C or menu edit/copy will do so), right-click the WinPT WinPT icon icon on the task bar and select 'Key Manager', then paste (menu: edit/paste) the key into the key manager.
From file: import from a text file using the WinPT Key Manager, menu key/import.
Generating your own key
This is a one-time only process. I still have to describe this in more detail... Using WinPT:
Right-click the WinPT WinPT icon icon on the task bar, select Key Manager. Select menu key/generate... You get a pop-up box you need to fill in. Choose key length at 1024 bits. You need to enter (and remember!) a passphrase (password), will be requested when decrypting received files.
You also need to export your public key for distribution, this can be done right-clicking the WinPT WinPT icon icon in the task bar, selecting the key manager, selecting your key and exporting it using the menu key/export. This will generate an ASCII (text) file with the specified name, which you can e.g. mail to persons needing to send you encrypted mail. This key file is a normal text file, no special handling required.
Generating a key revokation certificate does not seem to work under WinPT currently. Use the command line 'gpg --gen-revoke <user_id>' and answer the questions asked.. Basic procedures (Command line interface)
On Windows and Linux/UNIX, GPG can be accessed via the command line. In order to ease working with the program (there are quite some options and possibilities), I have some simple pre-wrapped batch files collected in a zip file (you can use the commands from these files to make your own UNIX/linux scripts). The settings are such that the system is compatible with the widely used PGP tools. Feedback is welcomed, as I am only a starter with GPG. Note: text in italics has to be replaced by your input. A more extensive explanation (Windows-based) can be found on 'A Practical Introduction to GNU Privacy Guard in Windows'.
Sending a file to an other party
For encryption to send to an other party, you will (once) need to have the public key of the receiving party, and to have this key entered into your key ring (see separate section further down this page).
The batch file gpg_enc will encrypt a specified file using the public key of the receiver, generating a file with the same name but with the .asc extension (is in ASCII format).
gpg_enc <filename> -r <recipientname>
The script:
- will overwrite an existing .asc file
- will ask whether you trust the key (if not set at trusted after reception)
- will only encrypt but not sign
- settings are PGP-compliant (version 5.x and higher)
- Basically does, next to some checking on arguments: gpg --compress-algo 1 --cipher-algo cast5 --yes -a -r <recipientname> -e <filename>
Receiving files from other parties
To receive an encrypted file from an external party, this party first needs to have your public key (see separate section later on, on how to send this to him/her). The sending party uses this key to encrypt the data and will send the resulting file to you.
In order to decrypt, you can use the gpg_dec batch file (basically doing: gpg -o <decrypted_file> -d <encrypted_file>). Note that this file will ask you to enter your password/phrase for your key.
gpg_dec <encrypted_file> <decrypted_file>
Importing public keys received from others
A public key is typically send to you via email in preparation to you having send a file. For example, have a look at my public key file (can also be used for testing).
Save the key in an ascii text file, and import it into your public key collection (key ring) using the gpg --import batch command.
gpg --import <file_with_key>
How to be sure a key can be trusted: calculate the 'fingerprint' and check the fingerprint by phone:
gpg --fingerprint <recipientname>
If you are sure the key is trustworthy, you can indicate this in the key ring database to prevent questions during an encryption process. Note: the below lines indicate a communication with an interactive gpg run giving the user input only:
gpg --editkey <recipientname>
trust
4
quit
gpg --list-keys will list all your available imported keys.
Generating your own key
This is a one-time only process. I stuill have to describe this in more detail... Start the interactive program using
gpg --gen-key
Choose key length 1024 bits. You need to enter (and remember!) a passphrase (password), will be requested when decrypting received files. You also need to export your public key for distribution:
gpg --export -a <yourname> > <yourkey.asc>
This will generate an ASCII (text) file <yourkey.asc>, which you can e.g. mail to persons needing to send you encrypted mail. This key file is a normal text file, no special handling required.
Cryptography rules and laws
Here you can find a survey of existing and proposed laws and regulations on cryptography - systems used for protecting information against unauthorized access. Governments have long restricted export of cryptography for fear that their intelligence activities are hampered by the crypto use of foreign states and scoundrels. Since the rise of crypto use over the past decades, governments increasingly worry about criminals using cryptography to thwart law enforcement. Thus, many countries have passed laws or are considering laws to maintain law-enforcement and national-security capabilities through regulation of cryptography.
Add comment