PW from cin

This commit is contained in:
j3d1 2018-09-05 21:51:13 +02:00
parent 15d7e00c29
commit 56affbe423
9 changed files with 246 additions and 183 deletions

3
.gitmodules vendored Normal file
View file

@ -0,0 +1,3 @@
[submodule "libs/libsodium"]
path = libs/libsodium
url = https://github.com/jedisct1/libsodium.git

View file

@ -5,7 +5,7 @@ DEBUG = -g
prefix=/usr/local prefix=/usr/local
all: shepherd rsa all: shepherd
shepherd: src/*.cpp src/*.h shepherd: src/*.cpp src/*.h
mkdir -p bin mkdir -p bin
@ -18,6 +18,14 @@ rsa: rsa.cpp
install: bin/shepherd install: bin/shepherd
install -m 0755 bin/shepherd $(prefix)/bin install -m 0755 bin/shepherd $(prefix)/bin
.PHONY: test
test:
test/test.sh test/test.db
# remove produced files, invoke as "make clean" # remove produced files, invoke as "make clean"
clean: clean:
rm -f bin/* rm -f bin/*
#%.o : %.cpp
# $(CC) $(CFLAGS) -c $^ -o $@

1
libs/libsodium Submodule

@ -0,0 +1 @@
Subproject commit 11eef91e4924f0a8130731125db3a88cf605805c

43
rsa.cpp
View file

@ -1,43 +0,0 @@
#include <fstream>
#include <iostream>
#include <stdio.h>
#include <string>
#include <openssl/aes.h>
using namespace std;
static const unsigned char key[] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
int main()
{
unsigned char text[]="hello world!";
unsigned char enc_out[80];
unsigned char dec_out[80];
AES_KEY enc_key, dec_key;
AES_set_encrypt_key(key, 128, &enc_key);
AES_encrypt(text, enc_out, &enc_key);
AES_set_decrypt_key(key,128,&dec_key);
AES_decrypt(enc_out, dec_out, &dec_key);
int i;
printf("original:\t");
for(i=0;*(text+i)!=0x00;i++)
printf("%X ",*(text+i));
printf("\nencrypted:\t");
for(i=0;*(enc_out+i)!=0x00;i++)
printf("%X ",*(enc_out+i));
printf("\ndecrypted:\t");
for(i=0;*(dec_out+i)!=0x00;i++)
printf("%X ",*(dec_out+i));
printf("\n");
return 0;
}

View file

@ -1,56 +1,49 @@
#include <iostream> #include <iostream>
#include <string>
#include <cstring>
#include "Database.h" #include "Database.h"
using namespace std; using namespace std;
bool Database::open(string filename) bool Database::open(string filename) {
{ if (sqlite3_open(filename.c_str(), &database) == SQLITE_OK)
if(sqlite3_open(filename.c_str(), &database) == SQLITE_OK) return true;
return true;
return false; return false;
} }
vector<vector<string> > Database::query2(string query) vector<vector<string> > Database::query2(string query) {
{ sqlite3_stmt *statement;
sqlite3_stmt *statement; vector<vector<string> > results;
vector<vector<string> > results; if (!strcmp(query.c_str(), ""))
if(!strcmp(query.c_str(),"")) return results; return results;
if(sqlite3_prepare_v2(database, query.c_str(), -1, &statement, 0) == SQLITE_OK) if (sqlite3_prepare_v2(database, query.c_str(), -1, &statement, 0)
{ == SQLITE_OK) {
int cols = sqlite3_column_count(statement); int cols = sqlite3_column_count(statement);
int result = 0; int result = 0;
while(true) while (true) {
{ result = sqlite3_step(statement);
result = sqlite3_step(statement);
if(result == SQLITE_ROW) if (result == SQLITE_ROW) {
{ vector<string> values;
vector<string> values; for (int col = 0; col < cols; col++) {
for(int col = 0; col < cols; col++) values.push_back(
{ (char *) sqlite3_column_text(statement, col));
values.push_back((char*)sqlite3_column_text(statement, col)); }
} results.push_back(values);
results.push_back(values); } else {
} break;
else }
{ }
break;
}
}
sqlite3_finalize(statement); sqlite3_finalize(statement);
} }
string error = sqlite3_errmsg(database); string error = sqlite3_errmsg(database);
if(error != "not an error") cout << query << " " << error << endl; if (error != "not an error")
cout << query << " " << error << endl;
return results; return results;
} }
void Database::close() void Database::close() {
{ sqlite3_close(database);
sqlite3_close(database);
} }

View file

@ -7,47 +7,62 @@
#include <vector> #include <vector>
#include <sqlite3.h> #include <sqlite3.h>
using namespace std; using namespace std;
class Database{ class Database {
private: private:
sqlite3 *database; sqlite3 *database;
class QueryStream : public std::ostream { class QueryStream : public std::ostream {
private: private:
class QueryBuf : public std::stringbuf { class QueryBuf : public std::stringbuf {
private: private:
Database *m_db; Database *m_db;
public: public:
QueryBuf(Database *db) { m_db = db;} QueryBuf(Database *db) {
~QueryBuf() { pubsync(); } m_db = db;
int sync() { }
m_db->result = m_db->query2(str());
str("");
return 0;
}
};
public: ~QueryBuf() {
QueryStream(Database *db) : std::ostream(new QueryBuf(db)) {} pubsync();
~QueryStream() { delete rdbuf(); } }
};
int sync() {
m_db->result = m_db->query2(str());
str("");
return 0;
}
};
public:
QueryStream(Database *db) :
std::ostream(new QueryBuf(db)) {
}
~QueryStream() {
delete rdbuf();
}
};
public: public:
bool open(string filename); bool open(string filename);
vector<vector<string> > query2(string query); vector<vector<string> > query2(string query);
void close();
void close();
vector<vector<string> > result; vector<vector<string> > result;
QueryStream query; QueryStream query;
Database(const std::string& filename) : query(this) { Database(const std::string &filename) :
database = NULL; query(this) {
open(filename); database = NULL;
}; open(filename);
~Database(){} };
~Database() {
}
}; };
#endif #endif

View file

@ -6,7 +6,6 @@
*/ */
#include <iostream> #include <iostream>
#include <regex> #include <regex>
#include <string>
#include "Manager.h" #include "Manager.h"
@ -27,7 +26,7 @@ int Manager::add(string pattern, string passwd) {
std::cmatch sm; std::cmatch sm;
if (regex_match(pattern.c_str(), sm, if (regex_match(pattern.c_str(), sm,
std::regex( std::regex(
"^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)@([a-zA-Z0-9\\.\\-]+)$"))) { "^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)@([a-zA-Z0-9/_\\.\\-]+)$"))) {
db.query << "INSERT INTO passwd (type, user, host, passwd) VALUES('" db.query << "INSERT INTO passwd (type, user, host, passwd) VALUES('"
<< sm[1] << "', '" << sm[2] << "','" << sm[3] << "','" << passwd << sm[1] << "', '" << sm[2] << "','" << sm[3] << "','" << passwd
<< "');" << flush; << "');" << flush;
@ -43,10 +42,16 @@ int Manager::add(string pattern, string passwd) {
} }
int Manager::create(string pattern) { int Manager::create(string pattern) {
cout << "12345" << endl; char charset[] = "ABCDEFGHIJKLMOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
db.query int len = strlen(charset);
<< "INSERT INTO passwd (type, user, host, passwd) VALUES('http', 'fish','test.de','12345');"
<< flush; string secret;
for (int i = 0; i < 20; i++) {
secret += charset[rand() % len];
}
cout << secret << endl;
add(pattern, secret);
return 0; return 0;
} }
@ -65,13 +70,70 @@ int Manager::clear() {
} }
int Manager::get(string pattern) { int Manager::get(string pattern) {
db.query std::cmatch sm;
<< "SELECT type, user, host, passwd FROM passwd WHERE type='http' AND user='blub';" if (regex_match(pattern.c_str(), sm,
<< flush; std::regex(
for (vector<string> row : db.result) { "^([a-zA-Z0-9%]+):([a-zA-Z0-9%/_\\.\\-]+)@([a-zA-Z0-9%/_\\.\\-]+)$"))) {
cout << row.at(0) << "://" << row.at(1) << "@" << row.at(2) << "\t" db.query << "SELECT * FROM passwd WHERE 1";
<< row.at(3) << endl; if (string("*").compare(sm[1]) != 0)
db.query << " AND type LIKE '" << sm[1] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND user LIKE '" << sm[2] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND host LIKE '" << sm[3] << "'";
db.query << ";" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else if (regex_match(pattern.c_str(), sm,
std::regex("^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)$"))) {
db.query
<< "SELECT * FROM passwd WHERE (type, user, host, passwd) VALUES('"
<< sm[1] << "', '" << sm[2] << "','" << sm[2] << "');" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else {
cout << "fehler: " << pattern << endl;
return 1;
}
}
int Manager::del(string pattern) {
std::cmatch sm;
if (regex_match(pattern.c_str(), sm,
std::regex(
"^([a-zA-Z0-9%]+):([a-zA-Z0-9%/_\\.\\-]+)@([a-zA-Z0-9%/_\\.\\-]+)$"))) {
db.query << "DELETE FROM passwd WHERE 1";
if (string("*").compare(sm[1]) != 0)
db.query << " AND type LIKE '" << sm[1] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND user LIKE '" << sm[2] << "'";
if (string("*").compare(sm[2]) != 0)
db.query << " AND host LIKE '" << sm[3] << "'";
db.query << ";" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else if (regex_match(pattern.c_str(), sm,
std::regex("^([a-zA-Z0-9]+):([a-zA-Z0-9/_\\.\\-]+)$"))) {
db.query
<< "DELETE FROM passwd WHERE (type, user, host, passwd) VALUES('"
<< sm[1] << "', '" << sm[2] << "','" << sm[2] << "');" << flush;
for (vector<string> row : db.result) {
cout << row.at(0) << ":" << row.at(1) << "@" << row.at(2) << "\t"
<< row.at(3) << endl;
}
return 0;
} else {
cout << "fehler: " << pattern << endl;
return 1;
} }
return 0;
} }

View file

@ -12,16 +12,24 @@
#include <string> #include <string>
#include "Database.h" #include "Database.h"
class Manager { class Manager {
public: public:
Manager(std::string); Manager(std::string);
~Manager(); ~Manager();
int add(std::string pattern, std::string passwd); int add(std::string pattern, std::string passwd);
int create(std::string pattern); int create(std::string pattern);
int show(); int show();
int clear(); int clear();
int get(std::string pattern); int get(std::string pattern);
int del(std::string pattern);
private: private:
Database db; Database db;
}; };

View file

@ -1,7 +1,6 @@
#include <sstream> #include <sstream>
#include <iostream>
#include <regex> #include <regex>
#include <string> #include <stdlib.h>
#include <getopt.h> #include <getopt.h>
#include "Manager.h" #include "Manager.h"
@ -16,86 +15,103 @@ constexpr unsigned int arg(const char* str, int h = 0) {
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
srand(time(NULL));
int opt, index; int opt, index;
string file = "passwd.db"; string file = getenv("HOME");
file += "/.shepherd/passwd.db";
const struct option longopts[] = { { "version", no_argument, 0, 'v' }, { const struct option longopts[] = {
"help", no_argument, 0, 'h' }, { "reverse", no_argument, 0, 'r' }, { {"version", no_argument, 0, 'v'},
"permanent", no_argument, 0, 's' }, { "debug", optional_argument, 0, {
'd' }, { "password", required_argument, 0, 'p' }, { "user", "help", no_argument, 0, 'h'},
required_argument, 0, 'u' }, { "interface", required_argument, 0, {"reverse", no_argument, 0, 'r'},
'i' }, { "header", required_argument, 0, 'b' }, { "hex", {
required_argument, 0, 'x' }, { "file", required_argument, 0, 'f' }, "permanent", no_argument, 0, 's'},
{ "timeout", required_argument, 0, 't' }, { "wait", {"debug", optional_argument, 0, 'd'},
required_argument, 0, 'w' }, { 0, 0, 0, 0 }, }; {
"password", required_argument, 0, 'p'},
{"user", required_argument, 0, 'u'},
{
"interface", required_argument, 0, 'i'},
{"header", required_argument, 0, 'b'},
{
"hex", required_argument, 0, 'x'},
{"file", required_argument, 0, 'f'},
{
"timeout", required_argument, 0, 't'},
{"wait", required_argument, 0, 'w'},
{
0, 0, 0, 0},};
while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts, while ((opt = getopt_long(argc, argv, "bhrvswxp:u:i:f:t:d::", longopts,
&index)) != -1) { &index)) != -1) {
switch (opt) { switch (opt) {
/*case 'h': /*case 'h':
fprintf(stderr, VERSION); fprintf(stderr, VERSION);
fprintf(stderr, USAGE, argv[0]); fprintf(stderr, USAGE, argv[0]);
fprintf(stderr, HELP); fprintf(stderr, HELP);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break; break;
case 'v': case 'v':
fprintf(stderr, VERSION); fprintf(stderr, VERSION);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
break;*/ break;*/
case 'r': case 'r':
//options.flags |= FLAG_REVERSE; //options.flags |= FLAG_REVERSE;
break; break;
case 'd': case 'd':
//options.flags |= FLAG_DEBUG; //options.flags |= FLAG_DEBUG;
//if (optarg != NULL) //if (optarg != NULL)
// options.debug_level = atoi(optarg); // options.debug_level = atoi(optarg);
break; break;
case 't': case 't':
//options.timeout = atol(optarg); //options.timeout = atol(optarg);
break; break;
case 'f': case 'f':
file = std::string(optarg); file = std::string(optarg);
break; break;
default: /* '?' */ default: /* '?' */
//cerr << "Unknown option" << endl; //cerr << "Unknown option" << endl;
//cerr << argv[0] << " <account identifier>" << endl; //cerr << argv[0] << " <account identifier>" << endl;
exit (EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
Manager mg(file); Manager mg(file);
switch (argc - optind) { switch (argc - optind) {
case 0: case 0:
mg.show(); mg.show();
break; break;
case 1: case 1:
if (!strcmp(argv[optind], "clear")) {
mg.clear();
cerr << file << " cleared" << endl;
} else {
mg.get(argv[optind]); mg.get(argv[optind]);
}
break; break;
case 2: case 2:
if (!strcmp(argv[optind], "new")) { if (!strcmp(argv[optind], "clear")) {
mg.create(argv[optind+1]); if (!strcmp(argv[optind + 1], "all")) {
} mg.clear();
break; cerr << file << " cleared" << endl;
case 3: }
if (!strcmp(argv[optind], "add")) { } else if (!strcmp(argv[optind], "gen")) {
mg.add(argv[optind+1], argv[optind+2]); mg.create(argv[optind + 1]);
} else { } else if (!strcmp(argv[optind], "del")) {
cerr << argv[0] << " <account identifier>" << endl; mg.del(argv[optind + 1]);
} } else if (!strcmp(argv[optind], "add")) {
string pw;
cout << argv[optind + 1] << ": " << flush;
cin >> pw;
mg.add(argv[optind + 1], pw);
} else {
cerr << argv[0] << " <account identifier>" << endl;
}
break; break;
} }
exit (EXIT_SUCCESS); exit (EXIT_SUCCESS);