File size: 7,740 Bytes
5a72624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Content script - Runs on every page
console.log('Phishing Detector: Content script loaded');

// Extract page-level features from DOM
function extractPageFeatures() {
  const features = {};
  
  try {
    // Favicon analysis
    const favicon = document.querySelector('link[rel*="icon"]');
    const currentDomain = window.location.hostname;
    if (favicon) {
      const faviconUrl = new URL(favicon.href, window.location.href);
      features.Favicon = faviconUrl.hostname !== currentDomain ? 1 : -1;
    } else {
      features.Favicon = 1; // No favicon is suspicious
    }
    
    // Request_URL - Check if objects loaded from different domain
    const images = document.querySelectorAll('img, video, audio');
    let externalResources = 0;
    images.forEach(img => {
      try {
        const imgUrl = new URL(img.src, window.location.href);
        if (imgUrl.hostname !== currentDomain) externalResources++;
      } catch (e) {}
    });
    const externalRatio = images.length > 0 ? externalResources / images.length : 0;
    features.Request_URL = externalRatio > 0.22 ? 1 : (externalRatio > 0.61 ? 0 : -1);
    
    // URL_of_Anchor - Check anchor tags
    const anchors = document.querySelectorAll('a');
    let suspiciousAnchors = 0;
    anchors.forEach(a => {
      const href = a.getAttribute('href');
      if (!href || href === '#' || href.startsWith('javascript:')) {
        suspiciousAnchors++;
      } else {
        try {
          const anchorUrl = new URL(href, window.location.href);
          if (anchorUrl.hostname !== currentDomain) suspiciousAnchors++;
        } catch (e) {}
      }
    });
    const anchorRatio = anchors.length > 0 ? suspiciousAnchors / anchors.length : 0;
    features.URL_of_Anchor = anchorRatio > 0.31 ? 1 : (anchorRatio > 0.67 ? 0 : -1);
    
    // Links_in_tags - Check meta, script, link tags
    const metaLinks = document.querySelectorAll('meta, script[src], link[href]');
    let externalMetaLinks = 0;
    metaLinks.forEach(tag => {
      const src = tag.getAttribute('src') || tag.getAttribute('href') || tag.content;
      if (src) {
        try {
          const url = new URL(src, window.location.href);
          if (url.hostname !== currentDomain) externalMetaLinks++;
        } catch (e) {}
      }
    });
    const metaRatio = metaLinks.length > 0 ? externalMetaLinks / metaLinks.length : 0;
    features.Links_in_tags = metaRatio > 0.17 ? 1 : (metaRatio > 0.81 ? 0 : -1);
    
    // SFH - Server Form Handler
    const forms = document.querySelectorAll('form');
    let suspiciousForms = 0;
    forms.forEach(form => {
      const action = form.getAttribute('action');
      if (!action || action === '' || action === 'about:blank') {
        suspiciousForms++;
      } else {
        try {
          const formUrl = new URL(action, window.location.href);
          if (formUrl.hostname !== currentDomain) suspiciousForms++;
        } catch (e) {
          suspiciousForms++;
        }
      }
    });
    features.SFH = forms.length > 0 && suspiciousForms > 0 ? 1 : -1;
    
    // Submitting_to_email - Check if form submits to email
    let hasEmailSubmit = false;
    forms.forEach(form => {
      const action = form.getAttribute('action');
      if (action && action.startsWith('mailto:')) hasEmailSubmit = true;
    });
    features.Submitting_to_email = hasEmailSubmit ? 1 : -1;
    
    // Redirect - Count meta redirects
    const metaRefresh = document.querySelector('meta[http-equiv="refresh"]');
    features.Redirect = metaRefresh ? 1 : -1;
    
    // on_mouseover - Check for mouseover events that change status bar
    let hasMouseoverStatusChange = false;
    anchors.forEach(a => {
      const mouseover = a.getAttribute('onmouseover') || a.getAttribute('onmouseout');
      if (mouseover && (mouseover.includes('window.status') || mouseover.includes('location'))) {
        hasMouseoverStatusChange = true;
      }
    });
    features.on_mouseover = hasMouseoverStatusChange ? 1 : -1;
    
    // RightClick - Check if right-click is disabled
    const hasRightClickDisable = document.body.getAttribute('oncontextmenu') === 'return false' ||
                                  document.oncontextmenu === null;
    features.RightClick = hasRightClickDisable ? 1 : -1;
    
    // popUpWidnow - Check for popup windows
    const scripts = document.querySelectorAll('script');
    let hasPopup = false;
    scripts.forEach(script => {
      if (script.textContent.includes('window.open') || 
          script.textContent.includes('popup')) {
        hasPopup = true;
      }
    });
    features.popUpWidnow = hasPopup ? 1 : -1;
    
    // Iframe - Check for iframes
    const iframes = document.querySelectorAll('iframe');
    features.Iframe = iframes.length > 0 ? 1 : -1;
    
    return features;
  } catch (error) {
    console.error('Error extracting page features:', error);
    return null;
  }
}

// Listen for messages from popup or background
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  if (request.action === 'getCurrentURL') {
    sendResponse({ url: window.location.href });
  }
  
  if (request.action === 'getPageFeatures') {
    const features = extractPageFeatures();
    sendResponse({ features: features });
  }
  
  if (request.action === 'showWarning') {
    showPhishingWarning();
  }
  
  return true;
});

// Show warning banner on page
function showPhishingWarning() {
  // Check if warning already exists
  if (document.getElementById('phishing-warning-banner')) {
    return;
  }
  
  const banner = document.createElement('div');
  banner.id = 'phishing-warning-banner';
  banner.style.cssText = `
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: linear-gradient(135deg, #ff6b6b 0%, #c92a2a 100%);
    color: white;
    padding: 15px;
    text-align: center;
    font-family: Arial, sans-serif;
    font-size: 16px;
    font-weight: bold;
    z-index: 999999;
    box-shadow: 0 2px 10px rgba(0,0,0,0.3);
    animation: slideDown 0.5s ease;
  `;
  
  banner.innerHTML = `
    <div style="max-width: 1200px; margin: 0 auto; display: flex; align-items: center; justify-content: space-between;">
      <div style="flex: 1;">
        ⚠️ <strong>WARNING:</strong> This site has been identified as potentially dangerous!
      </div>
      <button id="close-warning" style="
        background: rgba(255,255,255,0.2);
        border: 2px solid white;
        color: white;
        padding: 8px 20px;
        border-radius: 5px;
        cursor: pointer;
        font-weight: bold;
        margin-left: 20px;
      ">
        Dismiss
      </button>
    </div>
  `;
  
  // Add animation
  const style = document.createElement('style');
  style.textContent = `
    @keyframes slideDown {
      from {
        transform: translateY(-100%);
      }
      to {
        transform: translateY(0);
      }
    }
  `;
  document.head.appendChild(style);
  
  document.body.insertBefore(banner, document.body.firstChild);
  
  // Close button
  document.getElementById('close-warning').addEventListener('click', () => {
    banner.remove();
  });
  
  // Blur page content
  document.body.style.filter = 'blur(2px)';
  
  // Remove blur when warning is dismissed
  const observer = new MutationObserver(() => {
    if (!document.getElementById('phishing-warning-banner')) {
      document.body.style.filter = '';
      observer.disconnect();
    }
  });
  
  observer.observe(document.body, { childList: true });
}

// Auto-check on page load (optional - can be enabled in settings)
// Uncomment to enable automatic checking
/*
window.addEventListener('load', () => {
  chrome.runtime.sendMessage({ 
    action: 'checkPage', 
    url: window.location.href 
  });
});
*/