#โปรแกรมเมอร์ เก็บ Password แบบไหน? ถึงจะปลอดภัย
ในทางโปรแกรมมิ่งการเก็บ password ลงฐานข้อมูล (Database) ไม่ได้เก็บกันตรงโต้งๆ ไม่งั้นใครมาเห็นก็อ่านได้หมด ซวยกันพอดี
:
วิธีเก็บ password ที่ปลอดภัย
จะนำมาผ่าน Hash function เสียก่อน เช่น
hash("1234") ได้คำตอบออกมาเป็น
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9
:
หน้าที่ hash function จะแปลงพาสเวิร์ด "1234"
เป็นข้อความลับอะไรซักอย่างที่อ่านไม่ออก
ทั้งนี้ขนาดข้อความที่ได้จาก hash function จะคงที่ (fixed size)
:
สำหรับค่าที่ได้จาก Hash function มีหลายชื่อให้เรียกขาน เช่น
hash values, hash codes, digests
แต่ผมจะเรียกสั้นๆ ว่า "ค่า hash" แล้วกัน
:
ส่วนฟังก์ชั่นที่ใช้เป็น Hash function ในโลกนี้มีหลายตัว เช่น
MD5, SHA256, SHA512, RipeMD, WHIRLPOOL เป็นต้น
:
+++++
👉 ตัดกลับมาตอนนี้เราเก็บ password ในฐานข้อมูลเป็นค่า hash แล้วเวลายูสเซอร์ล็อกอินกรอก user name ป้อน password เข้ามาในระบบ
.
ก็จะมีสเตปการตรวจสอบ password ประมาณเนี่ย
.
1) ระบบจะเอา password มาเข้า hash funcion ได้เป็นค่า hash
2) เอาค่า hash ในข้อ 1 ไปเทียบดูในฐานข้อมูล (ของยูสเซอร์นั้น)
3) ถ้าค่าตรงกันแสดงว่ายูสเซอร์ป้อน password ได้ถูกต้อง แสดงว่าล็อกอินสำเร็จ
:
👉 เหตุผลที่ hash function มัน ok เพราะอาศัยคุณสมบัติดังนี้
1) hash function มันทำงานทางเดียว (one-way)
หมายถึงเราไม่สามารถนำค่า hash มาย้อนหาข้อความต้นฉบับได้เลย
.
ในกรณีนี้ต่อให้ hacker เห็นค่า hash เขาจะไม่สามารถถอดกลับ
มาเป็น "1234" ได้เลย
.
ด้วยเหตุนี้ค่า hash บางทีเขาจึงเรียกว่า "message digest" หมายถึง "ข้อความที่ย่อยสลาย" ...จนไม่รู้ต้นฉบับหน้าตาเป็นแบบไหนแล้ว
:
2) ถ้าข้อความต้นฉบับหน้าตาเดียวกันเป๊ะทุกกะเบียดนิ้ว
เวลาผ่าน hash function จะได้ค่า hash เหมือนเดิม
พอเปลี่ยนข้อความต้นฉบับนิดหนึ่ง
แม่เจ้า ....ค่า hash เปลี่ยนไปราวฟ้ากับเหว ต่างกันมาก
.
จึงเป็นไปไม่ได้ที่เราจะเก็บ password ต่างกัน
แล้วได้ค่า hash เดียวกัน ...เป็นไปไม่ด้ายยยย
(ไม่มีการชนกันหรือ crash)
:
+++++
👉 เพราะข้อดีของ hash function ที่ยกมา
เวลาเก็บ password ลงฐานข้อมูล จึงควรเปลี่ยนไปใช้ค่า hash แทน
.
รับรองได้ว่าต่อให้ hacker เจาะระบบเข้ามาได้ (กรณีเลวร้ายสุดๆ แหละ)
...แล้วอ่าน password ที่ถูกเข้ารหัส ก็จะอ่านไม่รู้เรื่อง
...ต่อให้พยายามถอดกลับมาเป็นข้อความต้นฉบับ ก็ทำไม่ได้นะจ๊ะ
:
ฟังเหมือนปลอดภัยนะ ถ้าเก็บรักษา password ด้วยวิธี hash function แต่ทว่า hacker ก็ยังสามารถใช้วิธีเดาสุ่มหา password ได้อยู่ดี ...ไม่ยากด้วย ขอบอกเลย
:
👉 ยกตัวอย่างง่ายๆ วิธี hack พาสเวิร์ดเบสิกสุดๆ
- ให้คิดว่าตอนนี้ hacker เจาะระบบเข้าไปอ่าน password ในฐานข้อมูลได้แล้ว
- จากนั้น hacker จะมองหาค่า hash (ของ password) ในฐานข้อมูลที่ซ้ำๆ กันอยู่
- นั่นหมายถึงเจอยูสเซอร์ใช้ password ซ้ำกัน จึงเจอค่า hash ซ้ำกันนั่นเอง
- แล้วการที่ยูสเซอร์ใช้ซ้ำ แสดงว่ามันเป็น password ง่ายนะซิ เช่น
123456, 1111, Baseball, Qwerty, password
.
ดังนั้น hacker ก็แค่ค้นหาในตาราง
ตารางที่ว่าจะเก็บ password พร้อมค่า hash
(เก็บพวก password ที่คนใช้กันเยอะ)
ซึ่ง hacker ก็จะค้นหาหาในตารางดังกล่าว
แบบไล่สุ่มไปเรื่อยๆ เดี่ยวก็เจอไม่ยาก
:
++++
👉 ก็เพราะเหตุนี้จึงต้องหาวิธีแก้ทาง hacker
ให้เดาสุ่มหา password มันทวีความยุ่งยากไปอีก
(จุดประสงค์ป้องเทคนิคพวกเดาสุ่ม เช่น
dictionary attacks, Brute Force Attacks, Lookup Tables,
Reverse Lookup Tables, Rainbow Tables)
:
👉 สำหรับวิธีการป้องกัน ก็จะทำประมาณเนี่ย
ก่อนที่จะเก็บ password ลงฐานข้อมูล ระบบจะต้องทำเยี่ยงนี้
1) จะนำ password มากบวกกับค่า salt
2) จากนั้นนำค่าที่ได้จากข้อ 1 มาเข้า hash function
แล้วเก็บค่า hash ที่ได้ลงฐานข้อมูล
.
ขออธิบายข้อ 2 เพิ่มเติม
จากเดิมเราเรียกใช้ hash function เช่น
hash("1234")
แต่เราจะเปลี่ยนมาเรียก
hash("1234" + "QxLUF1bgIAdeQX")
hash("1234" + "bv5PehSMfV11Cd")
hash("1234" + "YYLmfY6IehjZMQ")
.
ซึ่ง "QxLUF1bgIAdeQX", "bv5PehSMfV11Cd", "YYLmfY6IehjZMQ" ที่ยกตัวอย่าง
มันก็คือค่า "salt" (ที่แปลว่า "เกลือ")
เป็นค่า radom ที่แจกให้แต่ละยูสเซอร์ ไม่ซ้ำกันเลย
เราจะนำมาบวกกับ password ก่อนเข้า hash function
:
เวลาเก็บ password ในฐานข้อมูล
แต่ละยูสเซอร์จะต้องเก็บทั้งค่า hash กับ salt เอาไว้
.
👉 พอเวลายูสเซอร์ล็อกอินใส่ user name / password
1) ระบบก็เอา password มาบวกกับ salt
(แต่ละยูสเซอร์เก็บค่า salt คนละค่า)
2) นำค่าที่ได้จากข้อ 1 มาเข้า hash funcion
3) นำค่าที่ได้จากข้อ 2 ไปเปรียบเทียบกับ ค่า hash ในฐานข้อมูล
4) ถ้าตรงกันแสดงว่ายูสเซอร์ป้อน password ถูกต้อง แสดงว่าล็อกอินสำเร็จ
..
แต่มีข้อแม้ hash function ที่ควรใช้ได้แก่
Argon2, bcrypt, scrypt ($2y$, $5$, $6$), หรือ PBKDF2
มันถึงจะปลอดภัย ทำให้การเดาสุ่มหา password ทำได้ยากขึ้น
.
ส่วนพวก hash function ที่ทำงานได้รวดเร็ว เช่น
MD5, SHA1, SHA256, SHA512, RipeMD, WHIRLPOOL, SHA3, etc.
เนี่ยห้ามใช้นะครับ
หรืออย่าง crypt (เวอร์ชั่น $1$, $2$, $2x$, $3$) ก็ไม่ห้ามใช้นะครับ
:
+++++
👉 ในแง่การเขียนโปรแกรม
เข้าใจว่าแต่ละภาษาโปรแกรมมิ่ง หรือพวกเฟรมเวิร์ค
เขาคงเตรียมไลบรารี่ หรือเครื่องมือ
เอาไว้ให้ใช้ hash function รวมกับค่า salt อยู่แล้ว
เราสามารถเปิดคู่มือ แล้วทำตามได้เลยครับ
:
++++
👉 ย้ำที่อธิบายทั้งหมดนี้
เป็นการป้องกันการเจาะระบบฝั่งแอพ หรือระบบเท่านั้น
hacker ยังสามารถเดาสุ่มป้อน password
ได้โดยตรงที่หน้าแอพ หรือฝั่งล็อกอินหน้าโปรแกรมได้เลย (Brute Force Attacks)
.
ทางที่ดีระบบต้องเช็กว่าถ้ายูสเซอร์กรอก password ผิดติดต่อกันกี่ครั้ง?
ถึงจะระงับการใช้ user name นี้ชั่วคราว หรือจะแบน IP ที่ล็อกอินเข้ามาไปเลยก็ยังได้
.
ยิ่งถ้าเป็นการล็อกอินผ่านเว็บไซต์
ก็ควรให้เว็บเราใช้โปรโตคอล https ขืนไปใช้ http ธรรมดา
โอกาสเจอ hacker ดักจับ user name/ passwod กลางทางมีสูงมาก
.
เว้นแต่เราจะใช้เทคนิค Digest Access Authentication เข้าช่วย
ทำให้การส่ง user name/password ผ่าน http ธรรมดาได้อย่างปลอดภัย (แต่โค้ดดิ่งก็จะยุ่งยากตาม)
:
+++
😁 สรุป
1) เก็บพาสเวิร์ดตรงๆ โดยไม่เข้ารหัส -> hacker ชอบนักแล
2) เก็บพาสเวิร์ดโดยเข้ารหัสในรูป Hash values -> hacker อ่านไม่ออกก็จริง แต่ไม่ยากที่จะเดา password
3) เก็บพาสเวิร์ดโดยเข้ารหัสในรูป Hash values + salt vaues
-> hacker อ่านไม่ออก ต่อให้ไปเดาสุ่ม ก็จะทำได้ยากขึ้น
จุดประสงค์ข้อ 3 นี้เพื่อป้องกันด้วยเทคนิค ..... dictionary attacks, Brute Force Attacks, Lookup Tables, Reverse Lookup Tables, Rainbow Table
.
สุดท้ายขอจบเรื่อง hash funcion กับ password
ให้รอดพ้นจาก hacker ไว้เพียงเท่านั้น
หวังว่าจะเป็นประโยชน์ต่อทุกท่านนะครับ
.
++++++
เขียนโดย โปรแกรมเมอร์ไทย thai programmer
อ้างอิง
https://crackstation.net/hashing-security.htm
https://en.wikipedia.org/wiki/Hash_function
「message digest」的推薦目錄:
- 關於message digest 在 โปรแกรมเมอร์ไทย Thai programmer Facebook 的最佳貼文
- 關於message digest 在 王彪民 PM Wang Facebook 的最佳貼文
- 關於message digest 在 La Dolce Vita in Oz 澳洲微甜人生 Facebook 的最讚貼文
- 關於message digest 在 Message Digest and Digital Signature - YouTube 的評價
- 關於message digest 在 What is the difference between "Message Digest" and ... 的評價
- 關於message digest 在 The MD4/MD5 Message-Digest Algorithms 的評價
- 關於message digest 在 PKCS#7 CMS - Message digest calculation process 的評價
- 關於message digest 在 jdk7u-jdk/src/share/classes/java/security/MessageDigest.java ... 的評價
message digest 在 王彪民 PM Wang Facebook 的最佳貼文
I refuse to answer. The similar numbers have been calling me for a few days now. I know it’s Citibank telemarketers.
My phone rang again this morning. Decided to give whomever was working so hard a break.
As expected, a very nice lady on the other end of the line patiently explained my privilege as a card holder to sign up for an insurance policy AIA designed exclusively.
It took her 7 minutes to finish her script. Detailing out all main selling points, answering potential questions without me asking, laying down card holders option to cancel the policy after. I replied with some kind of sound from time to time so she won’t feel like she’s talking to the air.
When she finally was done, I gave her my answer. She didn’t sound surprise but have to ask for a reason.
I told her she did well. But I honestly couldn’t digest the information via a phone call like this. I want her to be clear about my message and feed it back to the management: it is very mind boggling to expect someone to be able to understand vital decision making information with a single call. I can’t see myself making a rational decision, hence all I can do is say no.
“It is not the way you delivered it, you did well. I just can’t work out everything I’ve been told just now, I don’t think it’s a smart way to sell a policy.” I said that with a smiling face, make sure she didn’t feel rejected.
And suddenly, I hear emotion in her tone. She smiled and thank me, briefly explain that’s her duty to make sure I as a cardholder get the opportunity and understand the benefit.
“You understand your are entitled such benefit as a cardholder, right?” She needed an affirmative answer. I realise that’s her KPI. Gladly answered “yes”, hoping that helps her a little even though she couldn’t close the deal.
I wished her better luck with her next call.
But why? Why would anyone think selling insurance policy via telemarketing is a good idea? Someone who hates telemarketers? It is designed for failure, a tiring and torturous failure.
I wonder what is the success rate. And what kind of customers they get, if any.
message digest 在 La Dolce Vita in Oz 澳洲微甜人生 Facebook 的最讚貼文
燕麥吃多會肥?!😱
我每次這樣說的時候,每一個人臉都瞬間變成這個emoji😱。屁啦! Vita 你騙我,燕麥明明就很健康,那個某某跟某某健身網紅都嗎這樣說!
的確,常常看到健康達人或是網紅們分享吃燕麥的好處,它含有豐富的維生素B群、E及鐵、鋅、鎂等礦物質,尤其當中的膳食纖維,能增加飽足感又有利於排便。
因此不少人認為它的纖維質多,熱量應該比較低,所以『多吃』沒有關係。但其實是錯的!
燕麥本身也有熱量,1碗白米飯與1碗熟燕麥,熱量差不多都是280大卡,如果吃太多,身體無法利用過多的熱量時,也一樣會胖!
試想,你為了減肥不敢吃白米飯,卻在那邊拼命地吃燕麥,而且可能還吃的都是含了麥芽糊精、果糖、高油的加工燕麥!母湯喔🙅🏻♀️
若你正在減脂~
❌燕麥早餐地雷組合:加工燕麥+牛奶+水果+堅果+果乾
我常常收到你們私訊問我:『Vita我的早餐很健康喔,都是吃以上這種組合,怎麼體脂還是麥丁麥動,一點想要往要降的意願都沒有😭。』
當然不會降囉,因為這樣的組合是高糖、高碳水、高脂的地雷啊。💣
燕麥是碳水(高升糖兩大匙約30克= 1/4碗白飯熱量。加工調味燕麥可能更高)
果乾是碳水(高升糖)
水果是碳水(高升糖)
牛奶(乳糖)
堅果(脂肪)一日建議量不超過五顆,不然熱量爆表!我自己是不吃超過3顆,你若抓一把放,我會搖頭說母湯。🤦🏻♀️
💚那到底怎麼健康吃又不會發胖呢?
✔️選擇剛切燕麥(steel cut oats):
它是燕麥中加工程度最少,保持最完整的纖維及營養,升糖指數最低,讓你可以獲取燕麥中最高品質的營養。我通常是用水煮,你也可以用無糖豆漿。
✔️搭配組合:
低脂無糖希臘優格(蛋白質)
乳清蛋白粉(蛋白質)
泡過水的奇亞籽1大匙(含有豐富纖維和蛋白質)
脂肪:無糖純杏仁醬(1茶匙)、或5顆無調味堅果
✔️水果:我會選擇低糖的藍莓、綠色奇異果、覆盆子。水果擁有纖維酵素,適量食用對身體有益,但果糖是脂肪肝的頭號敵人,所以一日量我不會超過掌心大。
✔️肉桂粉:撒上肉桂粉不但會增添風味,讓你有吃甜食的錯覺,它可以增加新成代謝並有降血糖、將血脂的好處。但孕婦忌、及患病者,請依照醫師或營養師指示服用。
💚為什麼減脂期間我會提高⬆️蛋白質的量,減少⬇️糖分(碳水)的攝取。
因為蛋白質跟碳水相比,人體需要燃燒更多的能量,及更多的時間來消化吸收蛋白質,所以,提高蛋白質的量不但會延長你飽足的時間,更會增加你燃脂的機會。
而碳水因為會抓住身上的水分,所以,一旦你的碳水量一降低,你會先排水消水腫。這也就是為什麼減脂初期你不吃澱粉,馬上就可以瘦個幾公斤,但其實,瘦的都只是水分,並非真正的脂肪!
💚吃燕麥吃錯會胖,這裡有兩部影片跟你分享:
從6:50~8:45開始看
https://youtu.be/NQm9jqhoZ0Y
以下這部是我最愛的一位營養師上傳的,她講解的非常清楚
https://youtu.be/7YW-j5YXLzU
💚水果不能吃多: 從11:01開始看
https://youtu.be/M4chH0nEvRs
🖤Oatmeal makes me FAT? !
I often received your message asking me: Vita, I am having oat for breakfast, I thought it supports to be very healthy but how came my fat % still so high? 😭
Indeed, Oats are rich in vitamin B complex, E, and minerals such as iron, zinc, and magnesium. In particular, the dietary fiber in it can increase satiety and defecating. Therefore, many people think that it is high in fibres and low in calories, so "eat more" does not matter.
Might you, Oatmeal isn’t free of calories, so if you eat too much of it, it’s just as likely to cause fat gain as any other food, especially if you load up your bowl with calorie-rich extras such as milk, fruit, nuts & dried fruit.
☝️Here is why your so-called healthy breakfast bowl might cause you a fat gain!
Oats are carbs (30 grams = 1/4 bowl of rice calories. Processed & flavored oats will have higher calories!)
Dried fruit is sugar
Fruit is sugar
Milk (lactose is sugar!)
Nuts (high in nutritions but also high in calories)
💚How to eat oats to avoid FAT GAIN?
✔️Choose steel cut oats:
It is the least processed oats which it remains the most complete fibre and nutrition, and the lowest glycemic index, allowing you to obtain the highest quality nutrition in oats. I usually cook it with water.
✔️Extras:
Protein: Low-fat & low carbs Greek yogurt
Protein: Whey protein powder
Fibre & Protein: 1 tablespoon of soaked chia seeds
Fat: 5 Raw nuts or 100% pure almond butter
✔️Fruit: I prefer low-sugar blueberries, green kiwis, and raspberries. Fruits contain fiber enzymes, and eating them in moderation is good for the body, but fructose is the number one enemy of fatty liver, so the daily amount will not exceed the palm of my hand.
✔️Cinnamon powder: Sprinkle with cinnamon powder will not only add flavor and give you the illusion of eating sweets, it can increase new metabolism, lower blood sugar, and reduce blood fat. However, pregnant women will need to consult with your doctor or dietitian.
💚Why do I increase ⬆️ protein?
Compared with carbohydrate, your body needs to burn more energy and take more time to digest and absorb protein. Therefore, increasing the amount of protein will not only prolong your satiety, but also increase your chances of burning fat.
#燕麥 #燕麥粥 #瘦身減肥 #燕麥早餐 #fatlosstips #減肥餐 #減脂餐 #fatlosstips #fatlossmotivation #oatmealbowl #oatsforbreakfast #oatsmeal
#健康飲食 #健康瘦身 #減肥方法 #減肥菜單 #減脂肪
message digest 在 What is the difference between "Message Digest" and ... 的推薦與評價
Different checksums (examples below) can be chosen depending on the application. message digest; hash algorithm; digital fingerprint ... ... <看更多>
message digest 在 The MD4/MD5 Message-Digest Algorithms 的推薦與評價
Overview¶. The MD4/MD5 Message-Digest Algorithm is a hash-based cryptographic function. It takes a message of arbitrary length as its input and produces a ... ... <看更多>
message digest 在 Message Digest and Digital Signature - YouTube 的推薦與評價
This video lecture is produced by S. Saurabh. He is B.Tech from IIT and MS from USA. Message Digest and Digital Signature To study interview ... ... <看更多>