使用C ++更改任何Linux用户密码 [英] change any linux user password using c++

查看:44
本文介绍了使用C ++更改任何Linux用户密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C ++更改任何Linux用户密码,任何解决方案都很棒.

我需要手动进行操作,这意味着打开文件(不能使用system()):

  • /etc/passwd
  • /etc/shadow
  • /etc/group

似乎我也需要锁定这些文件.有用于打开.txt文件和读取内容的C代码示例,但无法读取大内容,这也是问题.

  #include< stdio.h>#include< unistd.h>#include #include< sys/file.h>#include< stdlib.h>#include< string>const char* path = "/tmp/log.txt";无效的read_file(){int fd;char buf [1000];我fd = open(路径,O_RDONLY);flock(fd,LOCK_SH);for(i = 0; i <255; i ++){read(fd,& buf [i],1);usleep(10 * 10);//10毫秒}flock(fd,LOCK_UN);关闭(FD);printf(阅读器:%#x:%s \ n",getpid(),buf);usleep(10 * 10);//10毫秒}无效的reader(){read_file();退出(0);}int main(){setlinebuf(stdout);读者();返回0;} 

在理想的解决方案中,它必须像这样工作:

  1. 程序要求输入用户名.
  2. 程序要求输入新密码.
  3. 打开必要的文件,更改密码并保存文件,但不要破坏它.

有人可以解释我需要使用哪个文件来更改密码,以及如何正确打开它以免损坏系统吗?如何准确地编辑密码所在的那一行?

此刻,我正在使用Ubuntu 16.04.

编辑

我在评论中看到普通用户无法访问这些文件.一种意见是授予每个用户更改任何用户密码的权限.那么如果一个用户有权编辑这些文件并更改任何用户的密码呢?这样可以安全地打开和编辑系统文件 shadow ,保存并关闭它.我需要以某种方式锁定它或如何访问它?

那里 shadow 文件结构,因为所有人都可以看到密码有特殊的哈希或密码,因此,如果正确更改密码并保存文件,密码必须更改.

我的使用用户名获取用户数据的代码:

  #include< stdio.h>#include< pwd.h>#include< iostream>使用命名空间std;int main(){struct passwd * p_entry1,* p_entry2;char *用户名;cout<<"插入用户名:";cin>>用户名;/*通过输入的用户名查找用户数据*/p_entry1 = getpwnam(用户名);printf(用户名=%s =%s"用户ID =%d组ID =%d"实名=%s目录=%s"主外壳=%s",p_entry1-> pw_name,p_entry1-> pw_passwd,p_entry1-> pw_uid,p_entry1-> pw_gid,p_entry1-> pw_gecos,p_entry1-> pw_dir,p_entry1-> pw_shell);} 

解决方案

所有方法都可以通过这种方式->使c ++代码更改密码,我这样做是

  • 密码必须加密,因此我使用代码在 $ 6 $:SHA-512 哈希中进行加密.
  • 必须打开/etc/shadow 文件,读取该文件,然后按用户名查找用户并更改密码.那里您可以阅读有关 shadow 的信息文件结构.

要使用fstream打开文件,要查找和更改行,我使用了 std :: getline std :: vector .

要运行.cpp文件,需要在终端中成为root用户,然后运行 g ++ fileName.cpp -o excqFileName -lcrypt ./excqFileName 命令.那是给我的.

所有人,我都做了我的任务,这是可能的.不会共享代码,但我已经进行了足够的解释,以了解其工作原理.

I want to change any Linux user password using C++, any solution would be great.

I need to do it manually, that mean by opening files (Can't use system()):

  • /etc/passwd
  • /etc/shadow
  • /etc/group

Also seems I need to lock these files. Have c code sample for opening .txt file and reading content, but can't read large content, that is problem too.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/file.h>
#include <stdlib.h>
#include <string>

const char* path = "/tmp/log.txt";

void read_file()
{
    int fd;
    char buf[1000];
    int i;

    fd = open(path, O_RDONLY);
    flock(fd, LOCK_SH);

    for(i=0; i < 255 ; i++) {
        read(fd, &buf[i], 1);
        usleep(10 * 10);  // 10 ms
       }

    flock(fd, LOCK_UN);
    close(fd);

    printf("reader: %#x: %s\n", getpid(), buf);
    usleep(10 * 10);  // 10 ms
}

void reader()
{
    read_file();
    exit(0);
}

int main()
{
    setlinebuf(stdout);
    reader();
    return 0;
}

In ideal solution it must work like that:

  1. Program ask to enter username.
  2. Program ask to enter new password.
  3. Open necessary file, change password and save file, but not corrupt it.

Can someone explain with which file I need to work to change password and how to open it properly to not hurt the system? How to edit exactly that line where is password?

At the moment i'm working on Ubuntu 16.04.

EDIT

I saw in comments that normal user cant access these files. And one comment was to give permissions to every user change any user password. So what about if one user have permissions to edit these files and change password to any user. So is it way to safe open and edit system file shadow, save and close it. I need to lock it somehow or how I can access it?

There is shadow file structure, as all can see there is special hash or crypt for password, so if change it properly and save file, password must change.

My code to get user data using username:

#include <stdio.h>
#include <pwd.h>
#include <iostream>


using namespace std;

int main()
{
    struct passwd *p_entry1, *p_entry2;
    char* username;



    cout<<"Insert username: ";
    cin>>username;
    /* Find user data by entered username */
    p_entry1 = getpwnam(username);

    printf("username = %s = %s"
                   " userid = %d group id = %d"
                   " real name = %s directory = %s"
                   " primary shell = %s",
    p_entry1->pw_name, p_entry1->pw_passwd,
            p_entry1->pw_uid, p_entry1->pw_gid,
            p_entry1->pw_gecos, p_entry1->pw_dir,
            p_entry1->pw_shell);

}

解决方案

All is possible in this way -> to make c++ code which change password I done this:

  • Password must be encrypted, so I used code to make it in $6$:SHA-512 hash.
  • Must open /etc/shadow file, read it, find user by username and change password. There you can read about shadow file structure.

To open file used fstream, to find and change line I used std::getline and std::vector.

To run .cpp file, need to be root in terminal, run g++ fileName.cpp -o excqFileName -lcrypt and ./excqFileName commands. Thats it's for me.

That all folks, I did my task and it's possible. Will not share code, but I explained enough to understand how it works.

这篇关于使用C ++更改任何Linux用户密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆