1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| #include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
long GetFileSize( std::ifstream & Fichier )
{
// sauvegarder la position courante
long pos = Fichier.tellg();
// se placer en fin de fichier
Fichier.seekg( 0 , std::ios_base::end );
// récupérer la nouvelle position = la taille du fichier
long size = Fichier.tellg() ;
// restaurer la position initiale du fichier
Fichier.seekg( pos, std::ios_base::beg ) ;
return size ;
}
std::string min_maj(const std::string & chaine){
std::string s;
s=chaine;
std::transform( s.begin(), s.end(), s.begin(), static_cast<int (*)(int)>(toupper) );
return s;
}
std::string maj_min(const std::string & chaine){
std::string s;
s=chaine;
std::transform( s.begin(), s.end(), s.begin(), static_cast<int (*)(int)>(tolower) );
return s;
}
std::string string_hex(const std::string & buffer){ // converte un string en un string héxadécimal
std::ostringstream oss; //initialise un string stream qui stock la sortie de std::hex
for (std::string::const_iterator i = buffer.begin(); i != buffer.end(); ++i) //fait une boucle caractére par caractére
{
oss << std::hex << static_cast<int>(*i); //stock la sortie de std::hex dans oss
}
return(oss.str());
}
bool is_matching( // comparaison d'une chaine à une expression réguliére , elle n'ignore maintenant que les ?
const std::string & texte,
const std::string & motif
){
if (texte.length()!=motif.length()) return false;
for(std::size_t i=0;i<texte.length();i++){
if (motif[i]=='?') continue;
if(motif[i]!=texte[i]) return false;
}
return true;
}
struct couple {
std::string nom;
std::string code;
};
/// Ces deux fonctions font part de extraire_signatures , ne pas les utiliser !!!
std::string interpret_nom(const std::string & var_inter)
{
std::string nom,code;
std::istringstream is(var_inter+"\n");
is >> nom >> code;
return(nom);
}
std::string interpret_code(const std::string & var_inter)
{
std::string nom,code;
std::istringstream is(var_inter+"\n");
is >> nom >> code;
return(code);
}
/// Fin des deux fonctions
/// Début de la fonction extraire_signatures , c'est la fonction à utiliser
std::vector<couple> extraire_signatures(const std::string & n_fichier) // retourne un tableau qui contient les signatures et leurs noms , FONCTIONNE NICKEL !!!!
{
couple c;
std::vector<couple> virus;
// le constructeur de ifstream permet d'ouvrir un fichier en lecture
std::ifstream fichier;
// ouverture en lecture du fichier
fichier.open(n_fichier.c_str(),ios::in);
std::string ligne; // variable contenant chaque ligne lue
// cette boucle s'arrête dès qu'une erreur de lecture survient
while ( std::getline( fichier, ligne ) )
{
c.nom=interpret_nom(ligne);
c.code=interpret_code(ligne);
virus.push_back(c);
}
fichier.close();
return(virus);
} /// pour cette fonction , on donne le nom du fichier de signatures , son chemin est optionnel , elle retourne un tableau VECTOR qui contient deux éléments : nom et code
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Cette fonction cherche une chaine héxadécimal ( donc la signature aussi ) dans un fichier qu'elle code en héxadécimal , fonctionne NICKEL !!!
int recherche_fichier(
const std::string & n_fichier,
const std::string & motif_hex
){
std::string texte1;
char* texte2;
std::ifstream fichier;
bool sortie;
int pos1,length1;
int k=0;
// ouverture en lecture du fichier
fichier.open(n_fichier.c_str(),ios::in);
for (int j=0;j<GetFileSize(fichier);j++){
fichier.seekg(j);
texte2=new char[motif_hex.length()/2];
fichier.read(texte2,motif_hex.length()/2);
texte1=texte2;
sortie=is_matching(min_maj(string_hex(texte1)),min_maj(motif_hex));
if (sortie==true){
k++;
}
}
return k;
}
/// Note : si on veut chercher une chaine normale dans un fichier avec cette fonction , on l'utilise comme ça , resultat=recherche_fichier("Nom du fichier",string_hex("Une chaine normale"),&vector_pos);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Fonction de scan des virus , paramétres : Nom du fichier à scanner et le nom du fichier de signatures ///
std::vector<std::string> scan_virus(
const std::string & n_fichier,
const std::string & f_signatures
){
std::vector<couple> signatures;
signatures=extraire_signatures(f_signatures);
std::vector<std::string> nom_virus;
int i;
for (i=0;i<signatures.size();i++){
if (recherche_fichier(n_fichier,signatures[i].code)>=1){
nom_virus.push_back(signatures[i].nom);
}
}
return(nom_virus);
} |