Jelajahi Sumber

* Added MD5FILE()
* Added binmd5, not being parsed as of yet......


svn: 825

Bryan Drewery 22 tahun lalu
induk
melakukan
5749dfcad8
6 mengubah file dengan 59 tambahan dan 15 penghapusan
  1. 18 1
      src/conf.c
  2. 1 0
      src/conf.h
  3. 38 0
      src/crypt.c
  4. 2 0
      src/crypt.h
  5. 0 13
      src/misc.c
  6. 0 1
      src/misc.h

+ 18 - 1
src/conf.c

@@ -216,6 +216,7 @@ void init_conf() {
   conffile.uname = NULL;
   conffile.username = NULL;
   conffile.homedir = NULL;
+  conffile.md5 = NULL;
 }
 /*
  * Return the PID of a bot if it is running, otherwise return 0
@@ -304,6 +305,7 @@ void showconf() {
   conf_bot *bot = NULL;
 
   sdprintf("---------------------------CONF START---------------------------");
+  sdprintf("md5      : %s", conffile.md5);
   sdprintf("uid      : %d", conffile.uid);
   sdprintf("uname    : %s", conffile.uname);
   sdprintf("username : %s", conffile.username);
@@ -341,6 +343,7 @@ void free_conf() {
     /* must also free() anything malloc`d in conf_addbot() */
     free(bot);
   }
+  free(conffile.md5);
   free(conffile.uname);
   free(conffile.username);
   free(conffile.homedir);
@@ -349,6 +352,15 @@ void free_conf() {
 }
 
 int parseconf() {
+  char *md5 = MD5FILE(binname);
+
+  if (!conffile.md5) {
+    conffile.md5 = strdup(md5);
+  } 
+  /* fuck it
+  else if (conffile.md5 && strcmp(conffile.md5, md5))
+    fatal("Nice try :)", 0);
+  */
   if (!conffile.bots->nick && !conffile.bots->next) /* no bots ! */
     werr(ERR_NOBOTS);
 
@@ -464,6 +476,9 @@ int readconf(char *cfile)
         } else if (!strcmp(option, "binname")) {        /* filename of the binary? */
           str_redup(&conffile.binname, line);
 
+        } else if (!strcmp(option, "binmd5")) {        	/* binary hash */
+          str_redup(&conffile.md5, line);
+
         } else if (!strcmp(option, "portmin")) {
           if (egg_isdigit(line[0]))
             conffile.portmin = atoi(line);
@@ -551,7 +566,6 @@ int writeconf(char *filename, FILE *stream, int bits) {
   if ((bits & CONF_COMMENT) && conffile.uid != myuid)
     my_write(f, "#! uid %d\n\n", myuid);
 
-
   if (!conffile.uname || (conffile.uname && conffile.autouname && strcmp(conffile.uname, my_uname()))) {
     comment("# Automatic");
     my_write(f, "! uname %s\n", my_uname());
@@ -581,6 +595,9 @@ int writeconf(char *filename, FILE *stream, int bits) {
   comment("# binname is relative to binpath, if you change this, you'll need to manually remove the old one from crontab.");
   my_write(f, "! binname %s\n", conffile.binname);
 
+  comment("# DO NOT TOUCH THIS.");
+  my_write(f, "! binmd5 %s\n", conffile.md5);
+
   comment("");
 
   comment("# portmin/max are for incoming connections (DCC) [0 for any]");

+ 1 - 0
src/conf.h

@@ -20,6 +20,7 @@ typedef struct conf_bot_b {
 } conf_bot;
 
 typedef struct conf_b {
+  char *md5;		/* md5 hash of the binary */
   uid_t uid;
   char *uname;
   char *username;       /* shell username */

+ 38 - 0
src/crypt.c

@@ -279,6 +279,30 @@ char *MD5(const char *string)
   return md5string;
 }
 
+char *
+MD5FILE(const char *bin)
+{
+  static char     md5string[MD5_HASH_LENGTH + 1] = "";
+  unsigned char   md5out[MD5_HASH_LENGTH + 1] = "", buffer[1024] = "";
+  MD5_CTX ctx;
+  size_t binsize = 0, len = 0;
+  FILE *f = NULL;
+
+  if (!(f = fopen(bin, "rb")))
+    return "";
+
+  MD5_Init(&ctx);
+  while ((len = fread(buffer, 1, sizeof buffer, f))) {
+    binsize += len;
+    MD5_Update(&ctx, buffer, len);
+  }
+  MD5_Final(md5out, &ctx);
+  strncpyz(md5string, btoh(md5out, MD5_DIGEST_LENGTH), sizeof(md5string));
+  OPENSSL_cleanse(&ctx, sizeof(ctx));
+
+  return md5string;
+}
+
 char *SHA1(const char *string)
 {
   static char	  sha1string[SHA_HASH_LENGTH + 1] = "";
@@ -292,3 +316,17 @@ char *SHA1(const char *string)
   OPENSSL_cleanse(&ctx, sizeof(ctx));
   return sha1string;
 }
+
+/* convert binary hashes to hex */
+char *btoh(const unsigned char *md, int len)
+{
+  int i;
+  char buf[100] = "", *ret = NULL;
+
+  for (i = 0; i < len; i++)
+    sprintf(&(buf[i*2]), "%02x", md[i]);
+
+  ret = buf;
+  return ret;
+}
+

+ 2 - 0
src/crypt.h

@@ -21,6 +21,7 @@
 #define md5cmp(hash, string)            strcmp(hash, MD5(string))
 
 char *MD5(const char *);
+char *MD5FILE(const char *);
 char *SHA1(const char *);
 char *encrypt_string(const char *, char *);
 char *decrypt_string(const char *, char *);
@@ -30,5 +31,6 @@ char *decryptit (char *);
 int lfprintf (FILE *, ...);
 void EncryptFile(char *, char *);
 void DecryptFile(char *, char *);
+char *btoh(const unsigned char *, int);
 
 #endif /* !_CRYPT_H */

+ 0 - 13
src/misc.c

@@ -1094,19 +1094,6 @@ char *replace(const char *string, char *oldie, char *newbie)
   return (newstring);
 }
 
-/* convert binary hashes to hex */
-char *btoh(const unsigned char *md, int len)
-{
-  int i;
-  char buf[100] = "", *ret = NULL;
-
-  for (i = 0; i < len; i++)
-    sprintf(&(buf[i*2]), "%02x", md[i]);
-
-  ret = buf;
-  return ret;
-}
-
 #define HELP_BOLD  1
 #define HELP_REV   2
 #define HELP_UNDER 4

+ 0 - 1
src/misc.h

@@ -32,7 +32,6 @@ char *wbanner();
 char *color(int, int, int);
 void shuffle(char *, char *);
 void showhelp(int, struct flag_record *, char *);
-char *btoh(const unsigned char *, int);
 int listen_all(int, int);
 char *replace(const char *, char *, char *);
 int goodpass(char *, int, char *);