Listing zum Beitrag (Link)
#include
#include
#include
#include
char schemasuff[50];
int main()
{
int gui = 200;
char c;
char b;
char wort1[20] = {0};
char wort2[210] = {0};
char merker[210] = {0};
char *schema(char *text);
void lezeilo(char *str);
void pass(char path[1035], char klar[100],int zahl1,int zahl2);
void lower_string(char s[]);
char limit[] = ",";
char befehl[250];
char wortbas[200];
char pwgen[1035];
char passlast[100];
char passklar[100];
char datei[20];
char dateiG[20];
extern char schemasuff[50];
int zaehler = 0;
int zaehlbas = 0;
int schalter1 = 0;
int schalter2 = 0;
time_t zeit;
int zahl1,zahl2;
srand((unsigned) time(&zeit));
strcpy(passlast,"\0");
printf("Dateiname LDAP User (Benutzer1.ldif): ");
fgets(datei,20,stdin);
lezeilo(datei);
if (strlen(datei)<6) strcpy(datei,"Benutzer1.ldif");
FILE *exldap;
FILE *imldap;
FILE *passl;
passl = fopen("passliste.txt", "w");
imldap = fopen("import.ldif", "w");
if ( (exldap = fopen(datei, "r")) == NULL) {
printf("\nUser Falsch\n");
exit(0);
}
printf("Dateiname LDAP Gruppe (Gruppen1.ldif): ");
fgets(dateiG,20,stdin);
lezeilo(dateiG);
if (strlen(dateiG)<6) strcpy(dateiG,"Gruppen1.ldif");
FILE *exGldap;
FILE *imGldap;
imGldap = fopen("importG.ldif", "w");
if ( (exGldap = fopen(dateiG, "r")) == NULL) {
printf("\nGruppe Falsch\n");
exit(0);
}
printf("Schema (DC=bsp,DC=test,DC=de): ");
fgets(schemasuff,50,stdin);
lezeilo(schemasuff);
if (strlen(schemasuff)<6) strcpy(schemasuff,"DC=bsp,DC=test,DC=de");
printf("\n");
while((c = fgetc(exldap))!=EOF) {
if ((schalter2 == 0) && (c == 'd' || c == 'i' || c == 'o') && (zaehler == 0)) {
schalter2 = 1;
//
// Ausblenden der Kommentare zu Beginn
}
if ((schalter1 == 0) && (schalter2 == 1) && (c != '\n')) {
wort1[zaehler++] = c;
} else if ((schalter1 == 1) && (schalter2 == 1) && (c != '\n'))
{
wort2[zaehler++] = c;
}
if ((schalter1 == 0 && schalter2 == 1 && c == ' ') || (schalter2 == 1 && c == '\n')) {
if (schalter1 == 0 && zaehler > 1) {
schalter1 = 1;
} else if ((schalter1 == 1 && c == '\n'))
{
// Schema Aenderung
if (strcmp(wort1,"dn:: ") == 0) {
// Sonderfall dn:: Base64
// encode schreiben und lesen
lezeilo(wort2);
strcpy(befehl,"echo ");
strcat(befehl,wort2);
strcat(befehl,"|base64 -d > base.txt");
system(befehl);
FILE* base = fopen("base.txt", "r");
while((b = fgetc(base))!=EOF) {
wortbas[zaehlbas++] = b;
}
strcpy(wort2,schema(wortbas));
zaehlbas = 0;
memset(wortbas,'\0',200);
fclose(base);
// decode schreiben und lesen
strcpy(befehl,"echo ");
strcat(befehl,wort2);
strcat(befehl,"|base64 > base.txt");
system(befehl);
FILE* baseR = fopen("base.txt", "r");
while((b = fgetc(base))!=EOF) {
if (b != '\n') wortbas[zaehlbas++] = b;
}
strcpy(wort2,wortbas);
// printf("Durchlauf %i\n",durch);
// durch++;
zaehlbas = 0;
memset(wortbas,'\0',200);
fclose(baseR);
}
if (strstr(wort1,"dn::") || strstr(wort1,"dn:")) {
//printf("\n");
/*
Injektion Passwort
*/
//fprintf(passl,"%s;",wort2);
strcpy(merker,wort2);
lezeilo(merker);
fprintf(imldap,"\n");
}
if (strcmp(wort1,"dn: ") == 0) strcpy(wort2,schema(wort2));
// Namensaenderung
if (strcmp(wort1,"sAMAccountName: ") == 0) {
strcpy(wort1,"uid: ");
/*
Injektion Passwort
*/
strcpy(pwgen,"\0");
strcpy(passklar,"\0");
zahl1 = rand() % 10;
zahl2 = rand() % 10;
pass(pwgen,passklar,zahl1,zahl2);
lezeilo(pwgen);
fprintf(imldap,"userpassword: %s\n",pwgen);
fprintf(passl,"%s;%s;%s;%s\n",merker,pwgen,passklar,wort2);
strcpy(pwgen,"\0");
strcpy(passklar,"\0");
strcpy(merker,"\0");
lower_string(wort2);
}
if (strncmp(wort2,"person",5) == 0 ) strcpy(wort1,"#");
if (strncmp(wort2,"user",4) == 0 ) strcpy(wort1,"#");
if (strncmp(wort2,"organizationalPerson",10) == 0 ) strcpy(wort2,"inetOrgPerson");
if (strncmp(wort1,"name:: ",6) == 0 ) strcpy(wort1,"#");
//
if (schalter1 == 1 && c != ' ') {
if (strcmp(wort1,"#") != 0) {
//printf("%s%s\n",wort1,wort2);
fprintf(imldap,"%s%s\n",wort1,wort2);
}
memset(wort2,'\0',200);
memset(wort1,'\0',20);
schalter1 = 0;
}
}
zaehler = 0;
}
if (schalter2 == 0 && c == '\n') {
zaehler = 0;
} else if (schalter2 == 0) {
zaehler++;
}
}
//printf("\n# Benutzer Fertig\n");
fprintf(imldap,"\n");
// fclose(exldap);
fclose(imldap);
fclose(passl);
// Beginn der Routine fuer Gruppen Konvertierung
strcpy(befehl,"\0");
while(fgets(befehl,250,exGldap)) {
if ((strstr(befehl,"#") == 0) && strlen(befehl)>3) {
zaehler = strlen(befehl);
strncpy(wort1,strtok(befehl," "),20);
if (zaehler>1) {
strncpy(wort2,strtok(NULL,"\0"),200);
} else {
strcpy(wort2,"\0");
}
lezeilo(wort2);
if (strcmp(wort2,"group\r") == 0) strcpy(wort2,"posixgroup");
if (strcmp(wort1,"dn:") == 0) {
if ( gui > 200 ) {
fprintf(imGldap,"gidnumber: %i\n",gui);
}
gui++;
fprintf(imGldap,"\n");
strncpy(wortbas,strtok(wort2,","),200);
strcat(wortbas,",OU=Gruppen,");
strcat(wortbas,schemasuff);
strcat(wortbas,"\0");
strncpy(wort2,wortbas,200);
}
/* if (strcmp(wort1,"member::") == 0) {
// Sonderfall Base64
strcpy(wort1,"member:");
zaehlbas = 0;
lezeilo(wort2);
strcpy(befehl,"echo ");
strncat(befehl,wort2,200);
strncat(befehl,"|base64 -d > base.txt",30);
system(befehl);
FILE* base = fopen("base.txt", "r");
while((b = fgetc(base))!=EOF) {
if (b != '\n') wortbas[zaehlbas++] = b;
}
fclose(base);
strncpy(wort2,wortbas,200);
}
*/
if (strncmp(wort1,"member:",6) == 0) {
// member nach cn suchen
rewind(exldap);
lezeilo(wort2);
if (strncmp(wort1,"member:",7) == 0) strcpy(wortbas,"dn: ");
if (strncmp(wort1,"member::",8) == 0) strcpy(wortbas,"dn:: ");
strcat(wortbas,wort2);
// Zeilenweise durchlaufen der AD Datei
schalter1 = 0;
while(fgets(befehl,250,exldap)) {
if (strncmp(befehl,wortbas,20) == 0) schalter1 = 1;
if ((strncmp(befehl,"sAMAccountName:",15) == 0) && schalter1 == 1) {
strcpy(wort1,"memberuid: ");
strcpy(wort2,strtok(befehl," "));
strcpy(wort2,strtok(NULL,"\r"));
// lezeilo(wort2);
lower_string(wort2);
schalter1 = 0;
}
}
}
fprintf(imGldap,"%s %s\n",wort1,wort2);
//printf("%s %s\n",wort1,wort2);
}
}
fprintf(imGldap,"gidnumber: %i\n",gui);
//printf("\n# Gruppe Fertig\n");
fprintf(imGldap,"\n");
// fclose(exldap);
// fclose(exGldap);
fclose(imGldap);
printf("\nOK\n");
return 0;
}
char *schema(char *text) {
int laenge = 0;
int stop = 0;
int zaehler1 = 0;
int zaehler2 = 54;
extern char schemasuff[50];
char c;
char *teil1;
char *teil2;
char teil3[] = "OU=Benutzer,";
char **z = (char**) malloc((5)*sizeof(char*));
char *teil;
char limit[] = ",";
laenge = strlen(text);
if (laenge<60) exit(0);
// Schema zerschneiden
teil = (char*)malloc(200);
teil = strtok(text,limit);
while(teil != NULL) {
z[zaehler1] = teil;
// printf("Teil: %s\n",z[zaehler1]);
teil = strtok(NULL,limit);
zaehler1++;
}
teil1= z[0];
teil2= z[zaehler1-6];
zaehler1 = 0;
strncpy(text,teil1,strlen(teil1));
strncat(text,limit,1);
// Segmentation fault
strncat(text,teil2,7);
strncat(text,limit,1);
strcat(teil3,schemasuff);
strcat(teil3,"\0");
strcat(text,teil3);
return text;
}
// lezeilo --> letztes Zeichen loeschen
void lezeilo(char *str) {
size_t p=strlen(str);
str[p-1]='\0';
}
void pass(char path[1035], char klar[100], int zahl1, int zahl2)
{
char befehl[200];
FILE *fp;
char helfer[1035];
strcpy(path,"\0");
strcpy(klar,"\0");
// sechsstelliges Passwort
switch(zahl1) {
case 1: strcat(klar,"hUj3"); break;
case 2: strcat(klar,"e5J7"); break;
case 3: strcat(klar,"tTu8"); break;
case 4: strcat(klar,"aub1"); break;
case 5: strcat(klar,"a7Vj"); break;
case 6: strcat(klar,"i5co"); break;
case 7: strcat(klar,"Dfva"); break;
case 8: strcat(klar,"7cXm"); break;
case 9: strcat(klar,"juts"); break;
default: strcat(klar,"PO68"); break;
}
switch(zahl2) {
case 1: strcat(klar,"sdpK"); break;
case 2: strcat(klar,"q4km"); break;
case 3: strcat(klar,"7G6b"); break;
case 4: strcat(klar,"vesm"); break;
case 5: strcat(klar,"tTau"); break;
case 6: strcat(klar,"elqx"); break;
case 7: strcat(klar,"wbk3"); break;
case 8: strcat(klar,"sHmb"); break;
case 9: strcat(klar,"2mh6"); break;
default: strcat(klar,"aps5"); break;
}
strcpy(befehl,"passtest.sh ");
strcat(befehl,klar);
fp = popen(befehl, "r");
if (fp == NULL) {
printf("Fehler!\n" );
exit(1);
}
while (fgets(helfer, sizeof(path), fp) != NULL) {
//printf("%s", path);
strcat(path,helfer);
}
pclose(fp);
}
void lower_string(char s[]) {
// schieben der ASCII Werte zu lowercase
int c = 0;
while (s[c] != '\0') {
if (s[c] >= 'A' && s[c] <= 'Z') {
s[c] = s[c] + 32;
}
c++;
}
}
Du muss angemeldet sein, um einen Kommentar zu veröffentlichen.