在编译的 C 或 C++ 中加密密码代码

发布于 2024-09-05 17:50:12 字数 207 浏览 4 评论 0原文

我知道如何在终端中使用 GCC 和 CC 编译 C 和 C++ 源文件,但是我想知道编译后在这些文件中包含密码是否安全。

例如..我检查用户输入的某个密码,例如123,但似乎已编译的C/C++程序可以被反编译。

无论如何,有没有办法编译 C/C++ 源文件,同时保持源完全隐藏。 如果没有,任何人都可以提供一个加密输入的小示例,然后检查密码,例如:(SHA1,MD5)

I know how to compile C and C++ Source files using GCC and CC in the terminal, however i would like to know if its safe to include passwords in these files, once compiled.

For example.. i check user input for a certain password e.g 123, but it appears compiled C/C++ programs is possible to be decompiled.

Is there anyway to compile a C/C++ source file, while keeping the source completely hidden..
If not, could anyone provide a small example of encrypting the input, then checking against the password e.g: (SHA1, MD5)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一花一树开 2024-09-12 17:50:12

不,您无法安全地将密码包含在源文件中。可执行文件中的字符串是纯文本形式,任何拥有文本编辑器的人都可以轻松查看您的密码。

一个不太安全但会践踏某些人的方法是存储加密的字符串。所以,基本上:

enc = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"

bool check() {
    pass = getPassFromUser();
    encpass = myHashingFunction(pass);
    return pass == encpass;
}

这会阻止一些人,但实际上并没有更安全,对于程序集黑客来说,用另一个具有已知明文值的 sha256 编码字符串替换可执行文件中的“enc”字符串相对简单。

即使您使用单独的身份验证服务器,设置一个伪造的身份验证服务器并欺骗您的程序连接到该伪造的身份验证服务器也不困难。

No you can't securely include password in your source file. Strings in executable file are in plain text, anyone with a text editor can easily look at your password.

A not so secure, but would trample some people, is to store the encrypted string instead. So, basically:

enc = "03ac674216f3e15c761ee1a5e255f067953623c8b388b4459e13f978d7c846f4"

bool check() {
    pass = getPassFromUser();
    encpass = myHashingFunction(pass);
    return pass == encpass;
}

this will deter some people, but isn't really much more secure, it is relatively trivial for assembly hacker to replace the 'enc' string in your executable with another sha256-encoded string with a known cleartext value.

Even if you use a separate authentication server, it is not difficult to setup a bogus authentication server and fool your program connect to this bogus authentication server.

唐婉 2024-09-12 17:50:12

即使您使用 SHA1 生成哈希值,如果您以正常方式(编写一个函数来检查密码)执行此操作,也并不是那么安全,任何有决心或知识渊博的黑客只要有权访问可执行文件就能够绕过它(用已知的哈希值替换您的哈希值,或者只是用返回 true 的调用替换 checkPassword() 调用。

问题是您要防止谁?您的弟弟、黑客、国际间谍、工业间谍活动?

将 SHA1 与代码(或配置文件)中包含的哈希只会防止您的小兄弟(读到不愿意尝试破解您的程序而不是支付共享软件价格的普通计算机用户)。使用纯文本密码或 SHA1 哈希的情况几乎没有什么区别(也许多几个百分点的人不会打扰)

如果你想让你的代码安全,那么你将需要做更多关于安全的书。是一个很好的起点,但唯一真正的方法是参加教授保护技术的安全课程。这是一个非常专业的领域,滚动您自己的版本可能会适得其反,并且不会为您提供真正的保护(使用哈希只是第一步)。

Even if you use SHA1 to generate a hash it is not really all that safe if you do it in a normal way (write a function to check a password) any determined or knowledgable hacker given access to the executable will be able to get around it (replace your hash with a known hash or just replace the checkPassword() call with a call that returns true.

The question is who are you trying to protect against? Your little brother, a hacker, international spies, industrial espionage?

Using SHA1 with the hash just contained within in the code (or a config file) will only protect against you little brother? (read casual computer users that can't be bothered to try and hack your program instead of paying the share ware price). In this case using plain text password or a SHA1 hash makes little difference (maybe a couple of percent more people will not bother).

If you want to make your code safe against anything else then you will need to do a lot more. A book on security is a good starting point but the only real way to do this is to take a security class where protection techniques are taught. This is a very specialized field and rolling your own version is likely to be counter productive and give you no real protection (using a hash is only the first step).

好久不见√ 2024-09-12 17:50:12

不建议在代码中保留任何敏感的静态数据。您可以为此使用配置文件。在那里你可以存放任何你喜欢的东西。

但如果您确实想这样做,请首先记住,可以通过使用调试器进行调查并修改它来轻松更改代码。只有用户无权访问的程序才被视为更安全(例如网站)。

大多数(不同站点的)登录密码不是以明文形式存储在数据库中,而是使用 MD5、SHA1、Blowfish 等算法进行加密。

我建议您使用其中之一 算法 来自 OpenSSL图书馆。

我要做的是使用一些公钥加密算法。这可能需要更长的时间才能破解,因为在我看来,在谈论软件保护时没有什么是 100% 确定的。

It is not recommended to keep any sensitive static data inside code. You can use configuration files for that. There you can store whatever you like.

But if you really want to do that first remember that the code can be easily changed by investigating with a debugger and modifying it. Only programs that user doesn't have access to can be considered safer (web sites for example).

The majority of login passwords (of different sites) are not stored in clear in the database but encrypted with algorithms MD5, SHA1, Blowfish etc.

I'd suggest you use one of these algorithms from OpenSSL library.

What I would do is using some public-key cryptographic algorithm. This will probably take a little longer to be cracked because in my opinion there is nothing 100% sure when talking about software protection.

·深蓝 2024-09-12 17:50:12

如果将它们存储为纯文本,则不安全,您可以转储文件或使用字符串等实用程序在可执行文件中查找文本。

您必须以某种方式对它们进行编码。

It's not safe if you store them as plain text, you can just dump the file or use a utility like strings to find text in the executable.

You will have to encode them in some manner.

凯凯我们等你回来 2024-09-12 17:50:12

以下是使用 OpenSSL 的代码示例,可能会对您有所帮助。

#include <openssl/evp.h>

bool SHA256Hash(const char* buf, size_t buflen, char* res, size_t reslen)
{
    if (reslen >= 32)
    {
        EVP_MD_CTX mdctx;

        EVP_MD_CTX_init(&mdctx);

        EVP_DigestInit_ex(&mdctx, EVP_sha256(), NULL);
        EVP_DigestUpdate(&mdctx, buf, buflen);
        EVP_DigestFinal_ex(&mdctx, res, &len);

        EVP_MD_CTX_cleanup(&mdctx);

        return (len == 32);
    }

    return false;
}

我从 systools 库中获取了这个示例,并且必须对其进行调整。所以我不确定它是否可以在不进行修改的情况下编译。不过,它应该对你有帮助。

请注意,为了确定在二进制文件中存储某个密码的哈希值是否安全,我们必须知道您想要它的用途。

如果您希望它禁止程序的某些功能,除非给出一些特殊的密码,那么它是无用的:攻击者可能会删除整个密码检查代码,而不是尝试猜测或反转存储的密码。

Here is a code sample that might help you, using OpenSSL.

#include <openssl/evp.h>

bool SHA256Hash(const char* buf, size_t buflen, char* res, size_t reslen)
{
    if (reslen >= 32)
    {
        EVP_MD_CTX mdctx;

        EVP_MD_CTX_init(&mdctx);

        EVP_DigestInit_ex(&mdctx, EVP_sha256(), NULL);
        EVP_DigestUpdate(&mdctx, buf, buflen);
        EVP_DigestFinal_ex(&mdctx, res, &len);

        EVP_MD_CTX_cleanup(&mdctx);

        return (len == 32);
    }

    return false;
}

I took this sample from the systools library and had to adapt it. So i'm not sure it compiles without modifications. However, it should help you.

Please note that, to determine if storing a hash value of some password in your binary is safe, we must know what you want it for.

If you expect it to forbid some functionalities of your program unless some special password is given, then it is useless: an attacker is likely to remove the whole password-check code instead of trying to guess or reverse the stored password.

孤千羽 2024-09-12 17:50:12

尝试找出散列函数和加密方法来保护您的密码及其存储。

Try finding out Hashing Functions and Ciphering Methods for securing your passwords and their storage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文