Compare commits
6 Commits
master
...
dontwantto
Author | SHA1 | Date | |
---|---|---|---|
|
ef66510e59 | ||
|
87082d5d5e | ||
|
61382316d9 | ||
|
e88465c2c9 | ||
|
51880f5428 | ||
|
383abfc185 |
@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
/* gcc -std=gnu99 -Wall -g -o mylogin login_linux.c -lcrypt */
|
/* gcc -std=gnu99 -Wall -g -o mylogin login_linux.c -lcrypt */
|
||||||
|
|
||||||
|
/* Note, makepass.c is first used to create hashed password
|
||||||
|
then, info is manually saved in file passdb */
|
||||||
|
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -12,27 +16,22 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <crypt.h>
|
#include <crypt.h>
|
||||||
#include "pwent.h" /* Step 2 */
|
#include "pwent.h" /* Step 2 */
|
||||||
|
#include <termios.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#define TRUE 1
|
#define TRUE 1
|
||||||
#define FALSE 0
|
#define FALSE 0
|
||||||
#define LENGTH 16
|
#define LENGTH 16
|
||||||
|
|
||||||
void sighandler() {
|
void sighandler() {
|
||||||
//sigset_t mask;
|
|
||||||
//sigfillset(&mask);
|
|
||||||
//sigprocmask(SIG_SETMASK, &mask, NULL);
|
|
||||||
|
|
||||||
signal(SIGINT, SIG_IGN); /* This will catch ctrl+c and ignore it*/
|
signal(SIGINT, SIG_IGN); /* This will catch ctrl+c and ignore it*/
|
||||||
//signal(SIGTSTP, SIG_IGN); /* This will catch ctrl+z and ignore it*/
|
signal(SIGTSTP, SIG_IGN); /* This will catch ctrl+z and ignore it*/
|
||||||
/* add signalhandling routines here */
|
|
||||||
/* see 'man 2 signal' */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
//struct passwd *passwddata /* Step 1 */
|
mypwent *passwddata;
|
||||||
mypwent *passwddata; /* Step 2 */
|
|
||||||
/* see pwent.h */
|
|
||||||
|
|
||||||
char important1[LENGTH] = "**IMPORTANT 1**";
|
char important1[LENGTH] = "**IMPORTANT 1**";
|
||||||
|
|
||||||
@ -40,9 +39,7 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
char important2[LENGTH] = "**IMPORTANT 2**";
|
char important2[LENGTH] = "**IMPORTANT 2**";
|
||||||
|
|
||||||
//char *c_pass; //you might want to use this variable later...
|
char user_pass[LENGTH];
|
||||||
char prompt[] = "password: ";
|
|
||||||
char *user_pass;
|
|
||||||
|
|
||||||
sighandler();
|
sighandler();
|
||||||
|
|
||||||
@ -53,68 +50,132 @@ int main(int argc, char *argv[]) {
|
|||||||
printf("Value of variable 'important2' before input of login name: %s\n",
|
printf("Value of variable 'important2' before input of login name: %s\n",
|
||||||
important2);
|
important2);
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
printf("login: ");
|
printf("login: ");
|
||||||
fflush(NULL); /* Flush all output buffers */
|
fflush(NULL); /* Flush all output buffers */
|
||||||
__fpurge(stdin); /* Purge any data in stdin buffer */
|
__fpurge(stdin); /* Purge any data in stdin buffer */
|
||||||
|
|
||||||
|
bool okusername = true;
|
||||||
|
/* using fgets instead of gets to avoid buffer overflow attacks*/
|
||||||
if (fgets(user,sizeof(user),stdin) == NULL ) {
|
if (fgets(user,sizeof(user),stdin) == NULL ) {
|
||||||
clearerr(stdin); //if EOF only, clear error
|
clearerr(stdin);
|
||||||
continue; ///* overflow attacks. */
|
okusername = false;
|
||||||
}
|
}
|
||||||
user[strlen(user)-1] = '\0'; //replacing \n with \0
|
|
||||||
|
|
||||||
|
if (strlen(user) == sizeof(user)-1 || strlen(user) == 1){
|
||||||
|
// too long username or no username was entered
|
||||||
|
okusername = false;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
// replacing \n with \0
|
||||||
|
user[strcspn(user,"\n")] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
/* check to see if important variable is intact after input of login name - do not remove */
|
/* check to see if important variable is intact after input of login name - do not remove */
|
||||||
printf("Value of variable 'important 1' after input of login name: %*.*s\n",
|
printf("Value of variable 'important 1' after input of login name: %*.*s\n",
|
||||||
LENGTH - 1, LENGTH - 1, important1);
|
LENGTH - 1, LENGTH - 1, important1);
|
||||||
printf("Value of variable 'important 2' after input of login name: %*.*s\n",
|
printf("Value of variable 'important 2' after input of login name: %*.*s\n",
|
||||||
LENGTH - 1, LENGTH - 1, important2);
|
LENGTH - 1, LENGTH - 1, important2);
|
||||||
|
|
||||||
// using makepass.c to create hashed password -> saving it manually in passdb
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// using termios instead of the outdated getpass for a more secure program
|
||||||
|
// user_pass = getpass(prompt);
|
||||||
|
|
||||||
user_pass = getpass(prompt); //enter password using deprecated function :/
|
struct termios old_term_flags, new_term_flags;
|
||||||
passwddata = mygetpwnam(user); //get user info from passdb
|
|
||||||
|
|
||||||
|
/* Turn echoing off so password will not be shown in terminal */
|
||||||
|
if (tcgetattr(fileno(stdin), &old_term_flags) != 0){
|
||||||
|
perror("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
new_term_flags = old_term_flags;
|
||||||
|
new_term_flags.c_lflag &= ~ECHO;
|
||||||
|
if (tcsetattr(fileno(stdin), TCSAFLUSH, &new_term_flags) != 0) {
|
||||||
|
perror("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool okpassword = true;
|
||||||
|
printf("password: ");
|
||||||
|
if (fgets(user_pass,sizeof(user_pass),stdin) == NULL ) {
|
||||||
|
clearerr(stdin);
|
||||||
|
okpassword = false;
|
||||||
|
}
|
||||||
|
if (strlen(user_pass) == sizeof(user_pass)-1 || strlen(user_pass) == 1){
|
||||||
|
okpassword = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Restore terminal */
|
||||||
|
if (tcsetattr(fileno(stdin), TCSAFLUSH, &old_term_flags) != 0) {
|
||||||
|
perror("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!okpassword || !okusername){
|
||||||
|
printf("\nLogin Incorrect \n \n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
user_pass[strcspn(user_pass, "\n")] = '\0'; // replacing \n with \0
|
||||||
|
|
||||||
|
//////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
// get user info from passdb
|
||||||
|
passwddata = mygetpwnam(user);
|
||||||
|
|
||||||
if (passwddata != NULL) {
|
if (passwddata != NULL) {
|
||||||
/* You have to encrypt user_pass for this to work */
|
|
||||||
/* Don't forget to include the salt */
|
if (passwddata->pwfailed >= 10) {
|
||||||
|
printf("\nToo many failed login attempts. Your account has been locked.\nContact and Admin to unlock your account.\n");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Encryp user_pass with salt included */
|
||||||
char *user_pass_encrypted = crypt(user_pass, passwddata->passwd_salt);
|
char *user_pass_encrypted = crypt(user_pass, passwddata->passwd_salt);
|
||||||
if (&user_pass_encrypted == NULL) {
|
if (user_pass_encrypted == NULL) {
|
||||||
perror("Error");exit(EXIT_FAILURE);
|
perror("Error");exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
printf("encryption of typed pass: %s \n", user_pass_encrypted);
|
|
||||||
if (passwddata->pwfailed >= 10) {
|
|
||||||
printf("Too many failed login attempts. Your account has been locked.\nContact and Admin to unlock your account.\n");
|
|
||||||
} else if (!strcmp(user_pass_encrypted, passwddata->passwd)) {
|
|
||||||
|
|
||||||
printf(" You're in !\n");
|
if (!strcmp(user_pass_encrypted, passwddata->passwd)) {
|
||||||
|
// successfull login
|
||||||
|
printf("\n%d failed attemts to login \n",passwddata->pwfailed);
|
||||||
passwddata->pwfailed = 0;
|
passwddata->pwfailed = 0;
|
||||||
passwddata->pwage = passwddata->pwage + 1;
|
passwddata->pwage = passwddata->pwage + 1;
|
||||||
if (mysetpwent(user, passwddata) < 0) { /* execute the command */
|
|
||||||
|
if (mysetpwent(user, passwddata) < 0) {
|
||||||
perror("Error");exit(EXIT_FAILURE);
|
perror("Error");exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (passwddata->pwage > 100) {
|
if (passwddata->pwage > 10) {
|
||||||
printf(" Your password is OLD!!!!\n");
|
printf("You need to change your password !\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* check UID, see setuid(2) */
|
|
||||||
|
|
||||||
if (setuid(passwddata->uid) < 0) { /* execute the command */
|
|
||||||
perror("Error");exit(EXIT_FAILURE);
|
/* check UID, see setuid(2) */
|
||||||
|
if (setuid(passwddata->uid) < 0) { //replace this with the ID of the user running the program if you don't have sudo rights
|
||||||
|
perror("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
/* start a shell, use execve(2) */
|
/* start a shell, use execve(2) */
|
||||||
if (execve("/bin/bash",NULL,NULL) < 0) { /* execute the command */
|
if (execve("/bin/bash",NULL,NULL) < 0) {
|
||||||
perror("Error");exit(EXIT_FAILURE);
|
perror("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
} else {
|
printf("\nWelcome to this system !\n");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// user name exists but password does not match
|
||||||
passwddata->pwfailed = passwddata->pwfailed + 1;
|
passwddata->pwfailed = passwddata->pwfailed + 1;
|
||||||
if (mysetpwent(user, passwddata) < 0) { /* execute the command */
|
if (mysetpwent(user, passwddata) < 0) {
|
||||||
perror("Error");exit(EXIT_FAILURE);
|
perror("Error");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("Login Incorrect \n");
|
printf("\nLogin Incorrect \n \n");
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user