Team Ai
Apppublic

sameersyed/Defence_FrameWork

sourceHugging Faceupdated 8mo agoView on Hugging Face
0likes
image_security_analyzer.cpython-313.pyc101 linesDownload Raw Back to __pycache__
1�

2�i�-��`�SSKrSSKrSSKrSSKJr SSKrSSKJ	r	 SSK3JrJr "SS5r
g)�N)�Image)�MobileNetV2)�preprocess_input�decode_predictionsc�D�\rSrSrSrSrSrSrSrSr	Sr4S	rS5rg)�ImageSecurityAnalyzer�	c�B�[SSS9Ul[SSSS9Ulg)N�imagenetT)�weights�include_topF�avg)rr
�pooling)r�model�feature_extractor)�selfs �2D:\Security_app\backend\image_security_analyzer.py�__init__�ImageSecurityAnalyzer.__init__6s"�� ���F��7�!,�Z�U�\a�!b���c	�*�SU;aURS5Sn[R"U5n[R"[8R"U55RS5nURupEURU5URX4U5URU5URU5UUURURS.S.nURU5nXvS'U$![ anSS[#U530sS	nA$S	nAff=f)9z1Comprehensive security analysis of uploaded image�,��RGB)�width�height�format�mode)�adversarial_check�10spam_check�malicious_check�
quality_check�metadata�overall_risk�errorzAnalysis failed: N)�split�base64�	b64decoder�open�io�BytesIO�convert�size�_detect_adversarial�_detect_spam�_detect_malicious_content�_analyze_qualityrr�_calculate_overall_risk�	Exception�str)	r�11image_data�image_bytes�imagerr�resultsr$�es	         r�
analyze_image�#ImageSecurityAnalyzer.analyze_images��	;��j� �'�-�-�c�2�1�5�12� �*�*�:�6�K��J�J�r�z�z�+�6�7�?�?��F�E�!�J�J�M�E�&*�%=�%=�e�%D�"�/�/��f�E�#'�#A�#A�%�#H�!%�!6�!6�u�!=�"�$�#�l�l�!�J�J�	��G� �7�7��@�L�&2�N�#��N���	;��0��Q���9�:�:��	;�s�C+C.�.13D�8D
�D�
Dc��[R"URS55n[R"USS9n[	U5nUR14R
USS9n[USS9SnUSSnUSSUSS-15nSn/nUS	:aUS16-
nURSUSS
35 US:aUS-
nURSUSS
35 USSn	SU	R5;dSU	R5;aUS-
nURS5 [US5US:�aSO	US:�aSOSUUSS[U5S.$! SS/SS.s$=f)z<Detect adversarial perturbations using prediction confidence)��r=r��axis)�verbose�)�top�rg�?�(zLow prediction confidence (�.2f�)g�������?�zSmall confidence gap (�noise�pattern�zUnusual pattern detected�d�<�HIGH�MEDIUM�LOW)�17risk_score�severity�18indicators�top_prediction�19confidence)rPrQrRrT)�np�array�resize�expand_dimsrr�predictr�append�lower�min�float)20rr7�	img_array�predictions�decoded�top_confidence�confidence_gaprPrR�predicted_classs21          rr.�)ImageSecurityAnalyzer._detect_adversarial1s���(	[�������j�!9�:�I����y�q�9�I�(��3�I��*�*�,�,�Y��,�B�K�(��!�<�Q�?�G�$�Q�Z��]�N�$�Q�Z��]�W�Q�Z��]�:�N��J��J���#��b� �22��!�!�$?��s�?S�ST�"U�V���#��b� �23��!�!�$:�>�#�:N�a�"P�Q�&�a�j��m�O��/�/�/�1�1�Y�/�BW�BW�BY�5Y��b� �24��!�!�"<�=�"�*�c�2�&0�2�o�F�z�TV��8�\a�(�")�!�*�Q�-�#�N�3��
��	[�"#��b�XY�Z�Z�s�EE�	Ec	��[R"U5nSn/n/SQnX#4U;aUS-
nURSUSU35 X#-nUS:�dUS:aUS-
nURS	US2535 [R"U5n	U	S:�aUS-
nURS
U	SS35 [R"U5n26U27S:�aUS-
nURSU28SS35 [[R"URSURS5SS95nX#-nX�S-:aUS-
nURS5 [US5US:�aSUS.$US:�aSUS.$SUS.$) z'Detect spam/advertising characteristicsr))i��Z)i,��)i�rL)�iX)iPi�#zCommon ad size: �x�g�?�zUnusual aspect ratio: rE���z
Very bright (z.0fz/255)�KzHigh contrast (rF�����rCr>g{�G�z�?�29z!Low color diversity (flat design)rKrLrMrGrNrO�rPrQrR)30rUrVrZ�mean�std�len�unique�reshape�shaper\)
rr7rrr^rPrR�ad_sizes�aspect_ratio�31brightness�contrast�
unique_colors�total_pixelss
             rr/�"ImageSecurityAnalyzer._detect_spam]s����H�H�U�O�	��32��33�N���?�h�&��"��J���� 0���q���A�B��~���!��|�d�2��"��J���� 6�|�C�6H�I�J��W�W�Y�'�34�����"��J����
�j��-=�U�C�D��6�6�)�$���b�=��"��J�������~�Q�?�@��B�I�I�i�&7�&7��I�O�O�A�<N�&O�VW�X�Y�
��~���$�.�.��"��J����A�B��j�#�.�",�r�/��$�35�	36�CM�PR�?�x�$�37�	38�X]�$�39�	40rc�P�[R"U5nSn/nUS-n[R"U[R"US-5-5*nUS:�aUS-
nUR	S5 [R41"U5nUS:aUS-
nUR	S	5 URSS42:�aPURSS43:�a=USS2SS24n[R"U5n	U	S:aUS
-
nUR	S5 [US5US:�aSUS.$US:�aSUS.$SUS.$)z#Detect potential malicious patternsrrg�����|�=g�������?rGz)High LSB entropy (possible steganography)rqrJz)Very low variance (suspicious uniformity)�2N�rlz,Suspicious corner pattern (possible trigger)rKrL�CRITICALrMrOrr)	rUrV�sum�log2rZ�varrxrtr\)44rr7r^rPrR�lsb�lsb_entropy�pixel_variance�corner_patch�45corner_stds46          rr0�/ImageSecurityAnalyzer._detect_malicious_content�sI���H�H�U�O�	��47��48��!�m���v�v�c�B�G�G�C�%�K�$8�8�9�9������"��J����I�J����	�*���B���"��J����I�J��?�?�1���"�y���q�'9�B�'>�$�S�b�S�#�2�#�X�.�L�����-�J��A�~��b� �49��!�!�"P�Q��j�#�.�&0�2�o�50�$�51�	52�EO�RT�_�6�$�53�	54�Z_�$�55�	56rc��[R"U5n[[R"U55[[R"U55[[R57"[R"UR[5555[[R"URSURS5SS95S.nU$)zAnalyze image quality metricsrprCrr>)r{r|�	sharpness�color_diversity)rUrVr]rsrtr��gradient�astyperurvrwrx)rr7r^�metricss    rr1�&ImageSecurityAnalyzer._analyze_quality�s����H�H�U�O�	� ����	� 2�3��b�f�f�Y�/�0��r�v�v�b�k�k�)�2B�2B�5�2I�&J�K�L�"�2�9�9�Y�->�->�r�9�?�?�ST�CU�-V�]^�#_�`�	58���rc��SU;aSSS.$USSS-USSS	--US59SS	--nUS:�aSnOUS
:�aSnOUS:�aSnOSn[U5US.$)z%Calculate overall security risk scorer%r�UNKNOWN)�score�levelrrPg�������?r g333333�?r!�Fr�r�rMrGrNrO)�int)rr8�total_scorer�s    rr2�-ImageSecurityAnalyzer._calculate_overall_risk�s����g����3�3�
�'�(��6��<��L�!�,�/�#�5�
6��%�&�|�4�s�:�
;�	�����E�
�2�
��E�
�2�
��E��E���%��60�	61rc��SU;aSUS3$/nURS5 URSUSSSUSSS	35 URS625 USnURS5 URS
USSUSS35 URSUSS35 URSUSS35 USnURSUSS35 URSUSS35 US(a"USHnURSUS35 M OURS 5 URS5 US!nURS"USS35 URSUSS35 US(a"USHnURSUS35 M OURS#5 URS5 US$nURS%USS35 URSUSS35 US(a"USHnURSUS35 M OURS&5 URS5 URS635 URS'5 USSS(:�aEURS)5 URS*5 URS+5 URS,5 O�USSS-:�aEURS.5 URS/5 URS05 URS15 OsUSSS2:�a4URS35 URS45 URS55 O3URS65 URS75 URS85 S9RU5$):zGenerate clean security reportr%u🚨 Error: u%🔒 Image Security Analysis Report64 65zOverall Risk: r$r�z (r�z/100)66 67u�────────────────────────────────────────────────────────────68 69r#u📊 Image Information:70u• Dimensions: rrjrz pixels71u• Format: r�72u• Color Mode: rz73 74ru⚠️ Adversarial Detection: rQz   Risk Score: rPz/10075rRu   • u(   ✓ No adversarial patterns detected76r u📧 Spam Detection: u   ✓ No spam characteristics77r!u🛡️ Malicious Content: u&   ✓ No malicious patterns detected78u💡 Recommendations:79 80r�u%🚨 CRITICAL: Do not use this image81u(• Image shows multiple security risks82u?• May contain adversarial perturbations or malicious content83u6• Verify source and re-obtain from trusted location84r�u#⚠️ HIGH RISK: Use with caution85u(• Apply input validation before using86u/• Consider preprocessing (resize, normalize)87u+• Monitor model behavior with this input88rGu1⚡ MEDIUM RISK: Generally safe with precautions89u'• Standard preprocessing recommended90u$• Verify image source if possible91u!✅ LOW RISK: Image appears safe92u.• No significant security concerns detected93u,• Safe to use with standard preprocessing94�)rZ�join)r�analysis�report_parts�meta�adv�ind�spam�mals        r�generate_report�%ImageSecurityAnalyzer.generate_report�s����h��!�(�7�"3�!4�5�5���	���E�F����n�X�n�-E�g�-N�,O�r�RZ�[i�Rj�kr�Rs�Qt�t}�~�����/�0��95�#�����7�8����.�t�G�}�o�Q�t�H�~�>N�i�X�Y����l�4��>�*:�"�=�>����.�t�F�|�n�D�A�B��*�+�����<�S��_�<M�R�P�Q����o�c�,�.?�-@��G�H��|���<�(���#�#�g�c�U�"�$5�6�)�
��� K�L����D�!���%�����3�D��4D�3E�R�H�I����o�d�<�.@�-A��H�I�����L�)���#�#�g�c�U�"�$5�6�*�
��� B�C����D�!��(�)�����9�#�j�/�9J�"�M�N����o�c�,�.?�-@��G�H��|���<�(���#�#�g�c�U�"�$5�6�)�
��� I�J����D�!����/�0�	���7�8��N�#�G�,�r�1���� H�I���� K�L���� b�c���� Y�Z�
�n�
%�g�
.��
3���� F�G���� K�L���� R�S���� N�O�
�n�
%�g�
.��
3���� T�U���� J�K���� G�H���� D�E���� Q�R���� O�P��w�w�|�$�$r)rrN)
�__name__�96__module__�__qualname__�__firstlineno__rr:r.r/r0r1r2r��__static_attributes__�rrrr	s1��c�97 ;�D*[�X)98�V#99�J�100�2L%rr)r'r*�numpyrU�PILr�101tensorflow�tf�tensorflow.keras.applicationsr�*tensorflow.keras.applications.mobilenet_v2rrrr�rr�<module>r�s&��
�	����5�[�V%�V%r