This is my code for a server running a login manager, that log into a file the malicious access and print out the result of the wrong login. The chars user and pass come from the user input using the socket.
if ((memcmp(user, "admin", strlen("admin")) == 0)) {
/*code... */
}
else {
char msg[600];
strcpy (msg,"Login error with ");
strcat (msg,"user: ");
strcat (msg,user);
strcat (msg," password: ");
strcat (msg,pass);
strcat (msg," from: ");
strcat (msg, client_ip);
puts (msg);
logfile->Write(msg);
return false;
}
Well, the problem is the output both on the output console and in the logfile.
Like this:
Login error with user: lol
password: asd
:��ܔ��P{w� from: 127.0.0.1
Why are there the strange asci chars? How can avoid the new line since they come from user input by socket?
As multiple people have commented about, this snippet of code contains nothing C++ specific, so I'm answering as if you are working in plain C.
I'm guessing, since you use
memcmpabove, that your input strings are not null terminated.strcatwill keep on appendingchars from whatever the pointer wanders into until it runs into a'\0'. You'll need to add a null terminator if you want to use user or password as a C-style string, or else usestrncatand pass the length.Also, beware of overrunning
msg. You might have better luck usingsnprintfto format your message, since it accepts a maximum output string length.