Showing posts with label barcode scanner. Show all posts
Showing posts with label barcode scanner. Show all posts

Wednesday, November 2, 2016

Scan BarCode in Android barcodescanner:zxing Library



Step: 1
======



Credits to author of the Library.
https://github.com/dm77/barcodescanner

Add library to your build.gradle file under app folder

compile 'me.dm7.barcodescanner:zxing:1.9'


Step: 2
======

MainActivity.java
==================

package com.pratap.scanbarcode;

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText txt_barcodevalue;

Button btn_scan;

final int REQUEST_CAMERA_PERMISSION = 1001;
final int DELAY_TIME = 4000;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txt_barcodevalue = (EditText) findViewById(R.id.txt_barcodevalue);
btn_scan = (Button) findViewById(R.id.btn_scan);

btn_scan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkCameraPermissions();

}
});
}

public void openBarCodeScanner() {


Intent scanIntent = new Intent(this, BarCodeScannerActivity.class);
startActivityForResult(scanIntent, 1006);

}


public void checkCameraPermissions() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
openBarCodeScanner();
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
}
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);

if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
openBarCodeScanner();
} else {
// permission denied, boo!
showMessage("Camera Permission Denied!!!");
}
}
}


public void showMessage(String message) {

Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == 1006) {
if (resultCode == Activity.RESULT_OK && data != null) {

String barcodeNumber = data.getStringExtra("BarCodeNumber");
txt_barcodevalue.setText(barcodeNumber);

}
}


}

}


XML Layout file for Main Activity
=========================

activity_main.xml
=============


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:orientation="horizontal">

<EditText
android:id="@+id/txt_barcodevalue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="text" />

<Button
android:id="@+id/btn_scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scan" />

</LinearLayout>


</RelativeLayout>





Step: 3
======

BarCodeScannerActivity.java
=====================
Please add this Activity to your AndroidManifest file.


package com.pratap.scanbarcode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.zxing.Result;

import me.dm7.barcodescanner.zxing.ZXingScannerView;

public class BarCodeScannerActivity extends Activity implements ZXingScannerView.ResultHandler {
private ZXingScannerView mScannerView;

@Override
public void onCreate(Bundle state) {
super.onCreate(state);
mScannerView = new ZXingScannerView(this);
setContentView(mScannerView);
}

@Override
public void onResume() {
super.onResume();
mScannerView.setResultHandler(this);
mScannerView.startCamera();
}

@Override
public void onPause() {
super.onPause();
mScannerView.stopCamera();
}

@Override
public void handleResult(Result rawResult) {
// Do something with the result here
Log.v("Scan Result", rawResult.getText()); // Prints scan results
Log.v("Scan Qr code format", rawResult.getBarcodeFormat().toString()); // Prints the scan format (qrcode, pdf417 etc.)

Toast.makeText(BarCodeScannerActivity.this, rawResult.getText(), Toast.LENGTH_SHORT).show();

// If you would like to resume scanning, call this method below:
// mScannerView.resumeCameraPreview(this);

Intent intentMessage = new Intent();

// put the message in Intent
intentMessage.putExtra("BarCodeNumber", rawResult.getText());
// Set The Result in Intent
setResult(RESULT_OK, intentMessage);
// finish The activity
finish();


}
}



Step: 4
======

AndroidManifest.xml
===================


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pratap.scanbarcode">


<uses-permission android:name="android.permission.CAMERA" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>


<activity
android:name=".BarCodeScannerActivity"
android:screenOrientation="portrait">

</activity>
</application>

</manifest>


Demo
======













References
========
Credits to author of the Library.
https://github.com/dm77/barcodescanner


















Tuesday, November 17, 2015

Hackers Manipulate Barcodes to Execute Malicious Commands When Scanned

When it comes to hackers, it seems they’ve taken every avenue, everything from car hacking, to sniper rifle hacking, even artificial organ hacking, but no were not done yet. A new group of researchers have begun manipulating little tiny lines of data we all know as barcodes, to easily manipulate computers to execute their every command.
During this weeks PanSec 2015 Conference in Tokoyo, researchers with Tencent’s Xuanwu Lab debuted a number of attacks using poisoned barcodes scanned by any number of average barcode scanners, where they were able to open a shell and easily execute commands on a machine. The attacks, dubbed BadBarcode, are relatively simple to execute and the researchers behind the attack said it’s difficult to pinpoint where the vulnerability stems from, if it’s the host system that needs to be patched, or both or the possibility of a total different avenue.
“We do not know what the bad guys might do. BadBarcode can execute any commands in the host system, or [implant]a Trojan,” said Yang Yu, a key researcher who collaborated with his fellow colleague Hyperchem Ma. Yu is an extremely skilled researchers, last year being rewarded with a $100,000 payout from Microsoft for a trio of ASLR and DEP bypasses. “So basically you can do anything with BadBarcode,” Yu explained
Also See:Bypass Windows Bitlocker Disk Encryption In A Few Seconds


Yu said his team was able to exploit the fact that a majority of barcodes contain not only numeric and alphanumeric characters, but also full ASCII characters depending on the protocol being utilized. Barcode scanners are essentially keyboard emulators and if they support protocols such as Code128 which support ASCII control characters, an attacker could craft a barcode that once scanned opens a shell on the computer to which commands can be sent.
During their presentation, Yu and Ma said that Ctrl+ commands map to ASCII code and can be used to trigger hotkeys, which registered with the Ctrl+ prefix, to launch common dialogues such as OpenFile, SaveFile and PrintDialog. Attackers can leverage hotkeys to browse the computer’s file system, launch browsers and even execute programs.
BadBarcode is able to to execute a wide range of attacks, with the key being adding special control characters to the barcode that will tell the barcode reader to activate host system hotkeys, that will then allow you to begin activating desired functions. BadBarcode is simple, you can print the poisoned barcodes yourself on average paper.”
Fixing the issue is a tricky one, Yu said, due to the fact that it’s not limited to particular set scanners so no sole manufacture or vendor can be at direct fault. Vendors affected by BadBarcode include Esky, Symbol, Honeywell, and TaoTronics.
“BadBarcode is not a vulnerability of a certain product,” Yu explained. “It affects the entire barcode scanner-related industries. It’s even difficult to say that BadBarcode is the problem of scanners or host systems. So when we discovered BadBarcode, we even [did]not know which manufacturer should be reported.”
Also See:Large-Scale DDOS Attacks on TOR Exit Nodes


One potential fix suggested by Yu is forcing barcode scanner manufacturers to not enable additional features beyond standard scanning protocols by default, nor should they be allowed to transmit ASCII control characters to the host device by default. Another solution is to think twice about using barcode scanners that emulate keyboards, and always ensure you disable hotkeys.
Simple vulnerabilities this like could prove critical in the real world, posing as a potentially huge issue on the rise.
Also See:Charge your smartphone to 50% in just five minutes