• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • scamdetection
scamdetection.cpp
Go to the documentation of this file.
1 /*
2  Copyright (c) 2013-2015 Montel Laurent <montel@kde.org>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "scamdetection.h"
19 #include "scamdetectiondetailsdialog.h"
20 #include "scamcheckshorturl.h"
21 #include "settings/globalsettings.h"
22 
23 #include <QWebElement>
24 #include <QWebFrame>
25 #include <QDebug>
26 
27 using namespace MessageViewer;
28 static QString IPv4_PATTERN = QLatin1String("\\b[0-9]{1,3}\\.[0-9]{1,3}(?:\\.[0-9]{0,3})?(?:\\.[0-9]{0,3})?");
29 static QString addWarningColor(const QString &url)
30 {
31  const QString error = QString::fromLatin1("<font color=#FF0000>%1</font>").arg(url);
32  return error;
33 }
34 
35 ScamDetection::ScamDetection(QObject *parent)
36  : QObject(parent),
37  mCheckShortUrl(new ScamCheckShortUrl(this))
38 {
39 }
40 
41 ScamDetection::~ScamDetection()
42 {
43 }
44 
45 ScamCheckShortUrl *ScamDetection::scamCheckShortUrl() const
46 {
47  return mCheckShortUrl;
48 }
49 
50 void ScamDetection::scanPage(QWebFrame *frame)
51 {
52 #ifndef KDEPIM_NO_WEBKIT
53  if (GlobalSettings::self()->scamDetectionEnabled()) {
54  mDetails.clear();
55  mDetails = QLatin1String("<b>") + i18n("Details:") + QLatin1String("</b><ul>");
56  bool foundScam = false;
57  const QWebElement rootElement = frame->documentElement();
58  bool result = scanFrame(rootElement, mDetails);
59  if (result) {
60  foundScam = true;
61  }
62  foreach(QWebFrame *childFrame, frame->childFrames()) {
63  result = scanFrame(childFrame->documentElement(), mDetails);
64  if (result) {
65  foundScam = true;
66  }
67  }
68  if (foundScam)
69  Q_EMIT messageMayBeAScam();
70  }
71 #endif
72 }
73 
74 bool ScamDetection::scanFrame(const QWebElement &rootElement, QString &details)
75 {
76 #ifndef KDEPIM_NO_WEBKIT
77  bool foundScam = false;
78  QRegExp ip4regExp;
79  ip4regExp.setPattern(IPv4_PATTERN);
80  const QWebElementCollection allAnchor = rootElement.findAll(QLatin1String("a"));
81  Q_FOREACH (const QWebElement &anchorElement, allAnchor) {
82  //1) detect if title has a url and title != href
83  const QString href = anchorElement.attribute(QLatin1String("href"));
84  const QString title = anchorElement.attribute(QLatin1String("title"));
85  const QUrl url(href);
86  if (!title.isEmpty()) {
87  if (title.startsWith(QLatin1String("http:"))
88  || title.startsWith(QLatin1String("https:"))
89  || title.startsWith(QLatin1String("www."))) {
90  if (title.startsWith(QLatin1String("www."))) {
91  const QString completUrl = url.scheme() + QLatin1String("://") + title;
92  if ( completUrl != href &&
93  href != (completUrl + QLatin1Char('/'))) {
94  foundScam = true;
95  }
96  } else {
97  if (href != title) {
98  // http://www.kde.org == http://www.kde.org/
99  if (href != (title + QLatin1Char('/'))) {
100  foundScam = true;
101  }
102  }
103  }
104  if (foundScam) {
105  details += QLatin1String("<li>") + i18n("This email contains a link which reads as '%1' in the text, but actually points to '%2'. This is often the case in scam emails to mislead the recipient", addWarningColor(title), addWarningColor(href)) + QLatin1String("</li>");
106  }
107  }
108  }
109  if (!foundScam) {
110  //2) detect if url href has ip and not server name.
111  const QString hostname = url.host();
112  if (hostname.contains(ip4regExp) && !hostname.contains(QLatin1String("127.0.0.1"))) { //hostname
113  details += QLatin1String("<li>") + i18n("This email contains a link which points to a numerical IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname))+QLatin1String("</li>");
114  foundScam = true;
115  } else if (hostname.contains(QLatin1Char('%'))) { //Hexa value for ip
116  details += QLatin1String("<li>") + i18n("This email contains a link which points to a hexadecimal IP address (%1) instead of a typical textual website address. This is often the case in scam emails.", addWarningColor(hostname))+QLatin1String("</li>");
117  foundScam = true;
118  } else if (url.toString().contains(QLatin1String("url?q="))) { //4) redirect url.
119  details += QLatin1String("<li>") + i18n("This email contains a link (%1) which has a redirection", addWarningColor(url.toString())) +QLatin1String("</li>");
120  foundScam = true;
121  } else if ((url.toString().count(QLatin1String("http://")) > 1) ||
122  (url.toString().count(QLatin1String("https://")) > 1)) { //5) more that 1 http in url.
123  if (!url.toString().contains(QLatin1String("kmail:showAuditLog"))) {
124  details += QLatin1String("<li>") + i18n("This email contains a link (%1) which contains multiple http://. This is often the case in scam emails.", addWarningColor(url.toString())) + QLatin1String("</li>");
125  foundScam = true;
126  }
127  }
128  }
129  //Check shortUrl
130  if (!foundScam) {
131  if (ScamCheckShortUrl::isShortUrl(url)) {
132  details += QLatin1String("<li>") + i18n("This email contains a shorturl (%1). It can redirect to another server.", addWarningColor(url.toString())) + QLatin1String("</li>");
133  foundScam = true;
134  }
135  }
136  }
137  //3) has form
138  if (rootElement.findAll(QLatin1String("form")).count() > 0) {
139  details += QLatin1String("<li></b>") + i18n("Message contains form element. This is often the case in scam emails.") + QLatin1String("</b></li>");
140  foundScam = true;
141  }
142  details += QLatin1String("</ul>");
143  return foundScam;
144 #else
145  return false;
146 #endif
147 }
148 
149 void ScamDetection::showDetails()
150 {
151  if (!mDetailsDialog) {
152  mDetailsDialog = new MessageViewer::ScamDetectionDetailsDialog;
153  }
154 
155  mDetailsDialog->setDetails(mDetails);
156  mDetailsDialog->show();
157 }
158 
159 
globalsettings.h
QWebFrame
QWebFrame::documentElement
QWebElement documentElement() const
scamdetectiondetailsdialog.h
QUrl::host
QString host() const
QUrl::toString
QString toString(QFlags< QUrl::FormattingOption > options) const
MessageViewer::ScamDetection::showDetails
void showDetails()
Definition: scamdetection.cpp:149
addWarningColor
static QString addWarningColor(const QString &url)
Definition: scamdetection.cpp:29
QWebElementCollection::count
int count() const
QString::clear
void clear()
QRegExp::setPattern
void setPattern(const QString &pattern)
MessageViewer::ScamDetection::messageMayBeAScam
void messageMayBeAScam()
QRegExp
MessageViewer::ScamCheckShortUrl
Definition: scamcheckshorturl.h:35
MessageViewer::ScamCheckShortUrl::isShortUrl
static bool isShortUrl(const KUrl &url)
Definition: scamcheckshorturl.cpp:102
MessageViewer::ScamDetectionDetailsDialog
Definition: scamdetectiondetailsdialog.h:28
MessageViewer::ScamDetection::scanPage
void scanPage(QWebFrame *frame)
Definition: scamdetection.cpp:50
MessageViewer::ScamDetection::ScamDetection
ScamDetection(QObject *parent=0)
Definition: scamdetection.cpp:35
QObject
scamdetection.h
QWebElement
QString::isEmpty
bool isEmpty() const
MessageViewer::GlobalSettings::self
static GlobalSettings * self()
Definition: globalsettings.cpp:34
QString::startsWith
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const
QString
IPv4_PATTERN
static QString IPv4_PATTERN
Definition: scamdetection.cpp:28
QUrl::scheme
QString scheme() const
MessageViewer::ScamDetection::scanFrame
static bool scanFrame(const QWebElement &rootElement, QString &details)
Definition: scamdetection.cpp:74
QWebElement::attribute
QString attribute(const QString &name, const QString &defaultValue) const
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
scamcheckshorturl.h
QUrl
QLatin1Char
MessageViewer::ScamDetection::~ScamDetection
~ScamDetection()
Definition: scamdetection.cpp:41
QWebFrame::childFrames
QList< QWebFrame * > childFrames() const
MessageViewer::ScamDetection::scamCheckShortUrl
ScamCheckShortUrl * scamCheckShortUrl() const
Definition: scamdetection.cpp:45
QLatin1String
QString::count
int count() const
QString::fromLatin1
QString fromLatin1(const char *str, int size)
QWebElement::findAll
QWebElementCollection findAll(const QString &selectorQuery) const
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
QWebElementCollection
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal